简体   繁体   中英

My working fine PHP scripts aren't working anymore

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM