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
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
}
?>

Link to comment
Share on other sites

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*
}


?>

 

 

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

 

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Perhaps not "right or wrong" but the mysql_num_rows() function is specifically for this task, so why bother using anything else thats more complex and less efficient? The name of the function itself tells me that thats the best one to use :P

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.