Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder thindrakhya's Avatar
    Join Date
    Jun 2007
    Posts
    90
    Thanks
    8
    Thanked 0 Times in 0 Posts

    dealing with null values from database

    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

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder thindrakhya's Avatar
    Join Date
    Jun 2007
    Posts
    90
    Thanks
    8
    Thanked 0 Times in 0 Posts
    $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

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    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
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    New Coder thindrakhya's Avatar
    Join Date
    Jun 2007
    Posts
    90
    Thanks
    8
    Thanked 0 Times in 0 Posts
    thanks dude , i got it

  • #6
    New Coder thindrakhya's Avatar
    Join Date
    Jun 2007
    Posts
    90
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Got it!!!
    Last edited by thindrakhya; 08-30-2007 at 07:36 PM.

  • #7
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.

    Then you can do this:

    PHP Code:
    $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).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •