1

I'm having trouble getting any information to display from this query. Anyone know where I'm going wrong?

Thank you!

 $query =   "SELECT * ".
            "FROM comments, users ".
            "WHERE comments.user_id = users.user_id ".
            "ORDER BY comments.date DESC ".
            "LIMIT 10";

$result = mysql_query($query) or die(mysql_error());

   while ($row = mysql_fetch_array($result)) {


  echo $row['users.user_id'];
  echo $row['comments.comment'];


 }
1
  • 2
    Turn error_reporting to E_ALL and display_errors to On (that only on dev box) and you will see your problem. Commented Oct 26, 2009 at 20:39

5 Answers 5

4

use mysql_fetch_assoc() instead of mysql_fetch_array(). In your loop use the column name as the array key:

while ($row = mysql_fetch_assoc($result)) {

  echo $row['column_name1'];
  echo $row['column_name1'];

}

In your query try to be more specific on the select statement, try not to use *.

Sign up to request clarification or add additional context in comments.

Comments

2

You're probably getting the error because you are sorting (ORDER BY) on a field that does not exist in your query.

It would be best practice to not use the "SELECT *" querying. If all you need are specific values, specify them. This also helps when retrieving the data...

$query =   "SELECT users.user_id, comments.comment, comments.date ".
                        "FROM comments, users ".
                        "WHERE comments.user_id = users.user_id ".
                        "ORDER BY comments.date DESC ".
                        "LIMIT 10";

$result = mysql_query($query) or die(mysql_error());

   while ($row = mysql_fetch_array($result)) {


  echo $row['user_id'];
  echo $row['comment'];
  echo $row['date'];


 }

Comments

0

It is good practice to specify column names in a query rather than using * - on some DBs there is a performance impact and on all it prevents any unexpected behaviour cropping up from table changes.

In the example I think the issue is arsing from the array keys you are using - you don't need to include the table name in them, just the column name:

  echo $row['user_id']; 
  echo $row['comment'];

Comments

0

The better practice is to write only fields what you need in your sql query like this:

$query =   "SELECT u.user_id uid, c.comment comment ".
                    "FROM comments c, users u ".
                    "WHERE comments.user_id = users.user_id ".
                    "ORDER BY comments.date DESC ".
                    "LIMIT 10";

Using so type of queries you reduce the time of executing your query and transmitting data from database server to your php script. After this modification your cycle transformed to:

while ($row = mysql_fetch_array($result)) {
 echo $row['uid'], $row['comment'];

}

Comments

0

I use PDO method but this can work for you too:

<?php
  $sql = "SELECT * FROM comments as c INNER JOIN users as u ON  c.user_id=u.user_id WHERE u.user_id=:user_id ORDER BY c.date DESC LIMIT 10";
  

  //prepare the connection to database
  $prep = $conn->prepare($sql);

  //change the parameters for user_id on WHERE condition you can put anything you want but i will put the user session id
  $prep->bindParam(":user_id", $_SESSION['user_id']);
  $prep->execute();

 //ill use fetchAll function because it can be more than 1 comment
 $datas = $prep->fetchAll();

foreach($datas as $data){
  echo $data['user_id'];
  echo $data['comment'];
  echo $data['date'];
}

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.