0

I have this following question about different behaviour of result set for SELECT command and INSERT,UPDATE,DELETE command.

My Script:

//Part 1:

$query="Insert into student values($roll,'$name')" ;
$result=mysqli_query($con,$query) or die(mysqli_error($con));
if ($result)
{                                       
    $num=mysqli_affected_rows($con);  
    var_dump($num);                     
    echo $num." rows affected.";        
}

//Part 2:
$query1="select * from student";
$result=mysqli_query($con,$query1) or die(mysqli_error($con));
if ($result)
{
    $num1=mysqli_num_rows($result);
    var_dump($num1);
    echo $num1." rows affected.";
}

Following is output for both snippets:

db825aa1cae026df09122b9fe22687f4

My Question is can't we use mysqli_num_rows($result) for getting number of rows in part1 ?

3
Contributors
6
Replies
23
Views
4 Years
Discussion Span
Last Post by Rahul47
1

mysqli_affected_rows() returns the number of rows which were changed as a result of the last query. mysqli_num_rows() returns the number of rows in the array/resource (i.e. the query result) returned by the query.

Even if mysqli_num_rows may work for counting affected rows, it's probably not a good idea to do this, as this functionality may be removed in the future for all we know - the function wasn't necessarily designed for this.

Edited by Assembly Guy

0

Even if mysqli_num_rows may work for counting affected rows, it's probably not a good idea to do this

Then what do you suggest for counting the no of rows returned in case of SELECT query ?

this functionality may be removed in the future for all we know - the function wasn't necessarily designed for this.

So what was it designed for ?

Edited by Rahul47: mistakes

0

Okay, I got it. So conclusion is:

For SELECT statement mysqli_num_rows() works like mysqli_affected_rows() works for INSERT,UPDATE,DELETE.

Right ?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.