Page 1 of 1

php/mysql - retrieve mysql result and place in parameter

Posted: Wed Feb 09, 2005 12:52 pm
by ananth_ak
Hi,

i have a sql select query:

select value from valuetable where valueid = '34';

what i want to do is, get the value from the result and pass it into a parameter, so i can use it to insert into another table
is this possible?

i know you retrieve the last entered id from a table using: mysqli_insert_id function

just wondering if there is a general solution for getting any data.
thanks for your time.

Posted: Wed Feb 09, 2005 1:00 pm
by feyd
do the select query, fetch the resultant value, do the insert query.

Unless your database supports subqueries, it's not really possible to do it in short space.

php/mysql - retrieve mysql result and place in parameter

Posted: Wed Feb 09, 2005 7:11 pm
by ananth_ak
thanks for the reply

i've got mysql5 installed which supports sub-queries. but ive not tried it yet.

what about if i do:

insert value1 into table2
(select value1 from table1 where valueid=31)

value1 will be stored as an id in the table table2, so i can retrieve the data using mysqli_insert_id.

long way round, what you think?

Posted: Wed Feb 09, 2005 7:17 pm
by feyd
roughly looks okay to me, but I don't work with newer versions of MySQL that support subqueries.. so I can't say for sure.

Posted: Wed Feb 09, 2005 8:40 pm
by ananth_ak
nope, that didnt work. the search goes on for the solution.

Posted: Wed Feb 09, 2005 8:44 pm
by John Cartwright
As mentioned erlier, you could try 2 seperate queries.

Posted: Wed Feb 09, 2005 8:50 pm
by ananth_ak
tried it. the data entered into the tmp table fine, but the mysqli_insert_id function just outputs 0.

Posted: Wed Feb 09, 2005 8:54 pm
by John Cartwright
from php.net/
Return Values
The value of the AUTO_INCREMENT field that was updated by the previous query. Returns zero if there was no previous query on the connection or if the query did not update an AUTO_INCREMENT value.

Posted: Wed Feb 09, 2005 9:05 pm
by ananth_ak
thanks for that.
i forgot that the function doesnt retrieve any old data type.

is there any solution for retrieving single data items from a query of type: dates, varchar and int.

mysqli_fetch_field - can select primary key - but it puts it into an object.. is there any way of getting of extracting this object?

Posted: Wed Feb 09, 2005 9:09 pm
by feyd
mysql_fetch_result() use it sparingly..

Posted: Thu Feb 10, 2005 7:02 am
by ananth_ak
SOLUTION FOUND.

what i did was pass the result into mysqli_fetch_array
and used while look to populate a list for it,
then i passed this list (hopefully query all produces one result) into an array - and then i can pass it globally around all my other pages.

$sql = "select value from t1 where tname='ananth' and tolgroup='first';";
$rs=mysqli_query($conn,$sql);

while($row= mysqli_fetch_array($rs)){
$list .= $row["value"] ;

}

$qt2 = $list;
echo "$qt2";

thanks for all your suggestions.