-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?????

0

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

-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.

3 Comments

$result will never be empty
Well, I think they can be check this stackoverflow.com/questions/32322359/…
Nope they cannot. It is very important to understand the question you are trying to answer, or a post you are referring to.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.