0

I am working on an e-commerce page. I have written a piece of code to get a list of products from my database based on which shopping cart they are in. There are three tables, one for the shopping carts, one for the items in each cart and one listing the details of each product. The session id is stored in the table listing the carts.

I have written the following code in one part of a php file:

$sessionId = session_id();
$selStr = "SELECT cart_items.quantity, products.price, products.discount 
           FROM cart_items JOIN carts ON carts.id = cart_items.carts_id
           JOIN products ON cart_items.products_id = products.id
           WHERE session_id='".$sessionId."';";

$result = mysql_query($selStr);

When I get this page in the browser, the request times out on the server and it returns an error:

Fatal error: Maximum execution time of 30 seconds exceeded

I ran this SQL statement using phpMyAdmin and the code runs fine. In fact, it returns immediately. Also, if I break up the SQL statement and retrieve info from the tables one at a time with seperate calls to MySQL, then no timeout occurs. I am writing this SQL code to eliminate extraneous amounts of code in my files.

Is there a reason why the call to the MySQL database takes longer in the PHP code only?

EDIT: To clarify the question, I mean to ask why doesn't the MySQL call return immediately in my php code? Is there something stopping it from returning at all? A security issue maybe?

radiobrain77
  • 613
  • 1
  • 6
  • 19
  • The call does not take longer. In php.ini file the max_execution_time must be set 30 seconds. Please check the value using echo phpinfo(); if its really 30 seconds then increase it. – Naved Munshi Nov 22 '14 at 11:32
  • The call DEFINITELY takes longer. It is returned immediately in the phpMyAdmin page, yet through my website the call exceeds the 30 second limit. – radiobrain77 Nov 22 '14 at 11:33
  • you have to change **max_execution_time**. which is on php.ini file..and this question no need to recreate.it same as[link]( http://stackoverflow.com/questions/1263680/maximum-execution-time-in-phpmyadmin) – Rajib Ghosh Nov 22 '14 at 11:38
  • Also, I've increased the max_execution_time and it hasn't made any difference. The webpage still returns a time out error message. – radiobrain77 Nov 22 '14 at 11:38
  • 1
    It has nothing to do with the max_execution_time and increasing it is not the solution as serving a request in even 30 seconds itself is not acceptable! – Amir Rahimi Farahani Nov 22 '14 at 11:40
  • Yes. Furthermore, it shouldn't really make much difference, because the phpMyAdmin page is written in PHP too, and running off the same server with the same php.ini file on it. – radiobrain77 Nov 22 '14 at 11:41
  • where are you change the max_execution_time ???apache/php.ini or php/php.ini?????plz recheck... – Rajib Ghosh Nov 22 '14 at 11:42
  • It is in /opt/lampp/etc/php.ini which I gather is the apache (XAMPP) ini file. Not sure where the second one you mention is. – radiobrain77 Nov 22 '14 at 11:43
  • are you restart our apache after php.ini change??.would you like to restart the apache server?after that it should be ok – Rajib Ghosh Nov 22 '14 at 11:45
  • Try with the `mysqli` driver. – Amir Rahimi Farahani Nov 22 '14 at 11:50
  • are you restart our apache after php.ini change???? – Rajib Ghosh Nov 22 '14 at 11:51
  • i think driver change is not your solution – Rajib Ghosh Nov 22 '14 at 11:52
  • check $sessionId not to be NULL. then you are selecting much more records And please tell me what framework or CMS you are using – MOHA Nov 22 '14 at 11:58
  • Are you certain it's the query that's taking this long? If you are not using output buffering, put some echoes on the way and see where the execution stops. If you are already done with this, the only possible explanation is that `$sessionId` becomes something evil and causes an error in MySQL, which in turn bugs something after the query PHP statements. There is _no way_ this should take more than 30 seconds unless you are dealing with trillions of rows on a slower server. – John Weisz Nov 22 '14 at 12:08
  • 1
    The session id is fine. When I put it into phpMyAdmin I gave it a value that is echoed to the webpage. So they are both getting the same fully formed SQL string. – radiobrain77 Nov 22 '14 at 12:24
  • Then I can only think of some communications issue between the database and the webserver. Are you using the correct encoding? – John Weisz Nov 22 '14 at 12:53
  • Also, if this is solely for keeping the shopping cart intact between page views, I recommend using session variables instead of database references. Don't forget that you can store entire arrays in them. If you want to make it more persistent, introduce cookies into your system. – John Weisz Nov 22 '14 at 12:56
  • Correct me if I'm wrong, but if the encoding is wrong then surely all the table column calls in other SQL statements would return errors as well. – radiobrain77 Nov 22 '14 at 13:16
  • would you like to check ->"SELECT cart_items.quantity, products.price, products.discount FROM cart_items JOIN carts ON carts.id = cart_items.carts_id JOIN products ON cart_items.products_id = products.id WHERE session_id='".$sessionId."' limit 0,5;"; if you get data then your problem server side – Rajib Ghosh Nov 22 '14 at 13:21
  • Are you sure the two queries are connected to the same database on the same server? – Gordon Linoff Nov 22 '14 at 13:32
  • 100% sure. There is actually only one database in the system at present. Furthermore, altering it in the webpage reflects changes through myPhpAdmin, and vice versa. – radiobrain77 Nov 22 '14 at 13:34

1 Answers1

1

I figured it out myself.

The code listed above is running fine, however there's an odd kink in the expected output. Basically, I left out a call to get the next row in the result set from the database and so I fell for the oldest trick in the book, namely, an infinite loop.

I was tripped up by the fact I had an echo line after the call to the database to get $result (in the code I posted in the question). I was expecting this line to appear. Unfortunately, it was outputting the line off the page in the browser, so I got tricked into thinking that the echo line after the mysql_query call was not being reached.

Sigh. Thanks for anyone trying to help. Muchos gracias.

radiobrain77
  • 613
  • 1
  • 6
  • 19