Prev | Current Page 153 | Next

Emilian Balanescu and Cristian Darie

"Beginning PHP and MySQL E-Commerce: From Novice to Professional, Second Edition"


Connecting to MySQL
The SQL queries you write must be sent somehow to the database engine for execution. As
you learned in Chapter 2, you??™ll use PHP PDO to access the MySQL server.
Before writing the business tier code, you need to analyze and understand the possibilities
for implementation. The important questions to answer before writing any code include
the following:
??? What strategy should you adopt for opening and closing database connections when
you need to execute an SQL query?
??? Which methods of PHP PDO should you use for executing database stored procedures
and returning the results?
??? How should you handle possible errors and integrate the error-handling solution with
the error-handling code you wrote in Chapter 3?
Let??™s have a look at each of these questions one by one, and then we??™ll start writing some
code.
Opening and Closing Connections to the MySQL Server
There are two main possible approaches you can take for this. The first is illustrated by the following
sequence of actions, which needs to be executed each time the database needs to be accessed.
1. Open a connection to the database immediately before you need to execute a command
on the database.
2. Execute the SQL query (or the database stored procedure) using the open connection,
and get back the results. At this stage, you also need to handle any possible errors.


Pages:
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
Perfumy Kosmetyczka poker online alufelgi świadectwo energetyczne rzeszów