0

Hello i'm trying to get the comments form the releative table 'comments' of my database. I want to display if there are no comments a message like : 'no results', else if there are comments to display the comment box withe username the date and the message of the comments i have tryied this but with no result's :

function getComments($conn) {
$sql = ("SELECT * FROM comments ORDER BY cid DESC LIMIT 5");
$result = $conn->query($sql);
foreach($row as $result) {
    if(!mysql_num_rows($sql))
{
echo 'No results';
}
else
{
echo "<div class='comment-box2'><p>";
        echo $row['users']."<br>";
        echo $row['date']."<br>";
        echo nl2br($row['message']);
    echo "</p>
    </div>";
}
}
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
    is different from this question – djodjfew fefewfwefwe Jan 05 '17 at 13:25
  • 1
    Not really. You're using mysqli/PDO to run the query, then using mysql_* to get the number of rows. The two don't mesh. Switch mysql_num_rows to use the function for the new API you're using. – aynber Jan 05 '17 at 13:28
  • You need to change mysql_num_rows($result) to mysqli_num_rows($conn, $result). mysql_num_rows is the deprecated version and mysqli is the new version which is what you are doing your query on, but not getting the count – Boardy Jan 05 '17 at 13:28
  • and `foreach($row as $result) {` to `foreach($result as $row) {` I'd suggest reading some simple tutorials first – Robert Pounder Jan 05 '17 at 13:29
  • @RobertPounder I didn't notice that, although not sure if the foreach loop can be done like that I think you would need to `foreach($result->fetch_array() as $row)` – Boardy Jan 05 '17 at 13:30
  • @Boardy was aimed at op. and yeh probably, all of my recent projects have been laravel and if they've not I've used phinx, not used mysql/i base functions in a long time lol! – Robert Pounder Jan 05 '17 at 13:34
  • 1
    I'm not so sure this is a duplicate. It looks to me like the OP is a PHP newbie who may not even know that MySQL and MySQLi are different. Also note that he tried iterating over the results before verifying that it wasn't empty. @u_mulder – shalvah Jan 05 '17 at 13:46
  • also confused why this was marked as a dup lol – Robert Pounder Jan 05 '17 at 13:48

1 Answers1

0

I think you didn't order your code properly, along with a number of other mistakes you made, such as mixing mysql and mysqli. (You can see this question for an explanation of the difference between the two.) Try this:

function getComments($conn) 
{ 
  $sql = ("SELECT * FROM comments ORDER BY cid DESC LIMIT 5"); 
  $result = $conn->query($sql);

  //This line should come next;
  //you check the number of rows in the result of the query returned,
  //not in the SQL statement
  if(!($result->num_rows)) { //looks like you're using MySQLi, so you should do things properly
  echo 'No results'; 
  } else {

  //Then iterate over the rows if there were any
  while($row = $result->fetch_assoc()) { //again, using MySQLi
    echo "<div class='comment-box2'><p>"; 
    echo $row['users']."<br>"; //I'm pretty sure this should be singular
    echo $row['date']."<br>"; 
    echo nl2br($row['message']);
    echo "</p> </div>"; 
    } 
  } 
}
Community
  • 1
  • 1
shalvah
  • 881
  • 10
  • 21