One of the most important uses for PHP is dealing with databases. Aside from web applications, there are few instances where a site would need to make any sort of computations, aside from getting and retreiving information from a data base. But there are MANY instances when data needs to be sent and retreived from a database.
In order to apply this sort of functionality, a web developer would need to know how to deal with the results of a MySQLi query. Those more familiar with other object oriented programming languages should have little trouble understanding how to use PHP to deal with the results. Those with little programming exsperience shouldn't have any trouble either.
In a PHP program, the developer may write a MySQLi SELECT statement to retreive data from the database. A simple query will look at only one column in the database. Other querys can return a combination of many columns and rows, but that is beyond the scope of this essay. The result of a MySQLi query can result in two different outcomes.
The first outcome is that the query failed for some reason. This would cause the PHP command to return a valuse of 'false'. This is very handy, since if a query fails the developer can plan accordingly with simple if or while statements.
The second outcome is that the query is succesful, and that a column is returned by the PHP command. This column must be stored in some kind of variable for the server to use:
$query_result = mysqli_query('SELECT column FROM table');
Alternatly, if we want to make our own error statement:
$query_result = @mysqli_query('SELECT column FROM table');
After this, we can set the error message:
if(!$query_result){
exit('Danger Will Robinson!');
}
Next, we will need to see how to access the data from the variable $query_result. It can be difficult to filter data after you've retreived it with a MySQLi query, so you need to make sure that you're database tables and querys will return you only the data that you are looking for. Once you have the result set stored in the variable $query_result. it can be difficult to get a specific line of data from the set. If we know that there is only one item in the result set, then we can simply use a single assignment operator. If we need to print out the results, or store them into an array, we do this with a simple loop.
Printing out:
while($data = mysqli_fetch_array($query_result){
echo '(html p tag)' . $data['column'] . '(html /p tag)';
}
Store as array:
$store = array();
for($i = 0; $i = sizeof($query_result; $i ++){
store($i) = mysqli_fetch_array($query_result);
}
It is important to know that we do not need to know the exact size of the result set, nor the exact type of data that is stored. Since PHP is a loosly typed language, we don't have to worry about data types. Using result sets is not difficult, as long as we know what to expect.
References:
Database Driven Wbsite, Ch.4, Kevin Yank
http://us.php.net/
Contact Me | Policies | Colophon
© 2009, Jonathan Dunstan, All Rights Reserved.