SELECT FROM database
by
02 May 2003
Ok, next step is to get information FROM a database, and let it display on your webpage.
__________________________________________________
We are still using the same table as in the previous artikel (tabel)
I will give you the code, and then explain it all to you.
PHP Code:
<?php
mysql_connect("host","user","password");
mysql_select_db("databasenaam");
$select = "SELECT * FROM tabel";
$query = mysql_query($select)or die(mysql_error());
?>
In this part we have called ALL (*) information from table tabel, in this script it won't display anything.
The part above, the connection to the database, has been explained in the previous aricle
PHP Code:
$select = "SELECT * FROM tabel";
$query = mysql_query($select)or die(mysql_error());
Select all (*) from the database, or give me the mysql error.
Very easy IMO
Now let's go to the next step!! Show the results on a webpage.
PHP Code:
<?php
mysql_connect("host","user","password");
mysql_select_db("databasenaam");
$select = "SELECT * FROM tabel";
$query = mysql_query($select)or die(mysql_error());
while($list = mysql_fetch_object($query)){
echo "$list->naam
";
echo "$list->email
";
echo "$list->titel
";
echo "$list->info";
?>
Ok, now we've displayed all information of table tabel, on a page.
I assume you don't really know what is going on, so i'll explain the part to you
PHP Code:
while($list = mysql_fetch_object($query)){
$list is replace-able with al things you want, actually it says:
$list = show everything from the table
echo "$list->naam
";
echo "$list->email
";
echo "$list->titel
";
echo "$list->info";
The echo code you will know i guess, this is just a simple code to write something on a webpage, everything you want to echo, has to be in the double quote ("). Things here displayed are:
naam, email, titel, info
And like you see, it will display all info, the layout of this is quite simple, but you can edit it all the way you want (per example add it into a table)
When you've openen an echo code, don't use double quote in the double quotes, but use singel quotes instead.
To go one more step deeper into mySQL i want to explain the WHERE function in a mySQL query:
PHP Code:
<?php
mysql_connect("host","user","password");
mysql_select_db("databasenaam");
$select = "SELECT * FROM tabel WHERE naam='flup'";
$query = mysql_query($select)or die(mysql_error());
while($list = mysql_fetch_object($query)){
echo "$list->naam
";
echo "$list->email
";
echo "$list->titel
";
echo "$list->info";
?'>
Like you see i've only edited a little part of the query, and that is:
WHERE naam='flup'
Or: Selected everything in the table 'table' where 'naam' is equal to flup
You also could replace: naam='flup' to email='email' or titel='administrator'
Good luck!!
|