i have query in my php code
$dbc = mysqli_connect('localhost', 'root','', 'delivery')
or die('Error connecting to MySQL server.');
$count = "SELECT MAX(id_pelanggan) FROM pelanggan";
$result = mysqli_query($dbc, $count)
or die('Error select query');
if (empty($result)) {
$id_pelanggan = 1;
}
else {
$id_pelanggan = $result + 1;
}
and the result was
Object of class mysqli_result could not be converted to int in C:\xampp\htdocs\delivery\addcustomer.php
whereas in mysql id_pelanggan datatype is int. can anyone help me to make it works?
$id_pelanggan
variable instead of the value from your query.*_fetch array()
of your queryif()
condition because your $result
won't be empty no matter what happens. The number of result maybe.Put this and replace your if else
condition:
if(mysqli_num_rows($result) == 0){ /* IF FOUND 0 RESULT */
$id_pelanggan = 1;
}
else {
while($row = mysqli_fetch_array($result)){
$maxid = $row["id_pelanggan"];
}
$id_pelanggan = $maxid + 1;
} /* END OF ELSE */
Simple fetch the result and increment it. You can do this by -
$count = "SELECT MAX(id_pelanggan) as max_id_pelanggan FROM pelanggan";
$result = mysqli_query($dbc, $count)
or die('Error select query');
if (empty($result)) {
$id_pelanggan = 1;
}
else {
$data = mysqli_fetch_assoc($result);
$id_pelanggan = $data['max_id_pelanggan'] + 1;
}