Page 1 of 1

Issues with mysql_fetch_assoc()

Posted: Tue Aug 31, 2010 5:48 pm
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>
.....
.....

Re: Issues with mysql_fetch_assoc()

Posted: Tue Aug 31, 2010 5:59 pm
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'

Re: Issues with mysql_fetch_assoc()

Posted: Tue Aug 31, 2010 6:01 pm
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!

Re: Issues with mysql_fetch_assoc()

Posted: Tue Aug 31, 2010 6:26 pm
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'];
		}
}
	
?>

Re: Issues with mysql_fetch_assoc()

Posted: Tue Aug 31, 2010 6:31 pm
by mikosiko
read again Eric's post... specially the last line and compare it with what you wrote :wink:

Re: Issues with mysql_fetch_assoc()

Posted: Tue Aug 31, 2010 6:43 pm
by ZEKI692
I have and I'm still getting an error. I'm not clear on what I'm doing wrong.

Re: Issues with mysql_fetch_assoc()

Posted: Tue Aug 31, 2010 8:52 pm
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?

Re: Issues with mysql_fetch_assoc()

Posted: Thu Sep 02, 2010 2:07 pm
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'];
	}
}

Re: Issues with mysql_fetch_assoc()

Posted: Thu Sep 02, 2010 5:22 pm
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!