-1

I have this code:

$ip_adresa = $_SERVER['REMOTE_ADDR'];
$query = mysql_query("SELECT * FROM IP WHERE IP='$ip_adresa'");

if(mysql_num_rows($query) > 0) {
    echo 'Do something';
} else {
    echo 'Do something';
}

I have error

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in...

Qirel
  • 23,315
  • 7
  • 41
  • 57
mark22
  • 19
  • 3
    You should not be using `mysql_*` anymore you should port to `MySQLi` or `PDO`. – Script47 Oct 13 '15 at 21:48
  • I'd recommend you checkout http://laravel.com/ – Michael J. Calkins Oct 13 '15 at 21:50
  • Re-check your query. Most probably you won't have the same column and table name. – Akshay Oct 13 '15 at 21:59
  • 1
    **WARNING**: If you're just learning PHP, please, do not learn the obsolete `mysql_query` interface. It's awful and is being removed in future versions of PHP. A modern replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/). A guide like [PHP The Right Way](http://www.phptherightway.com/) can help explain best practices. Always be absolutely **sure** your user parameters are [properly escaped](http://bobby-tables.com/php) or you will have severe [SQL injection bugs](http://bobby-tables.com/). – tadman Oct 13 '15 at 22:04

2 Answers2

3

Function mysql_query returns FALSE on error. Your SQL statement appears to be incorrect, it is unclear what "IP" is, a column or a table.

Furthermore, as mentioned by Script47:

Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysqli_query() PDO::query()

http://php.net/manual/en/function.mysql-query.php

Uber Bot
  • 511
  • 3
  • 8
0

First of All mysql_* is deprecated in php from php 5.3 or 5.2 . So it's a bad idea to use that .

You should use PDO . Why PDO ? mysqli_* may be faster than PDO but PDO gives access to use wide range of databases . Most of the Frameworks use PDO because by this is the way they can give developers access to 12 databases in php very easily .

if you use PDO your query will be like this:

<?php


/**
*  Class description
*/
Class Connection {

private $host;
private $host_user;
private $host_pass;
private $db_name;
public  $db;

    function __construct(){
        # code...

        try {

        $this->host             = 'localhost';
        $this->host_user        = 'root';
        $this->host_pass        = '';
        $this->db_name          = 'your_database';

        //Main Connection script
        $this->db = new PDO('mysql:host='.$this->host.'; dbname='.$this->db_name, $this->host_user, $this->host_pass);
        $this->db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        $this->db->setAttribute( PDO::ATTR_EMULATE_PREPARES, 0 );
        $this->db->exec("SET CHARACTER SET utf8");

        } catch (PDOException $err){

            echo "harmless error message if the connection fails";
            $err->getMessage() . "<br/>";
            die();  //  terminate connection

        }

    }


}



?>

// Using namespace is a great choice but i'm doing that in a general your script.php

include_once 'connection_class.php';

class query extends Connection {

 // You need to call into your fille .. thant's it . safe query and safe // result :)
  public static function pdoQuery(){

      $query = $this->db->prepare("SELECT * FROM IP WHERE IP= :ip");
      $query->bindParam(':ip',$ip);
      $query->execute();
      return $query->rowCount();

  }

}
Aniruddha Chakraborty
  • 1,810
  • 1
  • 19
  • 31
  • Why use `PDO`? What are the benefits? Do you have any links you can provide to help the OP who is obviously a beginner. Does the OP know what classes are? Are they even familiar with OOP? What is a `namespace`? You should technically bring yourself down to the OP's level. – Script47 Oct 13 '15 at 22:15
  • Why use PDO? http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059 Benefits: http://stackoverflow.com/questions/6119097/secury-benefits-of-php-pdo-vs-mysql If you are beginner then try follow these first: https://www.youtube.com/watch?v=iCUV3iv9xOs&list=PL442FA2C127377F07 If you ask all those questions to google you will find your answers :D ... – Aniruddha Chakraborty Oct 13 '15 at 22:25
  • I'm talking from the perspective of a beginner, they were using `mysql_*` and now all of a sudden they are being told to change. So I'm simply saying throwing a class at them with no proper explanation or link within the answer isn't going to help them much. Add those link to your answer maybe? – Script47 Oct 13 '15 at 22:29
  • I updated the answer .. is this ok now ? :D @Script47 – Aniruddha Chakraborty Oct 13 '15 at 22:37