Issues with mysql_fetch_assoc()

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
ZEKI692
Forum Newbie
Posts: 12
Joined: Tue Aug 31, 2010 5:38 pm

Issues with mysql_fetch_assoc()

Post by ZEKI692 »

Hey, I keep getting an error when i try and load this page. It keeps saying "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in **********************"

This is the code:

Code: Select all

<?php
	mysql_connect("*****", "*****", "*****") or die(mysql_error());
	mysql_select_db("a*****") or die(mysql_error());
	
	$result = mysql_query("SELECT * FROM test");
	
function lookup($wanted) //Part number of the item this form is for.
{
	do { // Does the searching for the part name and the price.
		$row = mysql_fetch_assoc($result);
		$id = $row['id']; // Part number in the database
		$price = $row['price']; //Price listed for part number in the database
	} while ($id != $wanted);
}
	
?>

<html>
<head>
</head>
<body>

<script type="text/javascript"> 
function UpdateCost() {
var sum = 0;
var qty,
prc;
for (i=1; i<49; i++) {
qty = 'qty'+i;
prc = 'price'+i;
if
(document.getElementById(qty).checked == true)
{
sum += Number(document.getElementById(prc).value);
}
}
document.getElementById('totalcost').value = sum.toFixed(2);
}
</script> 	

# 236261 Engine A55 cpl. - kick * Inquire<br>
# 236274 Engine A55 cpl. - pedal * Inquire<br>

<form action="*****" method="post">
<input name="userid" type="hidden" value="*****">
<input type="hidden" name="return" value="*****">

<? lookup("230950"); ?>
<input type="hidden" name="product1" value="1/1 Crankcase A55 cpl. - kick <? echo $price; ?> ">
<input type="checkbox" id="qty1" name="qty1" value="1" onClick="UpdateCost()">
<input type="hidden" id="price1" name="price1" value="<? echo $price; ?>">1/1 # 230940 Crankcase A55 cpl. - kick <? echo $price; ?>
<br>
.....
.....
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Issues with mysql_fetch_assoc()

Post by Eric! »

That's because inside your function lookup() the resource variable $result is undefined. It is outside of the scope of your function. You either have to put the mysql_query inside the function or pass the variable pointing to the results.

By the way instead of just looping through all of the table looking for one item, you can get the database to find it for you, which is much more optimized. Look into using the WHERE clause in mysql....something like this:

SELECT * from test WHERE id='$wanted'
Last edited by Eric! on Tue Aug 31, 2010 6:07 pm, edited 3 times in total.
ZEKI692
Forum Newbie
Posts: 12
Joined: Tue Aug 31, 2010 5:38 pm

Re: Issues with mysql_fetch_assoc()

Post by ZEKI692 »

>.<
Wow, thats all? thanks. I just started messing with php today so I still make silly mistakes. Its all apart of learning I guess!
ZEKI692
Forum Newbie
Posts: 12
Joined: Tue Aug 31, 2010 5:38 pm

Re: Issues with mysql_fetch_assoc()

Post by ZEKI692 »

So, I tried doing what you suggested and I'm still getting the same error, even with the mysql_query inside the function.

Errors:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/s/c/s/scssalesweb/html/Streetmate2005Group1test.php on line 10

Code: Select all

$wanted = 0;
		
function lookup($wanted) //Part number of the item this form is for.
{
	$result = mysql_query("SELECT * FROM test WHERE $id=$wanted");
	while ($row = mysql_fetch_assoc(result)) 
	{
		$price = $row['price'];
		}
}
	
?>
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Issues with mysql_fetch_assoc()

Post by mikosiko »

read again Eric's post... specially the last line and compare it with what you wrote :wink:
ZEKI692
Forum Newbie
Posts: 12
Joined: Tue Aug 31, 2010 5:38 pm

Re: Issues with mysql_fetch_assoc()

Post by ZEKI692 »

I have and I'm still getting an error. I'm not clear on what I'm doing wrong.
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: Issues with mysql_fetch_assoc()

Post by internet-solution »

ZEKI692 wrote:I have and I'm still getting an error.
Read again. :roll:

Eric's code

Code: Select all

SELECT * from test WHERE id='$wanted'
Yours

Code: Select all

SELECT * FROM test WHERE $id=$wanted
Can you spot the difference?
ZEKI692
Forum Newbie
Posts: 12
Joined: Tue Aug 31, 2010 5:38 pm

Re: Issues with mysql_fetch_assoc()

Post by ZEKI692 »

All right, I see what I did, but now there's a new error.

Parse error: syntax error, unexpected T_STRING in /home/content/s/c/s/scssalesweb/html/Streetmate2005Group1test.php on line 9

Code for far:

Code: Select all


$wanted = NULL;
                
function lookup($wanted) //Part number of the item this form is for.
{
	$result = mysql_query(SELECT * from test WHERE id='$wanted');
         while($row = mysql_fetch_assoc($result)) 
	{      
		$price = $row['price'];
	}
}
ZEKI692
Forum Newbie
Posts: 12
Joined: Tue Aug 31, 2010 5:38 pm

Re: Issues with mysql_fetch_assoc()

Post by ZEKI692 »

So, I decided to read the manual on php.net, and learned quite a bit. I solved all my issues! Thanks for your help guys, it was much appreciated!
Post Reply