Jump to content

fetching mysql rows with php ---- else part not working


satbir

Recommended Posts

http://www.idea-ads.com/

 

 

<?php
include("../include/commands.php");
    $sname    = $_REQUEST['sname'];
    $name    = $_REQUEST['name'];
    $pass    = $_REQUEST['pass'];
    $repass    = $_REQUEST['repass'];
    
    $query = "select * from signup where sname = '$sname'";
    $result = mysql_query("$query", $con) or die ("error in result");
    $row = mysql_fetch_array($result) or die (mysql_error());
        if ($row)
        {
            echo $row['sname'];
        }
        else
        {
            echo "No record";             // this part is not working
        }
?>

you should write it like this:

$result = mysql_query($query);

and if you want to get a row from your base:

list($row) = mysql_fetch_row($result);

EDIT: also, what are you trying to say in your if statement?  if ($row) isn't enough. If you simply want to check if you have any rows in your signup table under the $sname, do it like so:

if($row > 0) 
{ 
//do stuff here
}
else
{
//do stuff here
}


the following line is causing your script to die() when there isn't a row to fetch. remove the or die (mysql_error()) part from this line -

  $row = mysql_fetch_array($result) or die (mysql_error());

mysql_fetch_array() returns a false value when there is no row to fetch. this triggers the or die() part AND since this isn't a mysql error, mysql_error() doesn't display anything.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.