php - Can't connect on database on my localhost -
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 , when execute , try connect database located on pc error:
warning: mysql_connect() [function.mysql-connect]: can't connect mysql server on '109.60.110.255' (4) in /home/a6859995/public_html/zavrsni/connect.php on line 12
how can fix that?
i recommend use pdo 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 in script this:
require_once("class_pdo.php"); $pdo = class_pdo::dbfactory(); $stmt = $pdo->prepare("select * `tablename` id = :id "); $stmt->bindvalue(":id", $id, pdo::param_int); $stmt->execute(); [...]
Comments
Post a Comment