0
require("conn.php");
$sql = "select * from pemohon where kp_baru='$kp';";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
    if($row == TRUE)
    {
     echo $row['kp_baru']; // can display
    }
    else
    {
        echo "No KP tiada didalam pangkalan data."; // can't display
    }
}
4
Contributors
9
Replies
35
Views
4 Years
Discussion Span
Last Post by dina85
0

$sql = "select * from pemohon where kp_baru='$kp';";
the first semi-colon (;) is un-neccessary.
Try

if($row['kp_baru']=='')
{
 echo "No KP tiada didalam pangkalan data."; 
}
else
{
    echo $row['kp_baru']; 
}

instead.

0

I'm not certain, but I think your $row will always be true. It contains an array with the result set from your query.
You need to select a value in each row of your query to check.
Another tip - try and avoid SELECT * FROM ... Rather select specific fields. That way there's less data being retrieved from the database and the query will run faster.

SELECT field1, field2, field3, `kp_baru` FROM pemohon where kp_baru='$kp';
...
while($row = mysql_fetch_array($result)) 
{
    if (field2 == requiredvalue) 
    {
         echo $row['kp_baru'];
    }
    else
    {
        echo "No KP tiada didalam pangkalan data.";
    }
}

I hope this helps... :-)

0

Then have you checked if there is something wrong with conn.php? Also make sure your error reporting is turned on. In the first line of your root script, put this line to do that:

error_reporting(E_ALL ^ E_NOTICE);

Edited by minitauros

0

Aaaah I think I understand what the problem is now. I thought nothing was displaying at all :). Try this if you will:

require("conn.php");
$sql = "select * from pemohon where kp_baru='$kp';";
$result = mysql_query($sql);

$has_results = false; // To be validated below.

while($row = mysql_fetch_array($result))
{
    // If the loop can be executed at least once, it means that results have been found.
    $has_results = true;

    // Display the row.
    echo $row['kp_baru'];
}

if(!$has_results)
{
    // The while loop has never been executed. No results were found.
    echo "No KP tiada didalam pangkalan data.";
}

Edited by minitauros

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.