Page 1 of 1

Is this mysqli code correct & complete-has everything needed

Posted: Tue Oct 07, 2008 11:49 am
by dgwade
Is the following mysqli code correct? and complete? Am I missing something? I ask because my hosting admins tell me that MySQL is good to go and therefore the code must be at fault. They tell me to talk with my developer. But I am the developer (who is rusty in PHP/MySQL since I develop at work using asp.net/MsSQL). The mysqli_affected_rows($link) reference contains the value of 1 after the update BUT the table never gets updated (in that subsequent SELECTS from either code or from PHPadmin using the same userid/password continues to show old values instead of the supposedly updated values). Below is the connection and update code:

Connection code:

Code: Select all

 
$link = mysqli_connect('localhost', 'userid','password','Uno8s');
if (mysqli_connect_errno()) {
    echo "Connect failed: " . mysqli_connect_errno() . " " . mysqli_connect_error() . "<br />";
    exit();
}
 
UPDATE code where $fndChatStream is a string known to contain value as are the other variables known to contain values. And the record containing ID=1 does exist.

Code: Select all

 
$MySQL = "UPDATE chatTbl set chatStream = '" . $fndChatStream . "\n" . $handle . " " . $inDate . " " . $inTime . "\n" . $inMessage . "' WHERE ID = 1";
if ($result = mysqli_query($link, $query)) {
   if (mysqli_affected_rows($link) > 0) {
      $numInserted_rows = 1;
   }
   else if (mysqli_affected_rows($link) == 0)
            $numInserted_rows = 0;
   else $numInserted_rows = -1;
}
else $numInserted_rows = 0;
 
Note again that mysqli_affected_rows($link) as shown below reports 1 record updated.

Code: Select all

 
$numInserted_rows = mysqli_affected_rows($link);
 
The connection seems good since the following query returns results. And the userid/password again is the same as the userid/password used from PHPadmin to originally create the database and to check for changed values (such changed values never being shown but instead the old values continuing to show).

Code: Select all

 
$query = "SELECT c.gameID, c.date, c.time, c.chatStream from chatTbl c WHERE c.gameID = " . $inGameID ;
if ($result = mysqli_query($link, $query)) {
   $numChat_rows = mysqli_num_rows($result); 
}
else $numChat_rows = 0;
if ($numChat_rows > 0) {
      $row                    = mysqli_fetch_assoc($result);
      $fndID                = $row["ID"];
      $fndGameID            = $row["gameID"];
}
 

Re: Is this mysqli code correct & complete-has everything needed

Posted: Tue Oct 07, 2008 12:54 pm
by dgwade
Problem solved! Since I usually work in asp.net/c# I was out of my comfort zone and copied code which is where I got $query. Then I slipped into comfortable pattern of placing sql code in the $MySQL variable instead. So there was a variable disconnect and I was blind to what looked right and of course was not right. When I repaired this variable disconnect (changed $MySQL to $query which I got in the first place from copying documentation examples) the update worked.

Sorry to distract anyone.