开发者

My working fine PHP scripts aren't working anymore

开发者 https://www.devze.com 2023-01-31 20:05 出处:网络
I\'ve been using this for a long time and now it stopped working. I just don\'t get why PHP changes on you. Someone help.

I've been using this for a long time and now it stopped working. I just don't get why PHP changes on you. Someone help.

The problem is with these two lines:

$res = mysql_query("SELECT num FROM ywsite WHERE item='sitehits'", $db);
$num = mysql_result($res,0,"num");

Here's the rest of the script:

<?php
    $res = mysql_query("SELECT num FROM ywsite WHERE item='sitehits'", $db);
    $num = mysql_result($res,0,"num");
    $equals = $num + 1;
    mysql_query("UPDATE ywsite开发者_StackOverflow SET num='$equals' WHERE item='sitehits'");
?>


That seems like a rather complicated way to increment a number. Just do this instead:

UPDATE ywsite SET num = num + 1 WHERE item = 'sitehits'

If it fails use mysql_error to see the error. The most likely cause of error is an incorrect table or column name.


"Stopped working" is not a problem description. Nothing just changes on you. If code you wrote stops working, it means something external you're relying on has changed, such as the version of the PHP interpreter you're running it on, or, say, a MySQL server you're connecting to.

  1. Check that MySQL is running and you can connect to it.

  2. Ensure you're actually connecting to it in your code. You say "here's the rest of the script", yet your script doesn't include any code to connect to a database before sending queries to it. If that's all the code there is, there used to be more and you removed it.

  3. Check that your SELECT query returned a result

    if (!$res) die("Error in SELECT query: " . mysql_error());
    
  4. Check that your UPDATE query ran the same way, by checking the return value of mysql_query

  5. Finally, you don't need to select the current value to add to that value. You can issue just one UPDATE query.

    mysql_query("UPDATE ywsite SET num = num + 1 WHERE item = 'sitehits'") or die("Error updating: " . mysql_error());
    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号