-1

I have php script:

 <?php

$host     = $_GET['host'];
$username = $_GET['username'];
$pass     = $_GET['pass'];

$con = mysql_connect($host, $username, $pass);
if (!$con) {
    echo 'Connection failed!';
} else {
    echo 'Connected successfully!';
}
mysql_close($con);

?>

running on remote server and when I execute it and try to connect to database located on my PC i get an error:

Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on '109.60.110.255' (4) in /home/a6859995/public_html/zavrsni/connect.php on line 12

How can I fix that?

DynamicsNinja
  • 159
  • 1
  • 3
  • 16

4 Answers4

1

I recommend to use pdo like this:

class_config.php:

class class_config {

    public static $db_host = 'localhost';
    public static $db_name = 'yourdbname';
    public static $db_user = 'youruser';
    public static $db_pass = 'yourpass';

}

class_pdo.php:

require_once "class_config.php";
class class_pdo {

    public static function dbFactory() {
        $host = class_config::$db_host;
        if(strpos($host,":") !==false) {
            $parts = explode(":",$host);
            $hostname = "unix_socket=".$parts[1];
        } else {
            $hostname = "host=$host";
        }
        $user = class_config::$db_user;
        $pass = class_config::$db_pass;
        $dbase = class_config::$db_name;        
        $pdo = new PDO("mysql:$hostname;dbname=$dbase", $user, $pass);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);        
        return $pdo;
    }
}

use it in your script like this:

require_once("class_pdo.php");

$pdo = class_pdo::dbFactory();

$stmt = $pdo->prepare("SELECT * FROM `tablename` WHERE id = :id ");
$stmt->bindValue(":id", $id, PDO::PARAM_INT);
$stmt->execute();

[...]
steven
  • 4,821
  • 2
  • 25
  • 55
0

Try This instead: mysql_connect() is deprecated so use mysqli_connect...

$host     = $_GET['host'];
$username = $_GET['username'];
$pass     = $_GET['pass'];
$databae  = $_GET['database'];

$con = mysqli_connect($host, $username, $pass, $database);
if (mysqli_connect_errno()) {
    echo 'Connection failed!';
} else {
    echo 'Connected successfully!';
}
mysql_close($con);

?>
TipuZaynSultan
  • 763
  • 4
  • 15
0

Though mysql_connect() is deprecated try using PDO.. I am just presenting it in mysql_connect for you..

$host     = $_GET['host'];
$username = $_GET['username'];
$pass     = $_GET['pass'];
$database  = $_GET['database'];
$connect=new connect($host,$username,$pass,$database);
class connect{

     function __construct($host,$user,$password,$db_name){

        mysql_connect($host,$user,$password) or die("Connection error");

        mysql_select_db($db_name);

        $error=mysql_error();

        if (!empty($error))

        {

            echo $error;

        }

    }

}
Mr AJ
  • 1,567
  • 3
  • 26
  • 49
-1

Thanks everyone on help. Main problem were privileges on MySQL database, but PDO usage helped me to understand how it's to be done these days, sorry on n00b code at start :D

DynamicsNinja
  • 159
  • 1
  • 3
  • 16