Testing if a DB query finds something or not.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LRW

    Testing if a DB query finds something or not.

    I'm trying to make an If/Else option in the case a database query comes back
    with nothing. But when I use the code included below, it always comes back
    saying there is no result.
    I'm guessing it's because it's asking if there are any results to the query
    after it's already finished and the array created.
    How can I get it to say basically: If there are no database records fitting
    the query, then do this...otherwis e do this?
    Thanks!!
    Liam

    $query_RS_con = "SELECT * FROM $conTBL WHERE $field = \"$item\"";
    $RS_con = @mysql_query($q uery_RS_con, $connection) or die("Couldn't query:
    " . mysql_error());
    while ($row_RS_con = mysql_fetch_arr ay($RS_con)) {
    $procid = $row_RS_con['uid'];
    $procid2 = $row_RS_con['id'];
    $procpass = $row_RS_con['cont_password'];
    }
    if (!($row_RS_con) ) {
    $statusmsg = "Your Username was not found. Please go back and try
    again.<br><br>< A HREF='javascrip t:history.go(-1)'>Click Here to go back and
    try again.</a><br>(You may need to click the ACCESS menu option
    twice.)<br>";
    } else {
    if (($pass1) == ($procpass)) {
    header("Locatio n:
    http://www.url.org/contact/change.php?uid= $procid$procid2 ");
    } else {
    $statusmsg = "Your Password did not match your Username. Please go back
    and try again.<br><br>< A HREF='javascrip t:history.go(-1)'>Click Here to go
    back and try again.</a><br>(You may need to click the ACCESS menu option
    twice.)<br>";
    }
    }


  • Michael Fuhr

    #2
    Re: Testing if a DB query finds something or not.

    "LRW" <druid@NOSPAHMc elticbear.com> writes:
    [color=blue]
    > I'm trying to make an If/Else option in the case a database query comes back
    > with nothing. But when I use the code included below, it always comes back
    > saying there is no result.
    > I'm guessing it's because it's asking if there are any results to the query
    > after it's already finished and the array created.[/color]

    Almost right -- see below.
    [color=blue]
    > How can I get it to say basically: If there are no database records fitting
    > the query, then do this...otherwis e do this?
    >
    > $query_RS_con = "SELECT * FROM $conTBL WHERE $field = \"$item\"";
    > $RS_con = @mysql_query($q uery_RS_con, $connection) or die("Couldn't query:
    > " . mysql_error());
    > while ($row_RS_con = mysql_fetch_arr ay($RS_con)) {
    > $procid = $row_RS_con['uid'];
    > $procid2 = $row_RS_con['id'];
    > $procpass = $row_RS_con['cont_password'];
    > }[/color]

    The above while loop is executed until $row_RS_con becomes false.
    Then you do this:
    [color=blue]
    > if (!($row_RS_con) ) {[/color]

    $row_RS_con should always be false here -- otherwise you'd still
    be in the while loop.

    You can use mysql_num_rows( ) to find out how many rows a SELECT
    query returned.

    --
    Michael Fuhr

    Comment

    Working...