Solved

php / MySQL problem

Posted on 2013-05-16
17
322 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 82

Expert Comment

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

Author Comment

by:Richard Korts
Comment Utility
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 21

Expert Comment

by:Kim Walker
Comment Utility
$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
 
LVL 82

Expert Comment

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

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
To xmediaman

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

Author Comment

by:Richard Korts
Comment Utility
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 21

Expert Comment

by:Kim Walker
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Richard Korts
Comment Utility
To xmediaman,

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

Author Comment

by:Richard Korts
Comment Utility
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 21

Accepted Solution

by:
Kim Walker earned 500 total points
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for sticking with me & all those code changes.
0
 
LVL 21

Expert Comment

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

Author Comment

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

Expert Comment

by:Ray Paseur
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now