Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 482
  • Last Modified:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

Setting up a basic Private message system in my site and I'm getting this error.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result....

Here is the code it is referring to...

if (isset($_POST['delete'])) {
$id = $_POST['id'];
$user = $_SESSION['user'];
$sql = mysql_query("UPDATE messages SET deleted = 'yes' WHERE id = '$id' AND to_user = '$user'")or die(mysql_error());
echo "Your message has been succesfully deleted.";
}
while($row = mysql_fetch_array($sql))
{ 

Open in new window


Obviously you can see the area it's referring to.
I tried to add a  or die(mysql_error());  but it's giving me an error in Dreamweaver.

Any suggestions would be great thanks.
0
PSTCAT
Asked:
PSTCAT
  • 4
  • 3
  • 2
  • +1
1 Solution
 
saimazzCommented:
you cant fetch from update query, there is no returning results. In your case if you need some results use SELECT statment in query.

SELCT * FROM messages WHERE id = '$id' AND to_user = '$user'"

Open in new window

0
 
Christopher Raymond MendozaCommented:
Have you checked the content of the variable $sql? It would not contain a set of rows/records since you are running an update statement, which is probably why mysql_fetch_array returns an error.

Try var_dump($sql) to check its content.
0
 
PSTCATAuthor Commented:
Thanks for replying.

I'm not trying to fetch a result I'm trying to set one.

The code, (I thought) would change a field from "no" to "yes" in the database
to mark the user's message as deleted.

Maybe I needed to add some more code to my question... here is a continuation from
from the while statement...

while($row = mysql_fetch_array($sql))
{ 
$user = $_SESSION['user'];
  echo "<table border=1>";
  echo "<tr><td>";
  echo "Message ID#: ";
  echo $row[id];
  echo "</td></tr>";
  echo "<tr><td>";
  echo "To: ";
  echo $row[to_user];
  echo "</td></tr>";
  echo "<tr><td>";
  echo "From: ";
  echo $row[from_user];
  echo "</td></tr>";
  echo "<tr><td>";
  echo "Message: ";
  echo $row[message];
  echo "</td></tr>";
  echo "</br>";
  mysql_query("UPDATE messages SET read_yet = 'yes' WHERE to_user = '$user' AND id ='$row_id'")or die(mysql_error());
?>

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Christopher Raymond MendozaCommented:
Help me understand this, please bear with me:
    1. you would like to set deleted = 'yes'
    2. display 'deleted' messages
    3. set read_yet = 'yes' for the displayed messages

Am I on the right track?
0
 
PSTCATAuthor Commented:
Maybe  I've got this entirely wrong.

I'm referring to the inbox page, I want to display the messages received and have a delete option.

Here is the entire code...

<?php
session_start();
require("database.php");

$user = $_SESSION['user'];

if (isset($_POST['view_old'])) {
$user = $_SESSION['user'];
$query = mysql_query("SELECT * FROM messages WHERE to_user = '$user' AND deleted = 'no'")or die(mysql_error());
while($row2 = mysql_fetch_array($query))
{ 
  echo "<table border=1>";
  echo "<tr><td>";
  echo "Message ID#: ";
  echo $row2['id'];
  echo "</td></tr>";
  echo "<tr><td>";
  echo "To: ";
  echo $row2['to_user'];
  echo "</td></tr>";
  echo "<tr><td>";
  echo "From: ";
  echo $row2['from_user'];
  echo " ";
  echo "</td></tr>";
  echo "<tr><td>";
  echo "Message: ";
  echo bb ($row2['message']);
  echo "</td></tr>";
  echo "</br>";
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td colspan=2></td></tr>
<tr><td></td><td>
<input type="hidden" name="id" maxlength="32" value = "<?php echo $row2['id']; ?>">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" class="button" name="delete" value="Delete Message">
</td></tr>
</table>
</form>
<?php
}
}

if (isset($_POST['delete'])) {
$id = $_POST['id'];
$user = $_SESSION['user'];
$sql = mysql_query("UPDATE messages SET deleted = 'yes' WHERE id = '$id' AND to_user = '$user'")or die(mysql_error());
echo "Your message has been succesfully deleted.";
}
while($row = mysql_fetch_array($sql))
{ 
$user = $_SESSION['user'];
  echo "<table border=1>";
  echo "<tr><td>";
  echo "Message ID#: ";
  echo $row[id];
  echo "</td></tr>";
  echo "<tr><td>";
  echo "To: ";
  echo $row[to_user];
  echo "</td></tr>";
  echo "<tr><td>";
  echo "From: ";
  echo $row[from_user];
  echo "</td></tr>";
  echo "<tr><td>";
  echo "Message: ";
  echo $row[message];
  echo "</td></tr>";
  echo "</br>";
  mysql_query("UPDATE messages SET message_read = 'yes' WHERE to_user = '$user' AND id ='$row_id'")or die(mysql_error());
?>

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td colspan=2></td></tr>
<tr><td></td><td>
<input type="hidden" name="id" maxlength="32" value = "<?php echo $row['id']; ?>">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" class="button" name="delete" value="Delete Message">
</td></tr>
</table>
</form>

<?

}
echo "</table>";
?>

Open in new window


Note: I changed read_yet to message_read. (Typo from me, sorry)
0
 
maeltarCommented:
Lines 50 and 53 are not compatible, you are updating a files and then trying to fetch using the same sql ...

0
 
maeltarCommented:
oops..  files = fields
0
 
PSTCATAuthor Commented:
Thanks maeltar,
Any suggestions of the best option for this?

0
 
maeltarCommented:
Just add another sql select statement before line 53

$query = mysql_query("SELECT * FROM messages WHERE to_user = '$user' AND deleted = 'no'")or die(mysql_error());

then change line 53 from

while($row = mysql_fetch_array($sql))

Open in new window

to

while($row = mysql_fetch_array($query))

Open in new window

0
 
PSTCATAuthor Commented:
Oh I see!

Excellent. It's now working perfectly.
Thanks a lot for all your help.


And thanks to everyone for your suggestions.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now