January 29, 2009
No Comments
As internet applications get more and more complex and start to become more like desktop applications and less like electronic brochures, they can become more and more complex to test to ensure that everything is working properly.
There are numerous strategies to cope with this including the development of “test based” development, where tests are written at the same time, or even before, the actual code of the site is. This way, any changes to the codebase can quickly be checked for bugs by running the test suite.
One interesting addition to this philosophy is selenium which takes this idea a bit further and actually automates browser based testing.
After a bunch of digging around and some help from the nice people on the #php channel on freenode IRC, I have cleared up the issue regarding PHP script execution and client connection.
If a client disconnects (closes the browser window / tab) and PHP subsequently tries to send information to the client but fails, the script stops executing.
Echoing does not necessarily mean sending information as PHP uses an output buffer. If you use the flush() function though you can force the buffer to be flushed to the client.
If you want to be sure a script will continue to execute when a client has disconnected, you can use the ignore_user_abort() function which will do exactly that.
Problem Solved
January 28, 2009
No Comments
Some times you are working on a database that is just too big to export and import via phpMyAdmin. In those circumstances the best alternative is to use the Linux command line, usually via SSH.
If you want to be able to download the file easily via FTP, you need to first of all go to your web root. For example:
cd /home/myhome/public_html
If you are not sure where you web root is, you can log in via FTP and copy the path displayed in the remote pane of the FTP programme.
Then to create the database dump run the following command, replacing the dbuser and dbname with the mysql username and the name of the database you want to back up.
mysqldump --opt -u dbuser -p dbname > dbname.sql
You will then be prompted to enter the password for that mysql user. Enter this and mysql will then create an SQL dump file in your web root.
If the dump file is really big, it is usually a good idea to then compress this file to make moving it around a bit easier. To create a gzip compressed copy of the file, use the following command, replacing dbname.sql with the actual database name you used.
gzip -c dbname.sql>dbname.sql.gz
At this point you might decide to delete the uncompressed SQL file to conserve space. To do that run the following command, again replacing dbname.sql with the actual name of the database.
rm -f dbname.sql
January 27, 2009
No Comments
Sometime you need a PHP script to check for the existence of a MySQL table. This function achieves that for you.
function db_table_exists($table){
$exists = false;
$tables_query = db_query("SHOW TABLES FROM " . MYSQL_DB);
while($t = mysql_fetch_assoc($tables_query)){
foreach($t as $k=>$v){
if($v == $table){
$exists = true;
break;
}
}
}
return $exists;
}
A lot of hosting accounts do not have CLI PHP enabled. If you have some maintainence scripts that you want to run on a regular basis then you can still do this using cron combined with cURL.
For example check out this crontab command:
# 0 0 * * * * /usr/bin/curl http://www.mydomain.co.uk/currency_update.php
This will run the currency update script every night.
To edit your crontab simply log into the server via SSH and then type the following command:
crontab -e
Hit [i] to go into insert mode. Then type your command. Then hit [esc] to get out of insert mode. Finally type :wq to save the changes and quit.
If your command is not right, you will get a message saying so with an option to try again at editing the file.
January 23, 2009
4 Comments
If you installed Magento via any other method than the downloader, then when you come to use Magento Connect, you will find that you are unable to due to permissions. For Magento Connect to work, all folders must have 777 permissions.
If you have SSH access to your server, you can fix this by running the following command:
find ./ –type d –exec chmod 777 {} \;
If you don’t have SSH access you could try a php based SSH emulator instead.
The other option is to use Filezilla 3 to recursively chmod the files for you. This can take a little while to run, but at least you don’t have to do it manually!
January 22, 2009
No Comments
Upgrading Magento can be a little tricky. I would definitely advise testing out any upgrades on a development copy of your site, not applying updates directly to the live site without testing for problems first.
Being such a young platform, Magento is in a phase of rapid development. Inevitably this development will introduce bugs and issues. Hopefully these will generally be minor but even so, you would want to get them resolved before applying them to your live site.
That said, here are some ways to upgrade Magento:
1. Via Magento Connect
Go to Magento Connect (System-Magento Connect in Admin Menu). Then in the extension key bit paste the following:
magento-core/Mage_All_Latest
2. Via Command Line (SSH Access Required)
If you have SSH access to your host, you can run the following commands to upgrade.
./pear mage-setup .
./pear install -f magento-core/Mage_All_Latest-stable
3. Clean Install over Existing Database
Finally you can download the latest package and install it, but point it at an existing Magento database. In theory it should then upgrade the Magento database to suit the current version. This method has not been that reliable for me though.
January 21, 2009
7 Comments
There are a few simple steps you need to take to move your Magento based store from one place to another.
First of all, create a full SQL dump of your store’s database and import this to a database on the new server. To do this, simply use the backup functionality within the Magento Admin. If you use phpMyAdmin things can become a bit weird.
Then copy all files apart from the contents of the /var/ folder to the web root of the new server.
Once the DB and the files are all in place, you need to edit the database settings in app/etc/local.xml to reflect the settings of the new server
Then you need to actually edit the DB and update two fields. To find the fields to edit run this query:
SELECT * FROM core_config_data WHERE path = 'web/unsecure/base_url' OR path = 'web/secure/base_url';
Then update these two fields to reflect the new base URL.
That’s it.
Originally stated in this helpful forum post
January 19, 2009
8 Comments
Don’t get me wrong, I love phpMyAdmin and use it every day. However sometimes on client servers the only access to the database is via phpMyAdmin and it hasn’t been set up properly. One really irritating example is when trying to dump a database so that it can be installed on my local development server. Do the export etc etc and then phpMyAdmin gives you the download, but no – its an empty file…
After messing around for 10 minutes you realise that no matter what you do, you aint getting your DB dump this way.
In steps this script, as mentioned here.
Just in case that link dies – here is the body of the script – with all credit and thanks to the original author:
<?php
set_time_limit(0);
/*---------------------------------------------------+
| mysqldump.php
+----------------------------------------------------+
| Copyright 2006 Huang Kai
| hkai@atutility.com
| http://atutility.com/
+----------------------------------------------------+
| Released under the terms & conditions of v2 of the
| GNU General Public License. For details refer to
| the included gpl.txt file or visit http://gnu.org
+----------------------------------------------------*/
/*
change log:
2006-10-16 Huang Kai
---------------------------------
initial release
2006-10-18 Huang Kai
---------------------------------
fixed bugs with delimiter
add paramter header to add field name as CSV file header.
2006-11-11 Huang Kia
Tested with IE and fixed the <button> to <input>
*/
$mysqldump_version="1.02";
$print_form=1;
$output_messages=array();
//test mysql connection
if( isset($_REQUEST['action']) )
{
$mysql_host=$_REQUEST['mysql_host'];
$mysql_database=$_REQUEST['mysql_database'];
$mysql_username=$_REQUEST['mysql_username'];
$mysql_password=$_REQUEST['mysql_password'];
if( 'Test Connection' == $_REQUEST['action'])
{
_mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password);
}
else if( 'Export' == $_REQUEST['action'])
{
_mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password);
if( 'SQL' == $_REQUEST['output_format'] )
{
$print_form=0;
//ob_start("ob_gzhandler");
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="'.$mysql_host."_".$mysql_database."_".date('YmdHis').'.sql"');
echo "/*mysqldump.php version $mysqldump_version */\n";
_mysqldump($mysql_database);
//header("Content-Length: ".ob_get_length());
//ob_end_flush();
}
else if( 'CSV' == $_REQUEST['output_format'] && isset($_REQUEST['mysql_table']))
{
$print_form=0;
ob_start("ob_gzhandler");
header('Content-type: text/comma-separated-values');
header('Content-Disposition: attachment; filename="'.$mysql_host."_".$mysql_database."_".$mysql_table."_".date('YmdHis').'.csv"');
//header('Content-type: text/plain');
_mysqldump_csv($_REQUEST['mysql_table']);
header("Content-Length: ".ob_get_length());
ob_end_flush();
}
}
}
function _mysqldump_csv($table)
{
$delimiter= ",";
if( isset($_REQUEST['csv_delimiter']))
$delimiter= $_REQUEST['csv_delimiter'];
if( 'Tab' == $delimiter)
$delimiter="\t";
$sql="select * from <code>$table</code>;";
$result=mysql_query($sql);
if( $result)
{
$num_rows= mysql_num_rows($result);
$num_fields= mysql_num_fields($result);
$i=0;
while( $i < $num_fields)
{
$meta= mysql_fetch_field($result, $i);
echo($meta->name);
if( $i < $num_fields-1)
echo "$delimiter";
$i++;
}
echo "\n";
if( $num_rows > 0)
{
while( $row= mysql_fetch_row($result))
{
for( $i=0; $i < $num_fields; $i++)
{
echo mysql_real_escape_string($row[$i]);
if( $i < $num_fields-1)
echo "$delimiter";
}
echo "\n";
}
}
}
mysql_free_result($result);
}
function _mysqldump($mysql_database)
{
$sql="show tables;";
$result= mysql_query($sql);
if( $result)
{
while( $row= mysql_fetch_row($result))
{
_mysqldump_table_structure($row[0]);
if( isset($_REQUEST['sql_table_data']))
{
_mysqldump_table_data($row[0]);
}
}
}
else
{
echo "/* no tables in $mysql_database */\n";
}
mysql_free_result($result);
}
function _mysqldump_table_structure($table)
{
echo "/* Table structure for table <code>$table</code> */\n";
if( isset($_REQUEST['sql_drop_table']))
{
echo "DROP TABLE IF EXISTS <code>$table</code>;\n\n";
}
if( isset($_REQUEST['sql_create_table']))
{
$sql="show create table <code>$table</code>; ";
$result=mysql_query($sql);
if( $result)
{
if($row= mysql_fetch_assoc($result))
{
echo $row['Create Table'].";\n\n";
}
}
mysql_free_result($result);
}
}
function _mysqldump_table_data($table)
{
$sql="select * from <code>$table</code>;";
$result=mysql_query($sql);
if( $result)
{
$num_rows= mysql_num_rows($result);
$num_fields= mysql_num_fields($result);
if( $num_rows > 0)
{
echo "/* dumping data for table <code>$table</code> */\n";
$field_type=array();
$i=0;
while( $i < $num_fields)
{
$meta= mysql_fetch_field($result, $i);
array_push($field_type, $meta->type);
$i++;
}
//print_r( $field_type);
echo "insert into <code>$table</code> values\n";
$index=0;
while( $row= mysql_fetch_row($result))
{
echo "(";
for( $i=0; $i < $num_fields; $i++)
{
if( is_null( $row[$i]))
echo "null";
else
{
switch( $field_type[$i])
{
case 'int':
echo $row[$i];
break;
case 'string':
case 'blob' :
default:
echo "'".mysql_real_escape_string($row[$i])."'";
}
}
if( $i < $num_fields-1)
echo ",";
}
echo ")";
if( $index < $num_rows-1)
echo ",";
else
echo ";";
echo "\n";
$index++;
}
}
}
mysql_free_result($result);
echo "\n";
}
function _mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password)
{
global $output_messages;
$link = mysql_connect($mysql_host, $mysql_username, $mysql_password);
if (!$link)
{
array_push($output_messages, 'Could not connect: ' . mysql_error());
}
else
{
array_push ($output_messages,"Connected with MySQL server:$mysql_username@$mysql_host successfully");
$db_selected = mysql_select_db($mysql_database, $link);
if (!$db_selected)
{
array_push ($output_messages,'Can\'t use $mysql_database : ' . mysql_error());
}
else
array_push ($output_messages,"Connected with MySQL database:$mysql_database successfully");
}
}
if( $print_form >0 )
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>mysqldump.php version <?php echo $mysqldump_version; ?></title>
</head>
<body>
<?php
foreach ($output_messages as $message)
{
echo $message."<br />";
}
?>
<form action="" method="post">
MySQL connection parameters:
<table border="0">
<tr>
<td>Host:</td>
<td><input name="mysql_host" value="<?php if(isset($_REQUEST['mysql_host']))echo $_REQUEST['mysql_host']; else echo 'localhost';?>" /></td>
</tr>
<tr>
<td>Database:</td>
<td><input name="mysql_database" value="<?php echo $_REQUEST['mysql_database']; ?>" /></td>
</tr>
<tr>
<td>Username:</td>
<td><input name="mysql_username" value="<?php echo $_REQUEST['mysql_username']; ?>" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="mysql_password" value="<?php echo $_REQUEST['mysql_password']; ?>" /></td>
</tr>
<tr>
<td>Output format: </td>
<td>
<select name="output_format" >
<option value="SQL" <?php if( isset($_REQUEST['output_format']) && 'SQL' == $_REQUEST['output_format']) echo "selected";?> >SQL</option>
<option value="CSV" <?php if( isset($_REQUEST['output_format']) && 'CSV' == $_REQUEST['output_format']) echo "selected";?> >CSV</option>
</select>
</td>
</tr>
</table>
<input type="submit" name="action" value="Test Connection"><br />
<br>Dump options(SQL):
<table border="0">
<tr>
<td>Drop table statement: </td>
<td><input type="checkbox" name="sql_drop_table" <?php if(isset($_REQUEST['action']) && ! isset($_REQUEST['sql_drop_table'])) ; else echo 'checked' ?> /></td>
</tr>
<tr>
<td>Create table statement: </td>
<td><input type="checkbox" name="sql_create_table" <?php if(isset($_REQUEST['action']) && ! isset($_REQUEST['sql_create_table'])) ; else echo 'checked' ?> /></td>
</tr>
<tr>
<td>Table data: </td>
<td><input type="checkbox" name="sql_table_data" <?php if(isset($_REQUEST['action']) && ! isset($_REQUEST['sql_table_data'])) ; else echo 'checked' ?>/></td>
</tr>
</table>
<br>Dump options(CSV):
<table border="0">
<tr>
<td>Delimiter:</td>
<td><select name="csv_delimiter">
<option value="," <?php if( isset($_REQUEST['output_format']) && ',' == $_REQUEST['output_format']) echo "selected";?>>,</option>
<option value="Tab" <?php if( isset($_REQUEST['output_format']) && 'Tab' == $_REQUEST['output_format']) echo "selected";?>>Tab</option>
<option value="|" <?php if( isset($_REQUEST['output_format']) && '|' == $_REQUEST['output_format']) echo "selected";?>>|</option>
</select>
</td>
</tr>
<tr>
<td>Table:</td>
<td><input type="input" name="mysql_table" value="<?php echo $_REQUEST['mysql_table']; ?>" /></td>
</tr>
<tr>
<td>Header: </td>
<td><input type="checkbox" name="csv_header" <?php if(isset($_REQUEST['action']) && ! isset($_REQUEST['csv_header'])) ; else echo 'checked' ?>/></td>
</tr>
</table>
<input type="submit" name="action" value="Export"><br />
</form>
</body>
</html>
<?php
}
?>
January 14, 2009
1 Comment
If your host doesn’t provide you with phpMyAdmin access, or if you simply want a faster lighter version of phpMyAdmin for simple DB admin tasks and running custom SQL queries, then take a look at phpMiniAdmin. Its a single script so its incredibly easy to install – simply upload it.
I would be tempted to beef up the security a little bit, maybe put it into a htaccess password protected folder or something if you can. It has password functionality built in, but this doesn’t seem to be working for me.
more
yatblog.com/2007/03/06/lightweight-alternative-to-phpmyadmin/
php-mini-sql-admin-is-simply-incredible/