Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

PHP/MySQL not returning expected results

Below is some php code. In this example the php code is setting a variable $ukey based on the calling code. The $ukey variable gets set BUT the value is intentionnly incorrect (testing). I'm expecting my logic to generate an error message when mysql $result is not found.

Question - Why doesn't  mysql die with an error?

<?php
    error_reporting(E_ALL);
    include ("config.inc.php");
    $ukey = htmlspecialchars(trim($_GET['ukey']));
                       
    $conn = mysql_connect("localhost", "X", "Y.") or die('Verify DB Failure ' . mysql_error());
    mysql_select_db("test");
                 
    $result = mysql_query("SELECT * FROM itemhdr WHERE itemhdr.ukey = '$ukey'");
               
           if (!$result) {
               die('QUERY ERROR: verifyupload ' . mysql_error());
           }
                       
          $row = mysql_fetch_assoc($result);
          $vcount = $row['verifycount'];
                 
echo "INSIDE PHP " .$ukey;
echo "|";                                              
echo "VCOUNT " .$vcount;                        
                             
    mysql_close($conn);
                       
?>

0
kbios
Asked:
kbios
  • 2
  • 2
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
But it is Not a MySQL error for the key to not exist.  It is just no actual results.  Try this.
<?php
    error_reporting(E_ALL);
    include ("config.inc.php");
    $ukey = htmlspecialchars(trim($_GET['ukey']));
                       
    $conn = mysql_connect("localhost", "X", "Y.") or die('Verify DB Failure ' . mysql_error());
    mysql_select_db("test");
                 
    $result = mysql_query("SELECT * FROM itemhdr WHERE itemhdr.ukey = '$ukey'");
               
           if (!$result) {
               die('QUERY ERROR: verifyupload ' . mysql_error());
           }
          $num_rows = mysql_num_rows($result);
          if ($num_rows > 0) {
          $row = mysql_fetch_assoc($result);
          $vcount = $row['verifycount'];
          } else echo "No results for $ukey.";
echo "INSIDE PHP " .$ukey;
echo "|";                                              
echo "VCOUNT " .$vcount;                        
                             
    mysql_close($conn);
                       
?>

Open in new window

0
 
kbiosAuthor Commented:
Thanks.

I get it. I 'assumed' that since the key didn't exist would return an error; but it makes perfect sense that it returns 0. I appreciate your help.
0
 
kbiosAuthor Commented:
Solution worked great. Thanks.
0
 
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad to help.
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now