Jump to content

[SOLVED] Determining if a table has 0 rows...


matty

Recommended Posts

I want to have an IF statment which is something like this...

 

$sel_names = mysql_query("select id, name users where userid='$userinfo[id]'");

$table = mysql_fetch_array($sel_names);

 

If ( table[name] == "0") {

echo "You have no data blah blah";

}else{

 

*Display a table with the data in*

 

What i need to know is what do i use to find out if the table has any data in it

 

Thanks, Matt

Link to comment
https://forums.phpfreaks.com/topic/40867-solved-determining-if-a-table-has-0-rows/
Share on other sites

correct or not.

 

 


<?php

$sel_names = mysql_query("select id, name users where userid=' ".$userinfo['id']." ' ");
$table = mysql_fetch_array($sel_names);

while($data=mysql_fetch_assoc($table)){

if($data['what_ever']==0){

echo "You have no data blah blah";

}else{

//what ever
}
?>

you could also use mysql_num_rows()

 

eg:

 


<?php

$sql = "SELECT * FROM <table> where userid='$userinfo[id]'";
$result = mysql_query($sql) or die(mysql_error());
$numrows = mysql_num_rows($result);

if ($num == 0) //no rows
{
echo "No Rows";
} else {
*display*
}


?>

 

 

 

I think it's better using a varialbe that you ++.

 

Example:

 

<?php

$count = 0;

while ($line = mysql_fetch_array($result, MYSQL_ASSOC))

//echo table and results

$count++;

?>

 

<?php
if ($count < 1) {
echo "<br><br>No rows were found in this table";
}
?>

Snooble

 

 

snooble that code is slow hope you no that

 

 

$count = 0;  <<< wast time?

 

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) // a big mysql_fetch_assoc bad?

 

//echo table and results

 

$count++; another time waster always let mysql do the job if it can ok.

 

?>

boo_lolly there are no right's or wrongs can you show us the text

on php where it say wright and wrong's please cheers lol

 

what i meant was, it is exactly what he is looking for. he wanted to know how to check if there were any results returned (or how many) from a mysql query... mysql_num_rows does exactly that... no need to incriment or anything. it's simple, it works. that's what it's there for. that's why it's right. not that everyone elses is wrong. i never said that. but using mysql_num_rows is most right  ;D

I see, thanks for that redarrow. I didn't know which was faster. First table i wrote is the one i still use.

 

cause i would echo out the variable :)

 

I'll have a read up on mysql_num_rows() as i've never used it. Thanks!

 

Snooble

 

@ Chris. I'm sure you'll reflect on that statement. As you'll need to link some functions to get what you want sometimes. It helps to know how they work.

 

 

 

In this post example your correct but as shown from above you dont always need to use mysql_num_rows but is best to but the same results are the same as posted on here ok.

 

there are no right's or wrongs just preferences that why were all programmers.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.