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

The Okay Man
  • 133
  • 1
  • 7
  • 2
    possible duplicate of [mysql\_numrows() error in MySQL Query Web Page Display](http://stackoverflow.com/questions/10477159/mysql-numrows-error-in-mysql-query-web-page-display) – Realitätsverlust May 12 '14 at 11:36
  • By ensuring that you have a valid database connection, and that your query statement is correct – Mark Baker May 12 '14 at 11:36
  • 2
    Sidenote : table is a **reserved word**. Surround it with backticks. – Shankar Narayana Damodaran May 12 '14 at 11:38
  • I didn't go through all this trouble to help you in [**your other question**](http://stackoverflow.com/questions/23604597/why-is-my-mysql-query-returning-no-result) to post an answer for you to come back and post another. You basically posted the same question repeatdly. – Funk Forty Niner May 12 '14 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? – Funk Forty Niner May 12 '14 at 11:54
  • @Fred-ii-: I feel your pain: OP changed the entire code from `mysql_*` calls to `mysqli_*` the moment I posted my answer, and the first comments pointing that mistake out appeared. The question then shifted to a RTFM/`mysqli_` is not a 1-on-1 `mysql_*` translation – Elias Van Ootegem May 12 '14 at 12:20
  • @EliasVanOotegem I feel the OP will be back asking another question, because of the `mysqli_result` function, which needs to be a native driver installed. I wasn't even able to run OP's code while fixing most or all of it. – Funk Forty Niner May 12 '14 at 12:22

4 Answers4

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
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • `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. – The Okay Man May 12 '14 at 11:44
  • @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) – Elias Van Ootegem May 12 '14 at 11:47
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

gbestard
  • 1,177
  • 13
  • 29
  • 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! – Elias Van Ootegem May 12 '14 at 11:37
  • @EliasVanOotegem I was editing my answer!! – gbestard May 12 '14 at 11:38
  • 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 – Elias Van Ootegem May 12 '14 at 11:41
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);
Dinesh
  • 4,066
  • 5
  • 21
  • 35
0

change:

$result = mysqli_query($query);

to:

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

I hope this will fix your error.

SuperDJ
  • 7,488
  • 11
  • 40
  • 74