-1

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.

7
  • 2
    possible duplicate of mysql_numrows() error in MySQL Query Web Page Display Commented May 12, 2014 at 11:36
  • By ensuring that you have a valid database connection, and that your query statement is correct Commented May 12, 2014 at 11:36
  • 2
    Sidenote : table is a reserved word. Surround it with backticks. Commented May 12, 2014 at 11:38
  • I didn't go through all this trouble to help you in your other question to post an answer for you to come back and post another. You basically posted the same question repeatdly. Commented May 12, 2014 at 11:47
  • "The error is for line $num = mysql_num_rows($result);" it's not in your body of code. You have $num = mysqli_num_rows($result); WTF? Commented May 12, 2014 at 11:54

4 Answers 4

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

mysqli_result is this correct. Thanks for the link to the manual. How come the websites I have looked at don't link to this anyway.
@TheOkayMan: Sure I know how that can be fixed. I linked to the manual of mysqli_*, just check out the documentation: the first argument you pass to that function is a resource (the return value of mysqli_query is a mysqli_result object, which is what you need to pass)
0

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

3 Comments

Horrible advice! The use of mysqli_connect is the only thing that should stay as is. Don't replace the good code with the bad, deprecated version!
@EliasVanOotegem I was editing my answer!!
If you were editing it when I posted the comment, it stands to reason that I couldn't see that. You don't have to shout. Besides: you're still suggesting to change mysqli_connect. Sure, you say it's better not to, but don't tell people they can still use mysql_* functions, because that extension must die
0

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);

Comments

0

change:

$result = mysqli_query($query);

to:

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

I hope this will fix your error.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.