Question

I get mysql_num_rows() expects parameter 1 to be resource when I try to bring mysql data to a table. What is the problem with the coding and have used mysql and mysqli correctly? The error is for line $num = mysql_num_rows($result);. I am pretty sure the problem is that $result but I do not know how to fix it.

    <?php

 $connection=mysqli_connect("localhost"/*hostname*/,
                          "username"/*username*/,
                          "password"/*password*/,
                          "dbname"/*database name*/);
$query = "SELECT * FROM table";
$result = mysqli_query($query);
$num = mysqli_num_rows($result);
mysqli_close();

$i=0;
while ($i < $num) {
$f1=mysqli_result($result,$i,"rowa");
$f2=mysqli_result($result,$i,"rowb");
$f3=mysqli_result($result,$i,"rowc");
$f4=mysqli_result($result,$i,"rowd");
$f5=mysqli_result($result,$i,"rowe");        ?>
            <table>
            <tr>
                 <td>a</td>
                 <td>b</td>
                 <td>c</td>
                 <td>d</td>
                 <td>e</td>
            </tr>
            <tr>
                <td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font>
</td>           </tr>

            </table>
            <?php } ?>  

The username, password, database and table name are not the actual names used in the code.

Was it helpful?

Solution

That's an easy fix: look closely at your first line:

mysqli_connect();

The mysqli_* extension is used, as it should be, but you then procede to call all sorts of deprecated mysql_* functions. Just change those calls to the more contemporary, and non-deprecated mysqli_* counterparts.

Other problems include your calling mysqli_close too soon (after you close the connection, you still try to work with the DB results).
And you're not even calling mysqli_close correctly: that function requires you to pass the db connection as an argument:

mysqli_close($connection);

Spend some time reading the manual

On your other problems: the query uses a reserved keyword: table, in MySQL, that should be escaped:

$result = mysqli_query(
    $connection,//pass mysqli connection as first param
    'SELECT * FROM `table`'
);
if (!$result)
    exit('Query failed');//ALWAYS CHECK RETURN VALUES!!
$count = mysqli_num_rows($result);
//and so on

OTHER TIPS

You are mixing mysql and mysqli functions

replace your connection function to mysql_connect()

Or even better, since mysql_ functions are deprecated try to change everything to mysqli or use PDO

This error means your query failed becouse you are mixing mysql_* and mysqli_*

try this:

 $connection=mysqli_connect("localhost"/*hostname*/,
                          "username"/*username*/,
                          "password"/*password*/,
                          "dbname"/*database name*/);
$query = "SELECT * FROM table";
$result = mysqli_query($connection,$query);
$num = mysqli_num_rows($result);

change:

$result = mysqli_query($query);

to:

$result = mysqli_query($connection, $query);

I hope this will fix your error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top