View Full Version : dealing with null values from database

08-30-2007, 06:18 PM

i am using query

select max(ID) + 1 from table1

Now my table contains no entry so i am not able to proceed

i tried using

then ID = 1

but this loop is not working

Can anyone help as i always have this probelm every time the data query returns the null value my lop


always gets executed. As query was not executed this loopshold not have executed

Any help

08-30-2007, 06:31 PM
You would need to post your actual code to get any help with why it is not working. 101 different people could write 101 different pieces of code to accomplish what you are trying, and each one could have a different problem or logic error with it that prevents it from working.

08-30-2007, 06:41 PM
$query = " SELECT MAX(user_id)+1 from users_main";
$resultSet = mysql_query($query) or die("Execution of failed 11 ");

$getUser_ID = mysql_fetch_array($resultSet);

$query_users_main = "insert into users_main
(username, password,user_id,date_of_joining)

$resultSet_users_main = mysql_query($query_users_main) or die("Execution of failed users_main");

This is my query
Now as firstquery returns no result beacuse the table was empty , the loop

if(resultSet) should not have executed but my loop still runs and give erros

08-30-2007, 06:48 PM
A query is successful, even if the result set contains zero rows (for example checking if a username/password pair exists in a database.) Your existing code is only checking if the query failed/succeeded (syntax error, no database selected, no connection to a database server...)

To check how many rows are in a result set, following a successful query, use the mysql_num_rows() function - http://php.net/mysql_num_rows

08-30-2007, 06:57 PM
thanks dude , i got it

08-30-2007, 07:28 PM
Got it!!!

08-30-2007, 10:02 PM
A more advanced solution (for those who are up to it), is to wrap the result set in a class, and have that class implement the internal PHP interface Iterator (http://us3.php.net/manual/en/language.oop5.iterations.php).

Then you can do this:

$result = /* however you want to query your database and create the new resultset class */;

foreach ($result as $row) {
/* handle your rows here */

I don't think it gets cleaner than that (not to mention that once you encapsulate your resultset, it can be made so it is not database specific).