...

View Full Version : dealing with null values from database



thindrakhya
08-30-2007, 05:18 PM
hi


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

if(!resultSet)
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

if(resultSet)
{
..............
}

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

Any help

CFMaBiSmAd
08-30-2007, 05: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.

thindrakhya
08-30-2007, 05: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);

if($resultSet)
{
$query_users_main = "insert into users_main
(username, password,user_id,date_of_joining)
values
('".$_REQUEST["txtUsername"]."','".$_REQUEST["txtPassword1"]."',".$getUser_ID[0].",'".$today."')";

$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

CFMaBiSmAd
08-30-2007, 05: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

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

thindrakhya
08-30-2007, 06:28 PM
Got it!!!

aedrin
08-30-2007, 09: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).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum