Llama!
Junior Member
Posts: 87
|
Post by Llama! on Sept 9, 2005 15:46:41 GMT
Hello again In this short tutorial i'll tell you how to look or the database really has something in it. First connect to the database: <?php $con = mysql_connect("localhost", "user", "pass"); mysql_select_db("db_name", $con); ?> Change what you need to change ("user", "pass" and "db_name") Now we make a query: <?php $con = mysql_connect("localhost", "user", "pass"); mysql_select_db("db_name", $con);
$query = mysql_query("SELECT * FROM table_name"); ?> Now we make a while loop to also let it echo everything out of the database when there is something in it. <?php $con = mysql_connect("localhost", "user", "pass"); mysql_select_db("db_name", $con);
$query = mysql_query("SELECT * FROM table_name");
while($ins = mysql_fetch_assoc($query)) {
} ?> Now we use the QUERY,a nd not the fetch_assoc, to look or the database has something in it. <?php $con = mysql_connect("localhost", "user", "pass"); mysql_select_db("db_name", $con);
$query = mysql_query("SELECT * FROM table_name");
while($ins = mysql_fetch_assoc($query)) {
if(empty($query)) { echo "Database table \"table_name\" is EMPTY"; }else{ echo "There is data in the database!"; } } ?> You need to edit: echo "There is data in the database!"; yourself to let it show the data. If you don't know how read my tut about grabbing data out of the database. Good Luck and hope you udnertsnad. Else shout
|
|
Boag
New Member
Stellaaa!!
Posts: 29
|
Post by Boag on Sept 10, 2005 19:54:09 GMT
Theres a much more efficient and quicker way to do that, here's how I would do it: <?php
$conn = mysql_connect("localhost","username","password"); mysql_select_db("db_name",$conn);
$check = mysql_num_rows(mysql_query("SELECT * FROM table_name",$conn));
if($check > 0){ echo "There Is Information In ThisTable"; }else{ echo "There Is No Information In This Table"; }
?> That completely removes all need for your while loop and all the other crazy stuff that you used - you should learn a bit of efficiency. - Boag
|
|
Llama!
Junior Member
Posts: 87
|
Post by Llama! on Sept 10, 2005 19:57:39 GMT
Well. I don't mind. If it works I am ok with it.
|
|
Boag
New Member
Stellaaa!!
Posts: 29
|
Post by Boag on Sept 10, 2005 20:00:36 GMT
You'll learn someday Optimising your codes is extremely satisfying and if you code clumsily sites will run very slowly - and speed is certainly half the battle.
|
|
Llama!
Junior Member
Posts: 87
|
Post by Llama! on Sept 10, 2005 21:03:08 GMT
I know. But I am young so code shorter than you
|
|