0

I'm very new to php and I have this code. I can't tell if the data is being taken from the database.

$sql = mysql_query("SELECT id,question,date,user,numberofcomments FROM questions");
if (!$sql) {
echo 'Could not run query: ' . mysql_error();
exit;
}

$result = mysql_fetch_row($sql);

echo '<table border="1"> 
                  <tr> 
                    <th>Title 1</th> 
                    <th>Title 2</th> 
                  </tr>';   

            while($row = mysql_fetch_assoc($result))  
            {                 
                echo '<tr>';  
                    echo '<td class="leftpart">';  
                        echo '<h3><a href="topic.php?id=' . $row[id] . '">' . $row['question'] . '</a><h3>';  
                    echo '</td>';  
                    echo '<td class="rightpart">';  
                        echo $row['date'];  
                    echo '</td>';  
                echo '</tr>';  
            }  

I'm getting this error.

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, array given in

Can someone explain this error. I commented out the while($row... and there was no error and the data did not show up making me assume the data isn't being taken from my database correctly. I'm not sure exactly how these functions work and the help file is confusing me so I came here.

What did I do wrong and what can I change?

EDIT:

I am connected to the database (I've pulled out information for usernames and passwords already)

Anthony
  • 433
  • 1
  • 5
  • 12

1 Answers1

1

mysql_fetch_assoc() expects to be passed in the resource returned by mysql_query(). Thus, your call should be

while($row = mysql_fetch_assoc($sql))

Instead, what you are passing in is the return value of mysql_fetch_row() which is an array or boolean.

PS - All mysql_* functions are deprecated / being deprecated. You should NOT be using them.

xbonez
  • 40,730
  • 48
  • 157
  • 236