In my database SQL, I have messages flowing between user a and b. I want to echo out all the messages they have had. But I am getting the error "array to string conversion" at the moment. I tried to use implode
but that didn't work too well. How can I receive every row of messages flowing between both users?
$query = "SELECT message "
. "FROM chat "
. "WHERE (user_name='a' or user_name='b') "
. "AND (user_to='b' or user_to='a')";
$sql = mysql_query($query);
$retrievedata = mysql_fetch_assoc($sql);
echo $retrievedata;
mysql_fetch_assoc
fetches a single row, as an associative array (column name to value). In order to print all the messages, you'd have to go over the entire result set:
while ($retrievedata = mysql_fetch_assoc($sql)) {
echo $retrievedata['message'];
}
Mandatory comment:mysql*
has been deprecated for several years now. Please use mysqli*
or PDO instead.
Use var_dump($retrievedata)
to print values of an array