-2

In my page A.php , I prepare a query like this : SELECT * FROM table WHERE id = ?

And I can use same PDO Prepared statements in this page , that I know.

But , if other page B.php also need the same query .

Am I need to prepared that query again?

Or how can I use the same PDO Prepared statements.

Or when I prepare the same query , server will auto know that was prepared?


Situation:

If i have a database website , then there are hundreds of pages load data just like the same way.

When user open each page , the query will send every times.

How can I build the page more wisely?


I am not looking for use the same "STRING" of query ;

I mean it is prepared statements , that I don't use $PDO->prepare again.

Is it possible?

fire790620
  • 49
  • 8
  • You will either need to add that query to `B.php`, or place the query in a separate file, ie. `includes.php`, and include it in both `A.php` and `B.php`. – Sean Aug 25 '16 at 03:26
  • But how can I pass the last Prepared statements ? – fire790620 Aug 25 '16 at 03:47
  • Do you understand how PHPs processing model works? Each request starts without any allocated resources and prior variables. How would you imagine/go about reusing a prepared $statement/handle/object between A.php and B.php? – mario Aug 25 '16 at 03:51
  • You didn't need to say that, you can just say : yes , you need to prepare it every page. I just image a way to storage the prepared statement. – fire790620 Aug 25 '16 at 04:03
  • i just wondering what the PDO Prepared statements can do. or it was seem to waste the resource. – fire790620 Aug 25 '16 at 04:07
  • 1
    http://stackoverflow.com/questions/2039416/are-prepared-statements-cached-server-side-across-multiple-page-loads-with-php – fire790620 Aug 25 '16 at 05:28
  • This link is more helpful – fire790620 Aug 25 '16 at 05:29

1 Answers1

2

By definition PHP scripts are standalone. So when you load A.php you have to connect to the DB and run your queries. When A.php is done processing, PHP will close the connection.

When you load B.php you will have to do the same thing

Machavity
  • 30,841
  • 27
  • 92
  • 100