-1

I'm having an issue with the warning "mysqli_num_rows() expects parameter 1 to be mysqli_result, object given", I'm trying to display content from a database and it seems to be working fine except for this warning. I've tried a lot of methods to fix this but it does not seem to be working. The warning tells me that the line with the problem is "if(mysqli_num_rows($result)>0) {", hope you can help me shed some light on this. Thanks.

<?php
$sql = "SELECT `id`, `name`, `type`, `size`, `url`, `owner`, `created` FROM `files`";
$result = $db->query($sql);
if($result) {
    if(mysqli_num_rows($result)>0) {
        echo "You have not uploaded any files.";
    } else {
        echo "  <table width='100%'>";
        echo "
    <tr>
      <td>Name</td>
      <td>Type</td>
      <td>Size</td>
      <td>URL</td>
      <td>Owner</td>
      <td>Created</td>
      <td></td>
    </tr>";
        while($row = $result->fetch(PDO::FETCH_ASSOC)) {
            echo "
    <tr>
      <td>{$row['name']}</td>
      <td>{$row['type']}</td>
      <td>{$row['size']}</td>
      <td>{$row['url']}</td>
      <td>{$row['owner']}</td>
      <td>{$row['created']}</td>
      <td><a href='file?id={$row['id']}'>Download</a></td>
    </tr>";
        }
        echo "
  </table>
";
    }
}
?>
deceze
  • 510,633
  • 85
  • 743
  • 889
joedm
  • 27
  • 7

3 Answers3

2

You cannot use mysqli with PDO

if you want to count the rows in PDO do this:

if($result->rowCount() === 0)

PDOStatement::rowCount

meda
  • 45,103
  • 14
  • 92
  • 122
  • it will now work that way, because $result is not the PDO object? i guess. not sure. – jay temp Oct 24 '14 at 02:25
  • because pdo and myqli are different extensions you cannot mix them.To use any mysqli function you need a mysqli connection – meda Oct 24 '14 at 02:26
  • This fixes the error however it now displays the "You have not uploaded any files." echo rather than the contents of the else statement. – joedm Oct 24 '14 at 02:31
  • 1
    @joedm just reverse the condition. either just put `<=0` of no rows found, or put the fetch inside the if block – Kevin Oct 24 '14 at 02:33
  • @joedm I got this mistake from you , you should compare with equal see update – meda Oct 24 '14 at 02:33
  • Perfect, thanks for the help, saved me from more hours of searching! – joedm Oct 24 '14 at 02:35
1

Looks like you are mixing object and procedural style of mysqli. Try this:

if ($result->num_rows > 0) 
JonasB
  • 458
  • 3
  • 9
0

you can also use fetchColumn from select statement

if ($result->fetchColumn() > 0)
jay temp
  • 1,207
  • 12
  • 11
  • Seems to have got rid of the error, however it is no longer displaying the table. It is displaying the "You have not uploaded any files." echo rather than the else statement. – joedm Oct 24 '14 at 02:29