1

How script php can check If there is an empty column in my database in Table the_ad Where column image_link For now my script can not check if there is an empty column What did I do wrong Here is my full code

<?php
$username = "root";
$password = "";
$hostname = "localhost"; 
$dbhandle = mysql_connect($hostname, $username, $password) 
or die("Unable to connect to MySQL");

$selected = mysql_select_db("test",$dbhandle) 
or die("Could not select test");

//fetch tha data from the database
$result_image_link = mysql_query("SELECT image_link FROM test. the_ad");
while ($row_image = mysql_fetch_array($result_image_link)) {
if (empty($result_image_link)) {
    echo "no image_link";
}
else
if (isset($result_image_link)) {
echo "PIC=".$row_image{'image_link'}."<br>";
}
}
mysql_close($dbhandle);
?>

I see the list of column likethis

    1. PIC=
    2. PIC=
    3. PIC=pic3.jpg
    4. PIC=pic4.jpg
    5. PIC=pic5.jpg
    6. PIC=pic6.jpg
    7. PIC=pic7.jpg

Why column one and two Not show the message

Thanks to anyone who can help

1
  • SELECT image_link FROM test.the_ad WHERE image_link<>'' AND image_link IS NOT NULL Commented Mar 11, 2020 at 8:05

3 Answers 3

2

Use this query: SELECT image_link FROM test WHERE image_link != NULL

Your check is concerning the whole amount of results. Only if there are no results returned from your query, the empty function will return false. You can use the query above or change your if statement to this !empty($row_image['image_link']).

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

3 Comments

+1. I think its better to change the query itself rather than getting the values not needed (and then checking for them) even if they are NULL values. This approach may be faster too.
If course changing the query will be better, but when you want to show a page with (for example) a list of users with their profile image (if existing), the other approach will be more useful.
I see what you are saying. This approach could be combined with the updated query when say you want to display a default image if no image exists. So in OP's case, you need a php check in addition to filtering at MySQL level.
2

You can add SQL statement WHERE `image` != ''

$result_image_link = mysql_query("
     SELECT `image` 
     FROM `table` 
     WHERE `image` != ''
");

while($row_image = mysql_fetch_array($result_image_link))
{
    echo "PIC=" . $row_image['image'] . "<br/>";
}

OR

while ($row_image = mysql_fetch_array($result_image_link)) 
{
    echo (isset($row_image['image']) AND $row_image['image'] != '')
        ? "PIC=" . $row_image['image'] . "<br/>" 
        : 'no picture <br/>';
}

Comments

0

You can simply use SQL operators IS NULL if you set your DB columns default value as NULL.

Hope this helps.

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.