Can someone help me with this code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aseylys
Forum Newbie
Posts: 1
Joined: Mon Oct 10, 2011 6:22 pm

Can someone help me with this code?

Post by aseylys »

First, I'm sorry if this doesn't belong here. I'm new to this site.

But this code, it keeps returning a "mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource" error, and I'm not sure how to fix it. I know that the mysql fet assoc error is usually associated with a boolean false or syntax/naming error but I can't find anything. Can someone help?

PS I left out my mysql info for safety reasons.

Code: Select all

<?php
mysql_connect("mysql host name","mysql user name","mysql password");
mysql_select_db("database name");
$name=$_POST['name'];
$comment=$_POST['comment'];
$submit=$_POST['submit'];

$dbLink = mysql_connect("mysql host name", "mysql user name", "mysql password");
	mysql_query("SET character_set_client=utf8", $dbLink);
	mysql_query("SET character_set_connection=utf8", $dbLink);

if($submit)
{
if($name&&$comment)
{
$insert=mysql_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment') ");
}
else
{
echo "please fill out all fields";
}
}
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Comment box</title>
</head>

<body>
<center>
<form action="commentindex.php" method="POST">
<table>
<tr><td>Name: <br><input type="text" name="name"/></td></tr>
<tr><td colspan="2">Comment: </td></tr>
<tr><td colspan="5"><textarea name="comment" rows="10" cols="50"></textarea></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Comment"></td></tr>
</table>
</form>

<?php
$dbLink = mysql_connect("mysql host name", "mysql username", "mysql password");
	mysql_query("SET character_set_results=utf8", $dbLink);
	mb_language('uni');
	mb_internal_encoding('UTF-8');

$getquery=mysql_query("SELECT * FROM comment ORDER BY id DESC");
while($rows=mysql_fetch_assoc($getquery))
{
$id=$rows['id'];
$name=$rows['name'];
$comment=$rows['comment'];
echo $name . '<br/>' . '<br/>' . $comment . '<br/>' . '<br/>' . '<hr size="1"/>'
;}
?>

</body>
</html>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Can someone help me with this code?

Post by Celauran »

What output does this give?

Code: Select all

var_dump($getquery);
Why are you continually redefining the database connection?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Can someone help me with this code?

Post by Celauran »

Divided wrote:Sometimes that can be caused by the query returning NULL. If this is the case you will need to wrap your while loop in an if loop to ensure that the while only proceeds if the resource is not null.
mysql_query() doesn't return NULL.
ouchiko
Forum Commoner
Posts: 35
Joined: Sun Oct 09, 2011 6:54 pm
Location: London

Re: Can someone help me with this code?

Post by ouchiko »

push out mysql_error() after the initial connection etc to make sure everything is valid up until that point.

mysql_query(query, IDENTIFIER) <-- add the optional secondary paramter $dbLink

and the run print mysql_error() to ensure there are no errors.
Post Reply