pagegen Posted September 5, 2008 Share Posted September 5, 2008 The code works no problem but its soo slow.. any suggestions? session_start(); $Busername = session_id(); $item = $_GET["item"]; $action = '2'; // Connect to database $dbc = mysql_connect ('localhost','username','pasword') OR die('Could not connect to mySql : ' . mysql_error()); mysql_select_db (databasename) OR die('Could not connect to mySql : ' . mysql_error()); /* Update item quantity in shopping cart */ $yesterday = date('Y-m-d H:i:s', mktime(0,0,0, date('m'), date('d') - 1, date('Y'))); $sql = "DELETE FROM TblBasket WHERE CartDate < '$yesterday'"; mysql_query($sql) or die(mysql_error()); // SQL query to select the rows $query = "SELECT * FROM TblBasket"; $result = mysql_query ($query); while($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $itemIdInDatabase = $row[itemId]; $UsernameOfUser = $row[userEmail]; if ($item == $itemIdInDatabase && $Busername == $UsernameOfUser){ $qunt = $row[qun] + 1; $query2 = "UPDATE TblBasket SET qun = $qunt WHERE itemId = '$item' && userEmail = '$Busername'"; $result2 = mysql_query($query2) or die(mysql_error()); $action = '1'; } } If ($action == '2'){ $query2 = "INSERT INTO TblBasket (id, userEmail, itemId, qun, CartDate) VALUES ('NULL','$Busername','$item', '1', NOW())"; $result2 = mysql_query ($query2); } mysql_close(); $ref = $_SERVER['HTTP_REFERER']; header ("Location: $ref"); Quote Link to comment https://forums.phpfreaks.com/topic/122852-isit-my-code-or-isit-the-server/ Share on other sites More sharing options...
JonnoTheDev Posted September 5, 2008 Share Posted September 5, 2008 Make sure that your queries are optimised using EXPLAIN http://dev.mysql.com/doc/refman/5.0/en/explain.html Quote Link to comment https://forums.phpfreaks.com/topic/122852-isit-my-code-or-isit-the-server/#findComment-634481 Share on other sites More sharing options...
PFMaBiSmAd Posted September 5, 2008 Share Posted September 5, 2008 Your code is looping through all the rows in the TblBasket table, testing each one against $item and $Busername. That is definitely not how to use a database. You should only operate on the rows that match what you are interested in. All of these lines of code - // SQL query to select the rows $query = "SELECT * FROM TblBasket"; $result = mysql_query ($query); while($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $itemIdInDatabase = $row[itemId]; $UsernameOfUser = $row[userEmail]; if ($item == $itemIdInDatabase && $Busername == $UsernameOfUser){ $qunt = $row[qun] + 1; $query2 = "UPDATE TblBasket SET qun = $qunt WHERE itemId = '$item' && userEmail = '$Busername'"; $result2 = mysql_query($query2) or die(mysql_error()); $action = '1'; } } Can be replaced with this - // SQL query to select the rows $query2 = "UPDATE TblBasket SET qun = qun + 1 WHERE itemId = '$item' && userEmail = '$Busername'"; $result2 = mysql_query($query2) or die(mysql_error()); if(mysql_affected_rows() > 0) { $action = '1'; } Quote Link to comment https://forums.phpfreaks.com/topic/122852-isit-my-code-or-isit-the-server/#findComment-634555 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.