dongren7374 2014-11-22 11:27
浏览 96

SQL语句超过了PHP的最大时间,但在phpMyAdmin中立即运行

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?

  • 写回答

1条回答 默认 最新

  • douzi3756 2014-11-22 13:53
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题