Query value inside of another query
Ok I have these two queries
$q = "INSERT INTO pages (title, subtitle, link_title, content, posted) VALUES ('$t', '$st', '$lt', '$c', UTC_TIMESTAMP())";
$pq = "SELECT page_id FROM pages WHERE posted 开发者_运维知识库= {$q['posted']}";
I need to get the page_id from pages where the the UTC timestamp equals the posted value in the first query. How is this possible? Basically I need to call the array query value from the first one into the second.
Thanks
Here's what I would do:
- Make sure you absolutely have to insert the data before working with it in PHP. Otherwise, it might make sense to work with the data and insert afterwards.
- Assuming you passed the test in #1, Run
mysql_query($q);
- Check the response for errors and handle them if there are any
- If the query succeeded, get the ID of the inserted row with
mysql_insert_id();
- Run
mysql_query($pq)
, using the value of the inserted ID to retrieve the row you just inserted.
Instead of letting MySQL generate the timestamp, can you generate it from PHP and use that in both functions?
Alternatively, instead of running the second query, why not just use the mysql_insert_id to get the id of the last query you ran - this is a lot more foolproof and doesn't require a second query.
精彩评论