13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Querying a Database from the <strong>Web</strong><br />

273<br />

You use the following line in the script to connect to the <strong>MySQL</strong> server:<br />

@ $db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books');<br />

This line instantiates the mysqli class <strong>and</strong> creates a connection to host localhost with<br />

username bookorama, <strong>and</strong> password bookorama123.The connection is set up to use the<br />

database called books.<br />

Using this object-oriented approach, you can now invoke methods on this object to<br />

access the database. If you prefer a procedural approach, mysqli allows for this, too.To<br />

connect in a procedural fashion, you use<br />

@ $db = mysqli_connect('localhost', 'bookorama', 'bookorama123', 'books');<br />

This function returns a resource rather than an object.This resource represents the connection<br />

to the database, <strong>and</strong> if you are using the procedural approach, you will need to<br />

pass this resource in to all the other mysqli functions.This is very similar to the way the<br />

file-h<strong>and</strong>ling functions, such as fopen(), work.<br />

Most of the mysqli functions have an object-oriented interface <strong>and</strong> a procedural<br />

interface. Generally, the differences are that the procedural version function names start<br />

with mysqli_ <strong>and</strong> require you to pass in the resource h<strong>and</strong>le you obtained from<br />

mysqli_connect(). Database connections are an exception to this rule because they can<br />

be made by the mysqli object’s constructor.<br />

The result of your attempt at connection is worth checking because none of the rest of<br />

code will work without a valid database connection.You do this using the following code:<br />

if (mysqli_connect_errno()) {<br />

echo 'Error: Could not connect to database. Please try again later.';<br />

exit;<br />

}<br />

(This code is the same for the object-oriented <strong>and</strong> procedural versions.) The<br />

mysqli_connect_errno() function returns an error number on error, or zero on success.<br />

Note that when you connect to the database, you begin the line of code with the<br />

error suppression operator, @.This way, you can h<strong>and</strong>le any errors gracefully. (This could<br />

also be done with exceptions, which we have not used in this simple example.)<br />

Bear in mind that there is a limit to the number of <strong>MySQL</strong> connections that can<br />

exist at the same time.The <strong>MySQL</strong> parameter max_connections determines what this<br />

limit is.The purpose of this parameter <strong>and</strong> the related Apache parameter MaxClients is<br />

to tell the server to reject new connection requests instead of allowing machine<br />

resources to be completely used up at busy times or when software has crashed.<br />

You can alter both of these parameters from their default values by editing the configuration<br />

files.To set MaxClients in Apache, edit the httpd.conf file on your system.<br />

To set max_connections for <strong>MySQL</strong>, edit the file my.conf.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!