Member Avatar for Rahul47

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 ?

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.

Member Avatar for Rahul47

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 ?

Member Avatar for Rahul47

Okay, I got it. So conclusion is:

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

Right ?

Right.

Member Avatar for Rahul47

Thanx all . . .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.