27.10.2015 Views

AJAX and PHP

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Server-Side Techniques with <strong>PHP</strong> <strong>and</strong> MySQL<br />

This data will be used when performing database operations. Any database operation consists of<br />

three m<strong>and</strong>atory steps:<br />

1. Opening the database connection<br />

2. Executing the SQL queries <strong>and</strong> reading the results<br />

3. Closing the database connection<br />

It's a good practice to open the database connection as late as possible, <strong>and</strong> close it as soon as<br />

possible, because open database connections consume server resources. The following code<br />

snippet shows a simple <strong>PHP</strong> script that opens a connection, reads some data from the database,<br />

<strong>and</strong> closes the connection:<br />

// connect to the database<br />

$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);<br />

// what SQL query you want executed?<br />

$query = 'SELECT user_id, user_name FROM users';<br />

// execute the query<br />

$result = $mysqli->query($query);<br />

// do something with the results...<br />

// ...<br />

// close the input stream<br />

$result->close();<br />

// close the database connection<br />

$mysqli->close();<br />

Note that we use the mysqli library to access MySQL. This is a newer <strong>and</strong> improved<br />

version of the mysql library, which provides both object-oriented <strong>and</strong> procedural<br />

interfaces to MySQL, <strong>and</strong> can access more advanced features of MySQL. If you have<br />

older versions of MySQL or <strong>PHP</strong> that don't support mysqli, use mysql instead.<br />

The exercise that follows doesn't contain <strong>AJAX</strong>-specific functionality; it is just a simple example<br />

of accessing a MySQL database from <strong>PHP</strong> code.<br />

Time for Action—Working with <strong>PHP</strong> <strong>and</strong> MySQL<br />

1. Connect to the ajax database, <strong>and</strong> create a table named users with the following code:<br />

CREATE TABLE users<br />

(<br />

user_id INT UNSIGNED NOT NULL AUTO_INCREMENT,<br />

user_name VARCHAR(32) NOT NULL,<br />

PRIMARY KEY (user_id)<br />

);<br />

2. Execute the following INSERT T comm<strong>and</strong>s to populate your users table with some<br />

sample data:<br />

INSERT INTO users (user_name) VALUES ('bogdan');<br />

INSERT INTO users (user_name) VALUES ('filip');<br />

INSERT INTO users (user_name) VALUES ('mihai');<br />

INSERT INTO users (user_name) VALUES ('emilian');<br />

INSERT INTO users (user_name) VALUES ('paula');<br />

INSERT INTO users (user_name) VALUES ('cristian');<br />

Because user_id is an auto_increment column, its values will be generated by the database.<br />

106<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!