0

my problem is that I want to check in my database that if i have at least one entry in the specific table I want to disable an image.

I think that I can have something like that...

<?php
    if($query = mysql_query("SELECT name,email,city,phone FROM personal_information WHERE username='$username'"))
    {
        if(mysql_num_rows($query)=0)
        {
            <a href="cv/addcv.php"><img src="images/addcv.png" style="display: none;"> </a>
        }
        else
        {
             <a href="cv/addcv.php"><img src="images/addcv.png" style="width:120px;height:120px"> </a>
        }
    }
?>

But it does not work... Can you suggest me any solution

Waaaaat
  • 634
  • 3
  • 14
  • 29
  • `if(mysql_num_rows($query)=0)` use either `>` or `!=`. the `echo` is no where to be found – Kevin Jan 15 '15 at 13:44
  • http://php.net/manual/en/language.operators.comparison.php A comparison operator is this for example `==` – JaMaBing Jan 15 '15 at 13:45
  • 1
    **Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).** They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). **Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement)** instead, and **use [PDO](http://us1.php.net/pdo).** **[Prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)** – Jay Blanchard Jan 15 '15 at 13:45
  • please dont use mysql_ anymore, its deprecated and will not work in newer PHP-versions. use mysqli_ or PDO instead. for the question: JaMaBing's comment should be the answer. – cari Jan 15 '15 at 13:45
  • What error message do you get when this script is executed on the server? – Amit Verma Jan 15 '15 at 13:46

3 Answers3

4

Two things. First you're not doing a comparison here -

if(mysql_num_rows($query)=0) // you need != (not equal) or > (greater than) or == (equal to)

Secondly, you're not echoing your HTML markup. That should cause errors. You should either close the PHP tags or echo the markup so you get correct output. For instance:

echo '<a href="cv/addcv.php"><img src="images/addcv.png" style="display: none;"> </a>';

You should add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1); so that you see all of the errors.

Please, stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO. You're inviting SQL injection!

Community
  • 1
  • 1
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
2

It doesn't work because you didn't add the PHP end tag before your HTML. Try this:

<?php
    if($query = mysql_query("SELECT name,email,city,phone FROM personal_information WHERE id>'$id' AND username='$username'"))
    {
        if(mysql_num_rows($query)==0)
        {
?>
            <a href="cv/addcv.php"><img src="images/addcv.png" style="display: none;"> </a>
<?php
        }
        else
        {
?>
             <a href="cv/addcv.php"><img src="images/addcv.png" style="width:120px;height:120px"> </a>
<?php
        }
    }
?>
Ziga Petek
  • 3,953
  • 5
  • 33
  • 43
1

Change

if(mysql_num_rows($query)=0)

To

if(mysql_num_rows($query)==0)

Also: the mysql functions are deprecated. Please use PDO instead. If that is not an option for some reason, you can also use the mysqli functions, but PDO is recommended nowadays.

Blaatpraat
  • 2,829
  • 11
  • 23