Sorry I think I posted it on the Wrong Forum, Please Delete this Post... :(

$result = mysql_query("
SELECT q.*, IF(v.id,1,0) AS voted
FROM quotes AS q
LEFT JOIN quotes_votes AS v 
ON 	q.id = v.qid
	AND v.ip =".$ip."
	AND v.date_submit = '".$today."'
");

$i=1;
$str='';
$script='';

while($row=mysql_fetch_assoc($result))
{
	// Looping through all the quotes and generating the list on the right of the page:
	
	$str.= '<div class="thumb" id="q-'.$i.'">'.substr($row['txt'],0,20).
	'<span class="points">...</span><div class="star-rating" id="rating-'.$i.'">';
	
	$row['rating'] = round($row['rating']);
	for($z=0;$z<5;$z++)
	{
		$str.='<input type="radio" name="rate-'.$i.'" value="'.($z+1).'" '.($z+1==$row['rating']?'checked="checked"':'').' disabled="disabled" />';
	}

	$str.='</div></div>';
	
	// Each quote calls the fillData JS function
	$script.="fillData(".formatJSON($row).");".PHP_EOL;
	
	$i++;
}

This is a Code for Quotation Script where User can Vote for the Quotation and Stuff..
But whenever I run this I encounter this Error:-

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /public_html/index.php on line 31

Please Help! Thank You! :D

Dani AI

Generated

Most likely cause: the SELECT failed so mysql_query() returned false, and mysql_fetch_assoc() was given that false value. That typically comes from a SQL syntax error, a broken DB connection, or malformed/unterminated string concatenation when you inject PHP variables into the SQL. As suggested, print the exact SQL you actually sent and run it in phpMyAdmin — that will show the MySQL error directly.

A minimal, practical check to add before you loop:

/* escape inputs first */
$ip_safe    = mysql_real_escape_string($ip);
$today_safe = mysql_real_escape_string($today);

/* run query (build $query separately) */
$result = mysql_query($query);
if (!$result) {
    error_log('MySQL error: ' . mysql_error() . ' -- SQL: ' . $query);
    die('Database error.'); /* remove or replace in production */
}

Key debugging steps that fix the majority of these warnings: ensure the DB connection is successful before querying; make sure string-valued variables are quoted inside SQL (or escaped first with mysql_real_escape_string()); check for accidental missing spaces when concatenating parts of the query (that collapses tokens and creates syntax errors); try enclosing column/table names in backticks if a name might be reserved; and confirm the logged SQL runs cleanly in phpMyAdmin. Also look at your error log rather than only relying on displayed warnings, and avoid leaving die() with raw SQL visible in production.

For longer-term safety consider switching to prepared statements via mysqli or PDO to avoid injection and quoting mistakes. PHP docs: mysql_query, mysql_error, mysql_real_escape_string, and the mysqli/PDO alternatives.

Recommended Answers

All 3 Replies

>> Sorry I think I posted it on the Wrong Forum, Please Delete this Post.

It'll get moved in a short while. Be patient :)

commented: Moved! +10

I already Posted it here, In PHP :P Please Delete this :D

There is problem with your query.
before executing it do following.

$query="SELECT q.*, IF(v.id,1,0) AS votedFROM quotes AS qLEFT JOIN quotes_votes AS v ON 	q.id = v.qid	AND v.ip =".$ip."	AND v.date_submit = '".$today."'";

echo $query;
.
.
.
$result = mysql_query($query);
.
.
.

copy the output of query, run it in phpmyadmin and check the query syntax properly there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.