Post by Llama! on Sept 9, 2005 15:45:28 GMT
Hello
Here I will teach how to grab data out of a MySQL database with a while loop.
first connect to your database:
Now we want to make a query which will grab the date later:
Fill in table_name with the name of the table you want to grab stuff out. In this tut table_name well have the fields ID, Name, Email.
Now let's make the while loop:
Now the While loop is inserted as $ins wich we need to grab the data.
Now we have to echo data out of the database:
Now it will echo the data out of the database. But when the dtaabse is empty it shows nothing... let's add an error. For the error you check or the query is empty:
Hope you understand and have fun
Here I will teach how to grab data out of a MySQL database with a while loop.
first connect to your database:
<?php
$con = mysql_connect("localhost", "username", "pass");
mysql_select_db("db_name", $con);
?>
Now we want to make a query which will grab the date later:
<?php
$con = mysql_connect("localhost", "username", "pass");
mysql_select_db("db_name", $con);
$query = mysql_query("SELECT * FROM table_name ORDER BY id DESC");
?>
Fill in table_name with the name of the table you want to grab stuff out. In this tut table_name well have the fields ID, Name, Email.
Now let's make the while loop:
<?php
$con = mysql_connect("localhost", "username", "pass");
mysql_select_db("db_name", $con);
$query = mysql_query("SELECT * FROM table_name ORDER BY id DESC");
while($ins = mysql_fetch_assoc($query))
{
}
?>
Now the While loop is inserted as $ins wich we need to grab the data.
Now we have to echo data out of the database:
<?php
$con = mysql_connect("localhost", "username", "pass");
mysql_select_db("db_name", $con);
$query = mysql_query("SELECT * FROM table_name ORDER BY id DESC");
while($ins = mysql_fetch_assoc($query))
{
echo "Id: ".$ins['id']." is named ".$ins['name']." and has the email ".$ins['email']"<br>";
}
?>
Now it will echo the data out of the database. But when the dtaabse is empty it shows nothing... let's add an error. For the error you check or the query is empty:
<?php
$con = mysql_connect("localhost", "username", "pass");
mysql_select_db("db_name", $con);
$query = mysql_query("SELECT * FROM table_name ORDER BY id DESC");
while($ins = mysql_fetch_assoc($query))
{
if(!empty($query)) //when not empty
{
echo "Id: ".$ins['id']." is named ".$ins['name']." and has the email ".$ins['email']"<br>";
}else{ //if empty
echo "There is no data in the database";
}
}
?>
Hope you understand and have fun