storing and using results from a query on another query

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
petroz
Forum Newbie
Posts: 16
Joined: Sun Sep 06, 2009 11:56 pm

storing and using results from a query on another query

Post by petroz »

Hi Guys,

I am having some troubles with this script. The goal is to get a unique ID from one table, store it as a array, then with another query update a second table using information from the first table all related to that first array created on the first query.

I am getting "Parse error: syntax error, unexpected T_STRING" on the first line of the second query.

I am pretty new at this.... so please forgive any stupid mistakes or complete misunderstandings.

Thanks,
P

Code: Select all

<?
include "db.php";
$uid = $_GET['uid'];
$request = $_GET['req'];
$requid = $_GET['requid'];
 
$getresp = "SELECT resp_uid FROM requests WHERE request = '$req'";  
 
$result = mysql_query($getresp) or die(mysql_error());
 
$row = mysql_fetch_array($result) or die(mysql_error());
 
$accept = "UPDATE requests2 SET `status` = '$status', `resp_name` = (SELECT `name` FROM `users` WHERE `uid` = '.$row'), `resp_organization` = (SELECT `organization` FROM `users` WHERE `uid` = '.$row'), `resp_email` = (SELECT `email` FROM `users` WHERE `uid` = '.$row'), `resp_phone` = (SELECT `phone` FROM `users` WHERE `uid` = '.$row'), `resp_url` (SELECT `email` FROM `users` WHERE `uid` = '.$row'), `resp_im` = (SELECT `im` FROM users WHERE `uid` = '.$row'),  `resp_address` = (SELECT `address` FROM `users` WHERE `uid` = '.$row')";  
 
$acceptresult = mysql_query($accept) or die(mysql_error());
?>
Also, if I echo the $row... All I see is "Array." Is that correct?
Last edited by petroz on Thu Oct 01, 2009 4:58 pm, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: storing and using results from a query on another query

Post by VladSun »

Correct.
Try print_r($row) ;)

Also, try to fetch all the data you need from users table and use it in the update query.
There are 10 types of people in this world, those who understand binary and those who don't
petroz
Forum Newbie
Posts: 16
Joined: Sun Sep 06, 2009 11:56 pm

Re: storing and using results from a query on another query

Post by petroz »

Thanks for the response Vlad!

OK... So now I can echo the $row I am expecting.. Not just "Array." But how can I use $row in the last query?

Also, can you explain? "Also, try to fetch all the data you need from users table and use it in the update query."

Thanks,
Peter
petroz
Forum Newbie
Posts: 16
Joined: Sun Sep 06, 2009 11:56 pm

Re: storing and using results from a query on another query

Post by petroz »

Just in case it helps..The results I am am getting are;

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SELECT `email` FROM `users` WHERE `uid` = 'Array'), `resp_im` = (SELECT `im` FR' at line 1

Thanks,
Peter
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: storing and using results from a query on another query

Post by VladSun »

print_r shows the contents of an *array* variable. You can reference an array element by its key:

Code: Select all

 
$id = $row['id'];
// or
$first_element = $row[0];
 
Read the PHP manual about using arrays.
There are 10 types of people in this world, those who understand binary and those who don't
petroz
Forum Newbie
Posts: 16
Joined: Sun Sep 06, 2009 11:56 pm

Re: storing and using results from a query on another query

Post by petroz »

Thanks Vlad!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: storing and using results from a query on another query

Post by VladSun »

petroz wrote:Also, can you explain? "Also, try to fetch all the data you need from users table and use it in the update query."
:)
Now you might want to try:

Code: Select all

<?
include "db.php";
$uid = $_GET['uid'];
$request = $_GET['req'];
$requid = $_GET['requid'];
 
$getresp = "SELECT * from `users` where `uid` = (SELECT resp_uid FROM requests WHERE request = '$req')";   
 
$result = mysql_query($getresp) or die(mysql_error());
 
$row = mysql_fetch_array($result) or die(mysql_error());
 
print_r($row);
See the results and modify your UPDATE query by removing ALL of the SELECT subqueries.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply