Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

php / MySQL problem

We have a php application that manages database; it's been working for years.

Today I revised a program because it didn't account for a numeric field being larger than 2 digits (the program, NOT the database).

Now an update query DOES NOT work.

Here is the query & result:

qry = UPDATE schedule set request = '#76 has a cut zone line next to the first column in front of house/ bill to customer/ contact Bob Moore at #76 Justin with Raintree/As look at standing water by N gate by N pool. Between #10 and #11 by flower bed very wet. contact Charlie Freeman 634-0487', slots = 2 where ListID = 'C70000-988055540' and sn = 130
res=

these were produced by the following php statements:

$Qry = "UPDATE schedule set request = '$desc', slots = $slot where ListID = '$listid' and sn = $serno";
      $res = mysql_query ($Qry, $Link);
      if ($listid == "C70000-988055540") {
            echo "qry = " . $Qry . "<br>";
            echo "res=" . $res . "<br>";
      }      
Notice that the $res is null. The query does NOT update the database.

If I copy the query & use phpmysql to manually insert the update statement, it properly updates the table.

The structure of the table is attached,

What is wrong?
schedule-str.jpg
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Do you have a copy of the previous version and the changed version?
Avatar of Richard Korts

ASKER

Dave,

I do but it's (of course) somewhat different.

Do you want me to post BOTH versions (full php program)?

The olde version uses depreciated php functions that some people yell at me about.

R
$res is the result of your query and is an object which cannot be echoed. It is the result of an UPDATE query and therefore contains no data. Try:
$Qry = "UPDATE schedule set request = '$desc', slots = $slot where ListID = '$listid' and sn = $serno";
      if ($res = mysql_query ($Qry, $Link) ) {
            if ($listid == "C70000-988055540") {
                  echo "qry = " . $Qry . "<br>";
                  echo "res=" . mysql_affected_rows($res) . "<br>";
            }
      } else {
            echo mysql_error($res)."<br>";
      }

Open in new window

This will echo the number of rows affected by the UPDATE query if successful or the error that is generated if not successful.
You said you changed something and now the query doesn't work.  Did you change the query too?
Please see this article for examples of how to run MySQL queries and test for success or failure.  MySQL_Query() is not a black box -- it return a value that must be tested for success.  It can and will fail, sometimes for reasons that are not in your control, and it fails silently with respect to PHP.  Your script must use PHP function calls to test for success or failure of the queries.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

You cannot echo a resource, but you can use var_dump() to verify that your earlier call returned a resource.
To xmediaman

I did yours; I get a COMPLETELY blank page.
To DaveBaldwin

I changed the code that leads up to building the php variables included in the query. So the actual query is different; in particular the value of $slot.
If the query completed successfully and the listid was NOT "C70000-988055540", there would be nothing to echo. Let's try another variation that should echo something regardless of the outcome:
$Qry = "UPDATE schedule set request = '$desc', slots = $slot where ListID = '$listid' and sn = $serno";
      if ($res = mysql_query ($Qry, $Link) ) {
            if ($listid == "C70000-988055540") {
                  echo "qry = " . $Qry . "<br>";
                  echo "res=" . mysql_affected_rows($res) . "<br>";
            } else {
                  echo $listid . "<br>";
            }
      } else {
            echo mysql_error($res) . "<br>$Qry = " . $Qry . "<br>";
      }

Open in new window

To xmediaman,

It produced exactly the attached (which seems impossible to me).
crazy.jpg
To xmediaman,

FYI, it DID NOT update the database record on either of the last two tries. I looked at it in phpMyadmin.

R
ASKER CERTIFIED SOLUTION
Avatar of Kim Walker
Kim Walker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why don't you try a SELECT to see if it returns what is supposed to be there?
$Qry = "SELECT * from schedule where ListID = '$listid' and sn = $serno";
$res = mysql_query ($Qry, $Link);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$res) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

while ($row = mysql_fetch_assoc($res)) {
    echo $row['ListID'];
    echo $row['cust'];
    echo $row['slots'];
    echo $row['sn'];
}

Open in new window

To: xmediaman

See following. This INSTANTLY solves the problem for me; I realized I was using the wrong version of the program hooked up to the OLD database. Ray Paseur & Dave Baldwin right too.

Error = Unknown MySQL server host 'db901.perfora.net' (1)
Query = UPDATE schedule set request = '#76 has a cut zone line next to the first column in front of house/ bill to customer/ contact Bob Moore at #76 Justin with Raintree/As look at standing water by N gate by N pool. Between #10 and #11 by flower bed very wet. contact Charlie Freeman 634-0487', slots = 2 where ListID = 'C70000-988055540' and sn = 130
Thanks for sticking with me & all those code changes.
You're welcome. Glad you got it figured out.
It's amazing how when it all dawns on you; just one simple thing & I saw the WHOLE picture.
If you read the article and take some time to understand the examples, you can save yourself a lot of work and trouble in the future.  If you read the article and still have some questions, please be sure to post the questions at EE.  Best of luck with the project, ~Ray