php/mysql - retrieve mysql result and place in parameter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ananth_ak
Forum Newbie
Posts: 23
Joined: Wed Feb 09, 2005 12:42 pm
Location: uk, london
Contact:

php/mysql - retrieve mysql result and place in parameter

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
ananth_ak
Forum Newbie
Posts: 23
Joined: Wed Feb 09, 2005 12:42 pm
Location: uk, london
Contact:

php/mysql - retrieve mysql result and place in parameter

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
ananth_ak
Forum Newbie
Posts: 23
Joined: Wed Feb 09, 2005 12:42 pm
Location: uk, london
Contact:

Post by ananth_ak »

nope, that didnt work. the search goes on for the solution.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

As mentioned erlier, you could try 2 seperate queries.
ananth_ak
Forum Newbie
Posts: 23
Joined: Wed Feb 09, 2005 12:42 pm
Location: uk, london
Contact:

Post by ananth_ak »

tried it. the data entered into the tmp table fine, but the mysqli_insert_id function just outputs 0.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
ananth_ak
Forum Newbie
Posts: 23
Joined: Wed Feb 09, 2005 12:42 pm
Location: uk, london
Contact:

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_fetch_result() use it sparingly..
ananth_ak
Forum Newbie
Posts: 23
Joined: Wed Feb 09, 2005 12:42 pm
Location: uk, london
Contact:

Post 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.
Post Reply