This is a heavily commented snippet that "connects to database and selects information and displays it" , this is for someone wanting to connect to and use a mysql database.
By : philip
<?php
// Set the various database connect information. Host is usually // localhost. If no password, just leave blank. This information is // given by your web host OR if your own server, you setup yourself. // See php manual for setup instructions. $db_host = ''; $db_user = ''; $db_pass = ''; $db_name = '';
// More then one table can be used but in ths example we use just // one. :-) And in many scripts just one is used. And it's easiest // to comment on. $db_table = '';
// ---- END CONFIGURATIONS ------------------------------------------//
// Make a connect to mysql. Through this connect many databases // and tables can be selected. $conn will be what we call this // connection. $conn = mysql_connect($db_host,$db_user,$db_pass);
if ($conn == true) {
// Select the database. mysql_select_db($db_name,$conn);
// We are selecting all fields (*) from database and using the $conn // database connection. By default $conn would be used but we're // learning so may as well not be lazy :-) Usually you wouldn't // select * but for the lazy and unsure, it works nicely. // // This is a very powerful function, one can query *any* SQL statement // and do many things, like update/delete/insert/select data. A good // basic SQL tutorial is : sqlcourse.com . The result of this // query is named $result $result = mysql_query("SELECT * from $db_table",$conn);
// While a row exists to equal the fetched object (row) of data // continue to loop until end of fetch (at the end) // The way data is called may be new. $row->dbfieldname . See // the manual on mysql_fetch_object() for more info. Other functions // can just as easily have been used, like mysql_fetch_array. That method // one would use format as : $row['id']; , $row['name'] , etc. See manual // for all the possible (and beautiful) tools that are available. while($row = mysql_fetch_object($result)) {
// These will loop through entire database. $tmp keeps getting // appended to so it's getting very large. This is what's printed // and where one makes look pretty :-) Below the database fields // id, name and url are being printed from the $db_table table. And, // the .= means it appends (concatenates) to $tmp so $tmp grows larger // and later. See : http://www.zend.com/zend/tut/using-strings.php // Again, id,name and url are just examples ... replace according to // your database.
// The while loop ends here at this brace so the above cycles through until // all data is gathered. }
// Else we are not connected ($conn == false) so print error. } else { echo 'could not connect to database : '. mysql_error(); }
// Print the pulled data. print $tmp;
?>
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.