Forum Moderators: coopster

Message Too Old, No Replies

PHP code for MySQL

can this be optimized?

         

smallcompany

2:06 am on Nov 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi,

I wonder if the code below is the best approach you would think of. All what is supposed to do is to write the variable into MySQL database. It checks if the variable is already there, and if it is, it increases the count, if it's not, it writes it as a new entry. Right now, I have almost 230,000 records (lines) in the database.
Can this be optimized or this is it?

Thanks

<?php

define ("CONN_HOST", "***");
define ("CONN_USER", "***");
define ("CONN_PASS", "***");
define ("CONN_DB", "***");

$var2 = strtolower($var2);
$tableName = "***";

$mysql_link = mysql_connect(CONN_HOST, CONN_USER, CONN_PASS);
mysql_select_db(CONN_DB, $mysql_link);

$sql = "select * from $tableName where query='$var2';";
$result = mysql_query($sql, $mysql_link) or die('Query failed:[$sql]<br>Error is: ' . mysql_error($mysql_link));

if ($result && mysql_num_rows($result) > 0)
{
$row = mysql_fetch_assoc($result);
$row['count']++;
$sql ="update $tableName set count={$row['count']} where query='$var2'";
$result = mysql_query($sql, $mysql_link) or die('Query failed:[$sql]<br>Error is: ' . mysql_error($mysql_link));
}
else
{
$sql ="insert into $tableName set query='$var2', count='1';";
$result = mysql_query($sql, $mysql_link) or die('Query failed:[$sql]<br>Error is: ' . mysql_error($mysql_link));
}
?>

Matthew1980

10:02 am on Nov 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there smallcompany,

I have *optimised* it as if it had written this:-

<?php

define ("CONN_HOST", "***");
define ("CONN_USER", "***");
define ("CONN_PASS", "***");
define ("CONN_DB", "***");

$var2 = strtolower($var2);
$tableName = "***";

$mysql_link = mysql_connect(CONN_HOST, CONN_USER, CONN_PASS) or die(mysql_error());
mysql_select_db(CONN_DB, $mysql_link) or die(mysql_error());

$sql = "SELECT * FROM `".$tableName."` WHERE `query` = '".$var2."' ";
$result = mysql_query($sql) or die('Query failed:[$sql]<br>Error is:'.mysql_error());

if ($result && mysql_num_rows($result) > 0){
$row = mysql_fetch_assoc($result);
$row['count']++;
$sql = "UPDATE `".$tableName."` SET `count` = '".$row['count']."' WHERE `query` = '".$var2."' ";
$result = mysql_query($sql) or die('Query failed:[$sql]<br>Error is: ' . mysql_error());
}
else{
$sql = "INSERT INTO `".$tableName."` SET `query` = '".$var2."', count = 1 ";
$result = mysql_query($sql) or die('Query failed:[$sql]<br>Error is: ' . mysql_error());
}
?>


I have just made the statments more sql friendly, and made it so the mysql_query() inherits the connection handle, which is a better method to have, other than that it seems fine. Also removed the quotes from around the count in the last query, numerical values don't really need to be quoted, if they are they are treated as strings - at least this is how I understand it to be.

Cheers,
MRb