$mysqli = new mysqli("localhost", "username", "password", "test");
if (mysqli_connect_errno()) {
echo("Failed to connect, the error message is : ".
mysqli_connect_error());
exit();
}
$mysqli->query("call sp_create_user('hasin')");
?>
That's it!
PDO
Another new extension added in PHP 5.1 for managing databases is PDO (although
PDO was available with PHP 5.0 as a PECL Extension). This comes with a set of
drivers for working with different database engines. PDO stands for PHP Data
Objects. It is developed to provide a lightweight interface for different database
engines. And one of the very good features of PDO is that it works like a Data Access
Layer so that you can use the same function names for all database engines.
Chapter 7
[ 177 ]
You can connect to different databases using DSN (Data Source Name) strings. In the
following example we will connect to a MySQL databases and retrieve some data.
$dsn = 'mysql:dbname=test;host=localhost;';
$user = 'user';
$password = 'password';
try {
$pdo = new PDO($dsn, $user, $password);
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
$result = $pdo->query("select * from users");
foreach ($result as $row)
echo $row['name'];
?>
That's fairly hassle free, right? It just connects to MySQL server with the DSN
(here it connects to test database) and then executes the query.
Pages:
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191