Jump to content

[SOLVED] if table is empty in a mysql db.....?


dsaba

Recommended Posts

If i have a table that is completely empty in a mysql database

meaning NO ROWS

 

and I run a query that selects * on that empty table

 

will it return false, NULL, empty, or 0

 

because i have a particular situation where this is happening and I ran if statements on all of those and nothing checks out!

Link to comment
https://forums.phpfreaks.com/topic/42188-solved-if-table-is-empty-in-a-mysql-db/
Share on other sites

i know this sounds like a stupid question right?

I know it is I wouldn't ask it because I THOUGHT i knew the answer, but apparantly my script is behaving differently

 

here is the code:

$pageip = $_SERVER['REMOTE_ADDR'];
$pagedate = date("Y-m-d");
$pagedatetime = date("Y-m-d g:i:s");

include 'connectmysql.php';

$pageviewquery = mysql_query("SELECT * FROM pageviews WHERE pageip='$pageip' AND pageid='25' ORDER BY pagedatetime DESC LIMIT 0, 1");
$pageviewqueryrow = mysql_fetch_array($pageviewquery);

if ($pageviewquery == FALSE) {
echo "query is false";
}
else {
echo "query is not false";
}

if ($pageviewquery == NULL) {
echo "query is null";
}
else {
echo "query is not NULL";
}

if ($pageviewquery == 0) {
echo "query is 0";
}
else {
echo "query is not 0";
}


 

 

it returns query is not false, query is not 0, and query is not null

when i echo the query it returns Resource id #3

when I echo mysql_error(); it returns nothing

when I echo mysql_errno(); it returns 0

 

so how can I not have an error? and then also have an error number?

 

I KNOW THIS TABLE IS EMPTY, so whats going on???? thanks for your help

here is a pic from my phpmyadmin

notfalsemm6.png

 

here is the exact code i'm using:

$pageip = $_SERVER['REMOTE_ADDR'];
$pagedate = date("Y-m-d");
$pagedatetime = date("Y-m-d g:i:s");

include 'connectmysql.php';
$pageviewquery = mysql_query("SELECT * FROM pageviews WHERE pageuserid='45'");
if ($pageviewquery == FALSE) {
echo "query is false";
}
else {
echo "query is not false";
}
mysql_close();

 

this returns QUERY IS NOT FALSE

as you can see in my image there is not row with pageuserid= 45

what is going on?

$pageviewquery = mysql_query("SELECT * FROM pageviews WHERE pageuserid='45'");

$result = mysql_fetch_array($pageviewquery);
echo $result;

if ($result == FALSE) {
echo "query is false";
}
else {
echo "query is not false";
}

 

You need to use mysql_fetch_array() to get the results. Otherwise, it is returning a Resource Id.

 

Jeremy

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.