Full-service Internet Marketing & Web Development
Recent Posts

Sponsors
![]() |
generic connect display mysql db infoCode Wizard, 12-30-2006 |
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.
<?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.
$tmp .= "ID : $row->id <br>
";
$tmp .= "Name : $row->name <br>
";
$tmp .= "Url : $row->url <br><hr>
";
// 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;
?>
|
|
Subscribe Now to receive new posts via Email as soon as they come out.
Comments
Post your comments

