How to determine if a row has no data?

I’m trying to make a PHP installation script for an application I’m working on. I want the script to install everything in the database IF and ONLY IF the table DOES NOT contain data. Oddly enough, it’s not working despite having some data in my database.


$q = mysql_query("SELECT * FROM fs_chart_member");

if($q['ID'] == NULL || $q['ID'] == ''){//if nothing exists...
	//create_admin();
	echo 'Admin created...';
}else{//otherwise, something is in the database...
	//header('Location: index.php');
	echo 'Off to index.php...';
}

Many of you will probably advise not to use the “SELECT *”, but if nothing is installed and this file will be slated to be deleted post-installation, then it shouldn’t really matter much, right?

Sorry if this is a redundant issue. :confused:

This will tell you if the table is empty:


SELECT
    COUNT(*) AS num_of_entries
FROM
    fs_chart_number

If num_of_entries is greater then 0 then you have existing records in the table.

Thanks, but I’m still having problems with this…

Inside of install.php, I have the following function (which I think is causing this):


include 'database.php';

print_r(fs_installed());//What gives?

if(fs_installed() === 0){
	//create_admin();
	echo 'Admin created...';
}else{
	//header('Location: index.php');
	echo 'Off to index.php...';
}

And inside database.php, I have the following function:

function fs_installed(){
$num_of_entries = ‘’;
$q = “SELECT COUNT(*) AS $num_of_entries FROM fs_chart_member”;
return $num_of_entries;
}

The reason I modified that “num_of_entries” from your previous response is because I was having some problems in spite of your input and thought that maybe changing that to a variable would help. Stupid, I know, but I was desperate. Ha.

Any insight into what I’m doing wrong here is appreciated. Again, I just can’t seem to return the right value. I’m trying to use “fs_install()” to return a ZERO if there’s nothing in the database table or else return whatever number constitutes for the number of entries there are (that way, I can use it for my IF condition that checks to see if the application is installed).

For some reason unknown to me, it simply returns nothing.

You have two problems:

  1. When you put a variable containing the empty string in a string, you don’t change it.

$q holds the string “SELECT COUNT(*) AS FROM fs_chart_member”

This is not valid SQL.

  1. All assigning this string to $q does is put a string in variable $q, a location in memory. You have to send that to the MySQL server, and read the result out of the result set returned. Your function just puts a string in a variable, then returns an empty string. It doesn’t actually talk to a database.
function fs_installed() {
  //ASSUMES YOU HAVE OPENED A DATABASE CONNECTION ALREADY
  
  $q = "SELECT COUNT(*) AS `num_of_entries` FROM fs_chart_member";
  $result = mysql_query($q) or die(mysql_error());
  $row = mysql_fetch_assoc($result);

  return $row['num_of_entries'];
}

Thanks for the wake-up, Dan. You cleared a lot up. One last thing, though: is it always necessary to include the “OR DIE” stuff after each query?

A good friend of mine used to say:
“necessary? nothing is necessary except for going to the toilet and dying”

Just wanting to say that you can choose to put it or not.
But Good Practice indicate to put a die statement.
(or in more complicated version a try and catch)

This always prevents your script from doing unexpected things when something is wrong with the DB, plus it helps with debugging.

Imagine that you don’t know the problem lies in the DB connection, it can take you a long tile before you will notice it went wrong there…

my small 2 cents

Ha. Nice…

Anyway, thanks for the input. :slight_smile: Makes sense.