|
Pull It Back Out
OK, now we've got our data in the database. Let's do something with it. Copy and paste the following into a text file and save it in your Web server document tree with a .php3 extension. Let's explain what happens here. The mysql_connect() function opens a link to a MySQL server on the specified host (in this case it's localhost) along with a username (root). If you needed to specify apassword, you'd add it here as well. The result of the connection is stored in the variable $db. mysql_select_db() then tells PHP that any queries we make are against the mydb database. We could create multiple connections to databases on different servers. But for now, let's leave it to this. Next, mysql_query() does all the hard work. Using the database connection identifier, it sends a line of SQL to the MySQL server to be processed. The results that are returned are stored in the variable $result. Finally, mysql_result() is used to display the values of fields from our query. Using $result, we go to the first row, which is numbered 0, and display the value of the specified fields. The syntax of the printf function may seem a little odd if you haven't used Perl or C before. In each of the lines above, %s indicates that the variable in the second half of the expression (e.g., mysql_result($result,0,"position")) should be treated as a string and printed. For more on printf, see the PHP documentation. So there we have it. We successfully complied, installed, and configured MySQL and PHP, and we've executed a simple script to retrieve some information. |
Recent Posts
Pull It Back Out
Load Up a Database in Test Your First Script For Test Installing PHP3 Installing MySQL Introducing PHP and MySQL PHP/MySQL Overview Syndication Tools |
|
You are not logged in. FREE Sign Up or Log In
©2009 Flixya Entertainment, LLC. All rights reserved.




Free Sign Up - Start Making Money on Flixya »



