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();
}
}