mysql_fetch_array() when querying from more than 1 table

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

mysql_fetch_array() when querying from more than 1 table

Post by anjanesh »

Hello
I have this

Code: Select all

$res=mysql_query("SELECT a.InvStatus,b.CustID,b.IncrCredit FROM CartMaster a,Customer b WHERE a.CustID=b.CustID");
$row=mysql_fetch_row($res);
$row['IncrCredit'] won't work but $row[2] will. Or is there another method ?

$RewardPts=$row['IncrCredit'];
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in xxx.......xxx\xxx.php on line 179

Thanks
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

use column alias to id the column, then use the alias name

Code: Select all

$res=mysql_query("SELECT a.InvStatus as status,b.CustID as ID, b.IncrCredit as Credit FROM CartMaster a,Customer b WHERE a.CustID=b.CustID"); 

...

$customer = $rows['ID'];
$credit = $rows['Credit'];
....
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Now it doesn't give a warning. It gives a notice. Is there anything I have to change in php.ini ?

Notice: Undefined index: IncrCredit in c:\program files\apache group\apache\htdocs\shoppingportal\admin\invoicestatus.php on line 180
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Sorry. That had to be changed to mysql_fetch_array()
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

I am not getting a warning for this :

Code: Select all

$sql="
SELECT (a.PresentCredit*b.Qty)
FROM Product a,CartDetail b
WHERE a.PID=b.PID AND a.CartID='".$_POST['InvNo'][$i]."'"

$res=mysql_query($sql);

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\program files\apache group\apache\htdocs\shoppingportal\admin\invoicestatus.php on line 182

And in phpmyadmin it doesn't show any error but at the same time it does not display the multiplication result of these 2 values
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Try making your mysql_query line into this:

Code: Select all

$res=mysql_query($sql) or die(mysql_error());
just to see if you get an error when you query.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

just to clarify mysql_fetch_row()... it only returns index offset built queries.. so $blah['somefield'] will always fire a warning.
Post Reply