Have you tried that or are you new to PHP/MySQL?
<?php
// Connecting to MySQL... mysql_connect('host','user','password')
mysql_connect('localhost', 'root', 'somepass');
// Selecting a database to work in
mysql_select_db('dbname');
// The SQL you are working with
$sql = 'SELECT * FROM table';
// Setting a resource so we can use so we can communicate with the database
$resource = mysql_query($sql);
// Looping through the results (since the result is an array) and printing the results in the desired format
while($result = mysql_fetch_assoc($resource))
{
echo "<a href=\"{$result['URL']"\">{$result['title']}</a><br /> {$result['description']}<br />{$result['URL']}<br /><br/>";
}
?>
- <?php
- // Connecting to MySQL... mysql_connect('host','user','password')
- mysql_connect('localhost', 'root', 'somepass');
-
- // Selecting a database to work in
- mysql_select_db('dbname');
-
- // The SQL you are working with
- $sql = 'SELECT * FROM table';
-
- // Setting a resource so we can use so we can communicate with the database
- $resource = mysql_query($sql);
-
- // Looping through the results (since the result is an array) and printing the results in the desired format
- while($result = mysql_fetch_assoc($resource))
- {
- echo "<a href=\"{$result['URL']"\">{$result['title']}</a><br /> {$result['description']}<br />{$result['URL']}<br /><br/>";
- }
- ?>
When you select anything from the database it would be retrieved as an object or array... depends on how you ask for it.
ArrayThere are different kinds of arrays you can get from the database and it's all based on the type of function you use.
Associative Array - In this kind of array the key would be the fieldname and the value would be the value of the field. Below is an example of an associative array:
<?php
$associative = array('key' => 'value',
'fieldname' => 'fieldvalue',
'URL' => 'http://www.google.com');
?>
- <?php
- $associative = array('key' => 'value',
- 'fieldname' => 'fieldvalue',
- 'URL' => 'http://www.google.com');
- ?>
Numeric and/or Associative Array This kind of function can retrieve the information from the database in either associative array, or numeric array, or both if you prefer. By default, it would be both. Below is a usage example and an example of a result using this function. (I explain the code in the comments, so read the comments in there).
<?php
// We only want a numeric array
foreach($result = mysql_fetch_array($resource, MYSQL_NUM))
{
echo $result[2];
/*
array(1 => 'field1value',
2 => 'field2value');
*/
}
// We only want associative array
foreach($result = mysql_fetch_array($resource, MYSQL_ASSOC))
{
echo $result['URL'];
/*
array('id' => 'fieldidvalue',
'URL' => 'fieldURLvalue');
*/
}
// Now we want both types of array
foreach($result = mysql_fetch_array($resource)) // mysql_fetch_array($resource, MYSQL_BOTH) <-- That would be the same thing
{
echo $result['URL'] . '<br />';
echo $result[2];
/*
array('id' => 'fieldidvalue',
1 => 'fieldidvalue', // Same as the 'id'... just the key is numeric
'URL' => 'fieldURLvalue',
2 => 'fieldURLvalue'); // Same as the 'URL'... just the key is numeric
*/
}
?>
- <?php
- // We only want a numeric array
- foreach($result = mysql_fetch_array($resource, MYSQL_NUM))
- {
- echo $result[2];
- /*
- array(1 => 'field1value',
- 2 => 'field2value');
- */
- }
-
- // We only want associative array
- foreach($result = mysql_fetch_array($resource, MYSQL_ASSOC))
- {
- echo $result['URL'];
- /*
- array('id' => 'fieldidvalue',
- 'URL' => 'fieldURLvalue');
- */
- }
-
- // Now we want both types of array
- foreach($result = mysql_fetch_array($resource)) // mysql_fetch_array($resource, MYSQL_BOTH) <-- That would be the same thing
- {
- echo $result['URL'] . '<br />';
- echo $result[2];
- /*
- array('id' => 'fieldidvalue',
- 1 => 'fieldidvalue', // Same as the 'id'... just the key is numeric
- 'URL' => 'fieldURLvalue',
- 2 => 'fieldURLvalue'); // Same as the 'URL'... just the key is numeric
- */
- }
- ?>
Numeric Array - Retrieves the information from the database in a numerical array.
<?php
$numeric = array(1 => 'value',
2 => 'fieldvalue',
3 => 'http://www.google.com');
?>
- <?php
- $numeric = array(1 => 'value',
- 2 => 'fieldvalue',
- 3 => 'http://www.google.com');
- ?>
Not sure why they made three different ones when the middle one can do both of them...
ObjectYou can also retrieve data from the database as an object instead of an array.
mysql_fetch_object - "Returns an object with properties that correspond to the fetched row..." (
http://www.php.net).
<?php
foreach($result = mysql_fetch_object($resource))
{
echo $result->URL . <br />;
echo $result->description;
}
?>
- <?php
- foreach($result = mysql_fetch_object($resource))
- {
- echo $result->URL . <br />;
- echo $result->description;
- }
- ?>
Objects are a little different then arrays and they are used differently too, as you can see in the example above. instead of...
$result['description'];... we do ...
$result->description;Hope that this have helped you understand how this thing works a little better
