Jump to content

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


Go to solution Solved by mac_gyver,

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
        }
?>

Edited by satbir

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
}


Edited by Matic
  • Solution

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.