Improve company productivity with a Business Account.Sign Up

x
?
Solved

php / MySQL problem

Posted on 2013-05-16
17
Medium Priority
?
342 Views
Last Modified: 2013-05-16
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
0
Comment
Question by:Richard Korts
  • 8
  • 4
  • 3
  • +1
17 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39172177
Do you have a copy of the previous version and the changed version?
0
 

Author Comment

by:Richard Korts
ID: 39172205
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
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 39172212
$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.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39172270
You said you changed something and now the query doesn't work.  Did you change the query too?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39172331
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.
http://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.
0
 

Author Comment

by:Richard Korts
ID: 39172379
To xmediaman

I did yours; I get a COMPLETELY blank page.
0
 

Author Comment

by:Richard Korts
ID: 39172393
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.
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 39172402
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

0
 

Author Comment

by:Richard Korts
ID: 39172464
To xmediaman,

It produced exactly the attached (which seems impossible to me).
crazy.jpg
0
 

Author Comment

by:Richard Korts
ID: 39172485
To xmediaman,

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

R
0
 
LVL 22

Accepted Solution

by:
Kim Walker earned 2000 total points
ID: 39172554
OK. That makes sense to me though I'm not sure why the error wasn't displayed. Try this:
$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 "Error = " . mysql_error() . "<br>Query = " . $Qry . "<br>";
      }

Open in new window

0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39172620
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

0
 

Author Comment

by:Richard Korts
ID: 39172881
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
0
 

Author Closing Comment

by:Richard Korts
ID: 39172884
Thanks for sticking with me & all those code changes.
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 39172951
You're welcome. Glad you got it figured out.
0
 

Author Comment

by:Richard Korts
ID: 39172975
It's amazing how when it all dawns on you; just one simple thing & I saw the WHOLE picture.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39173537
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
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

606 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question