Page 1 of 1

storing and using results from a query on another query

Posted: Thu Oct 01, 2009 3:36 pm
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?

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

Posted: Thu Oct 01, 2009 4:49 pm
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.

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

Posted: Thu Oct 01, 2009 5:08 pm
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

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

Posted: Thu Oct 01, 2009 5:24 pm
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

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

Posted: Thu Oct 01, 2009 5:56 pm
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.

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

Posted: Thu Oct 01, 2009 9:04 pm
by petroz
Thanks Vlad!

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

Posted: Fri Oct 02, 2009 2:48 am
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.