Jump to content

mySQLI conversion issues


azparties

Recommended Posts

First issue localhost should be in quotes

$con=mysqli_connect(localhost,$username,$password, $database);

Second issue the the variables $quantity and $id do not appear to be defined any where. Where are these variables defined?

 

Third issue

$mysqli->query("UPDATE sale SET  `quantity`='$ud_quantity' WHERE `index`='$id'";

The $mysqli object doesn't exist. You're using the procedural function to connect to mysq there for you should use the procedural mysqli_query function. Also you where missing the closing parenthesis at the end of the line too. The above line should be

mysqli_query($con, "UPDATE sale SET  `quantity`='$ud_quantity' WHERE `index`='$id'");

Fourth issue

printf("Affected rows (UPDATE): %d\n", $mysqli->affected_rows or die(mysql_error());

The or die() clause is in the wrong place, it should be after the mysqli_query() call. mysql_error should be mysqli_error()

 

 

 

As a side note the switch statement could be rewritten as a simple if/else statement

if($quantity >= 0 && $quantity <= 30)
    $ud_quantity = $quantity + 1;
else
    $ud_quantity = 'ERR';
 You're using the procedural function to connect to mysq there for you should use the procedural mysqli_query function. 
 
You can mix-and-match object methods and procedural functions. This runs fine
 
$db = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE); // using defined constants
$res = $db->query("SELECT COUNT(*) FROM votes") or die($db->error);
$row = mysqli_fetch_row($res);
echo $res->num_rows;   // --> 1
echo $row[0];   // --> 182685

edit: You use the object method or pass the object to the procedural function

The procedural functions are just small wrappers basically, eg:

function mysqli_query($obj, $query){
   return $obj->query($query);
}

 

I suspected that that were the case, which would make the object versions minimally more efficient than the procedural ones.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.