0

Hello How can I verify If there any information column or not In Table users Here is my full script

<?php
$username = "root"; 
$password = ""; 
$hostname = "localhost"; 
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); 
$selected = mysql_select_db("demo1",$dbhandle) or die("Could not select demo1"); 
//$result_name = mysql_query(" SELECT `name` FROM `users` WHERE `name` != '' ");
$result_name = mysql_query("SELECT name FROM demo1. users");
while($row_name = mysql_fetch_array($result_name)) { 
//echo "NAME=" . $row_name['name'] . "<br/>";
if ($row_name == null)
echo "NAME= no name <br/>";
else
{
echo "NAME=" . $row_name['name'] . "<br/>";
}
}
?>

I get the result but when there is no name i.e. name is null I want to output "NAME= no name" but I just get "Name =" http://s23.postimg.org/ky8xcoy4b/Untitled_1.jpg Thanks to anyone who can help

4 Answers 4

2

Here you are using $row_name as an array, so you should use $row_name['name'] in If condition like,

if (is_null($row_name['name']) or trim($row_name['name'])=="")
    echo "NAME= no name <br/>";

Read is_null()

Sign up to request clarification or add additional context in comments.

Comments

1

Since you have the following code,

while($row_name = mysql_fetch_array($result_name))

If no information was found it will not enter the loop, so you don't need to check inside the loop if no information was found.

Comments

0

Replace if ($row_name == null) to

if ($row_name == null || $row_name == "")

Comments

0

Just place your while loop inside the following if statement to check if any information is returned from your query:

if(mysql_num_rows($result_name)>0)
{


}

It basically gives the number of rows of the result which should obviously be greater than zero if there is any information retreived

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.