Reputation: 623
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?
Upvotes: 0
Views: 175
Reputation: 623
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.
Upvotes: 1