开发者

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:

  1. 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.
  2. Assuming you passed the test in #1, Run mysql_query($q);
  3. Check the response for errors and handle them if there are any
  4. If the query succeeded, get the ID of the inserted row with mysql_insert_id();
  5. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜