Her kommer et lite eksempel på hvordan du kan hente ut informasjon fra en MySQL database. Det er et veldig simpelt eksempel for å få ting startet her på Dev1.no. Kodene er dokumentert på engelsk, da det er en vane for meg og er god kodeskikk å gjøre det.
Dette er kun en kjapp introduksjon, som sagt tidligere, og burde ikke brukes i en live side. Det burde komme opp sjekk på innhold i databasen om det skulle brukes.
Spør om det er noe usikkerheter ved koden.
<?php
/************************
* The (really) basic: *
************************
* Example code of fetching information out
* of a MySQL database. The first in the series
* of examples on how to fetch information out of
* diffrent sources. Later we'll have fetching from:
* - External/Internal document (With regexp)
* - Flat files (Non XML)
* - XML-document
*
* And ofcourse from MySQL, as you'll see from this
* example.
*
* This is a code from Mikael Brevik at
* http://mikaelb.net
*
*/
// Connect to MySQL and select database to work in
// First argument is server (usally localhost)
// Second argument is username, and the last is password
$db_link = mysql_connect ('localhost', 'username', 'password');
// Check if we were able to connect...
if(!$db_link) {
die ("Couldn't connect to the database");
}
// Select the database whom we are working in for this project.
// The last argument isn't nessesery, but in projects where you
// have multiple connections it's nessesery to have control.
$database_link = mysql_select_db ('database_name', $db_link);
// Check if the db selection was successfull.
if(!$database_link) {
die ("Database selection failed.");
}
// Now that we've connected to the db and selected what
// db we're working on, we'll show the db what information
// we'd like to fetch. In this example we'll just fetch
// info from two fields: name and age. Won't say to much
// about this, since it's not really essisential for the
// guide.
$query = "SELECT `age`, `name` FROM `my_table`";
// Run the query (as seen above) to the MySQL-server
$result = mysql_query ($query);
// Check if the query returned successfully.
if(!$result) {
die ("Couldn't fetch information out of the
database. Error with the query");
}
// We're collecting the output into a variable to save
// resources by not using a echo-statment by every instance
// of the loop. (Making a nice undefined list in HTML of it)
$output = '
- ' . "\n";
- '.$row['name'].' ('.$row['age'].')
// Now here we do all the magic. We use the resource we get from
// the mysql_query ()-function and make some usefull information
// out of it. We got 3 regular functions to do this for us. All
// are used in a loop (I think it's easiest to use a while()-loop),
// like the one you see beneeth this comment.
// Those functions are:
// - mysql_fetch_array () :: Fetch into an array with numeric and
// name-taged keys. (Both that is. Numeric is set by what order
// they are in)
// - mysql_fetch_assoc () :: Same as the above, just without the
// numeric keys.
// - mysql_fetch_object () :: Almost the same as the first one,
// just that $row will be an object (learn about this later in
// OOP.) You get the information into a string-type by using $row->name
// instead of the array-version ($row['name']).
while ($row = mysql_fetch_assoc($result)) {
$output .= '
'."\n";
}
// Wrap up the undefined list.
$output .= '
' . "\n";
// Print out the collected results:
echo $output;
// There you go, hope that'll be of some help, even if it's
// really basic and a small code. It should be well (maybe
// too well) documented.
// - Mikael Brevik
?>