Jump to content

Isit my code or isit the server?


pagegen

Recommended Posts

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");

Link to comment
https://forums.phpfreaks.com/topic/122852-isit-my-code-or-isit-the-server/
Share on other sites

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';
            }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.