-2

I write this script in roder to verify if the sql empty or not:

if (isset($_GET['edit'])){
 $id = $_GET['edit'];
$update = true;
$result = $mysqli->query("SELECT * FROM task WHERE id_task=$id") or die($mysqli->error());
$count = count($result);
if($count == 1){
   $row = $result->fetch_array();
   $nom_task = $row['name_task'];
   $id_operation = $row['id_operation'];
   $nom_operation = $row['name_operation'];
}

any suggetions to resove this please? the problem is in $count and $result?????

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Sh5bark
  • 3
  • 1

2 Answers2

1

mysqli::query returns a mysqli_result object (see https://www.php.net/manual/en/mysqli.query.php) That object is not countable (meaning it cannot be used as a parameter in the count function)

If you want to know the number of rows returned by your SQL request, use $num_rows property from that mysqli_result object :

$result = $mysqli->query('SELECT ...');
$count = $result->num_rows;

Also as a (very important) side note, your code is vulnerable to SQL Injection, please see : https://stackoverflow.com/a/601524

docl
  • 89
  • 5
-2

Hey try this code snippet:

if (isset($_GET['edit'])){
 $id = $_GET['edit'];
$update = true;
$result = $mysqli->query("SELECT * FROM task WHERE id_task=$id") or die($mysqli->error());

if(!empty($result) && count($result) == 1){
   $row = $result->fetch_array();
   $nom_task = $row['name_task'];
   $id_operation = $row['id_operation'];
   $nom_operation = $row['name_operation'];
}

Simply check the array if it's empty or not before count check.