I am developing a dynamic website and I'm using both local and remote host. I've tried to implement a code in which I stablish connection first with the localhost. If connection to the locahost is false or can't be established, I go to the second connection (remote server or host). But this time, I want to do something different. Now I want to know if it's possible to connect to two hosts/servers at the same time with no errors. The databases and tables are the same. I'm asking this, because I just want to avoid sql backup. What do you say? Moreover, I want to use the same variable $pdo, so I don't need to change or repeat all connections to the tables.
This is my current code:
<?php
try {
//Local Host - XAMPP
$dsn = 'mysql:host=localhost;dbname=bananas';
$user = 'root';
$pw = '';
$sessionpath = 'C:/xampp/tmp';
$pdo = new PDO($dsn, $user, $pw);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) { //$e
//echo 'Error: '.$e->getMessage();
if(!isset($pdo) || $pdo == false){
try{
//Remote Host
$dsn = 'mysql:host=bananas123;dbname=bananas';
$user = 'mybigbanana';
$pw = '6969';
$sessionpath = '/php_sessions';
$pdo = new PDO($dsn, $user, $pw);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e) {
echo 'Error: '.$e->getMessage();
}
}
}
?>
Note: I don't want to do something like having two variables for each connection, like, for example, $pdo and $pdo2. I want only one variable for both connections, which is $pdo.
Related
I'm using a mysql database. I would like to connect to it with the script i wrote :
<?php
function getDatabase() {
$host = 'localhost:3306';
$db = 'freya';
$login = 'root';
$pw = 'helloitsme';
try {
return new PDO('mysql:host='.$host.';dbname='.$db.';charset=utf8', $login, $pw);
} catch (Exception $e) {
die('Erreur : '.$e->getMessage());
}
}
$db = getDatabase();
I have seen that this error is recurent but none of the solutions worked for.
I checked the my.cnf, and i'm sure that i'm using the port where the mysql db is.
I'm also sure that the db name, the login and the password are correct, because i'm using them to reach the db with the shell.
What could be the problem ?
You don't need to specify the port as 3306 is default for mysql, but if you do, the correct connection string is
'mysql:host=localhost;port=3306 ...'
I have a local web application based on PHP that uses an online Mysql server.
I need to know when I can reach that server (when do I have internet connection, or when the server is shutdown).
I've tried this
$conn = new mysqli('hostname','username','password','db');
if($conn->connect_error){
//offline action
}
else{
//online action
}
which works but it lasts for about a minute to determine if server is reachable or not.
I was wondering if there is any other solution for this that lasts less than 3 or 4 seconds.
Check if the server returns a response first, you can use fsockopen() but other methods are also available like using the operating system shell so you can ping.
function is_connectable(string $host, int $port=80, int $timeout=3){
if($fp = #fsockopen($host, $port, $errno, $errstr, $timeout)){
return fclose($fp) || true;
}
return false;
}
$host = "mysql.someserver.com";
$port = 3306;
$user = "username";
$pass = "mypassword";
$mydb = "mydatabase";
if(is_connectable($host, $port)){
try{
$pdo = new PDO("mysql:host=$host;port=$port;dbname=$mydb", $user, $pass);
} catch(Exception $e){
die($e->getMessage());
}
} else {
die("mysql server '$host' is down");
}
There is also use the timeout from the database driver directly:
PDO::ATTR_TIMEOUT for PDO
MYSQLI_OPT_CONNECT_TIMEOUT for MySQLi
what is the best way for create a connection between PHP and SQL server that are seperate?(two server: server a SQL and server b PHP)
notice that I use wamp.
I read some articles like below but I want to know is there any new idea?
I test this code that works perfectly:
try{
$user = 'user';
$password = 'pass';
$server="localhost";//or server IP
$database="database";
$conn = odbc_connect("Driver={SQL Server};Server=$server;Database=$database;", $user, $password);
} catch (PDOException $e) {
echo "Failed : " . $e->getMessage() . "\n";
exit;
}
I use PDO_ODBC Method:
1- Install ODBC on server that has wamp and enable PHP_PDO_ODBC extension on your wamp
2- Use this code that supports UTF-8:
try{
$hostname = "IP";
$dbname = "database";
$username = "username";
$pw = "password";
$pdo = new PDO ("odbc:Driver={SQL Server Native Client 10.0};Server=$hostname;Database=$dbname; Uid=$username;Pwd=$pw;");
} catch (PDOException $e) {
echo "Failed : " . $e->getMessage() . "\n";
exit;
}
$query = $pdo->prepare("select field_name from table");
$query->execute();
for($i=0; $row = $query->fetch(); $i++){
echo iconv("CP1256","UTF-8", $row['field_name'])."<br>";
}
3- replace these Items with yours:
IP-database-username-password-field_name-table
4- sometimes you need use "SQL SERVER" instead of "SQL Server Native Client 10.0" and sometimes you need use "IP,port" instead of "IP" and sometimes you need use "ISO-8859-6" instead of "CP1256".
From the PHP manual: http://php.net/manual/en/pdo.construct.php
Example #1 Create a PDO instance via driver invocation
<?php
/* Connect to a SQL Server database using driver invocation */
$dsn = "sqlsrv:Server=12345abcde.database.windows.net;Database=testdb", "UserName#12345abcde", "Password";
try {
$dbh = new PDO($dsn);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
Just change the HOST ip to the IP and port of your mysql server.
I am currently running a web application with thousands of visitors and users connecting at the same time. I have a lot of "sleeping" connections in WHM (like 50-65).
I think I am using persistent connections, but my question is, how can I disable this?
This is how I connect to the database:
$dbh = null;
$class = 'PDO';
$database = $settings['database']; //Get the database login information. It is stored in an array();
global $database;
try {
$dsn = "mysql:host=" . $database['host'] . ";dbname=" . $database['db'];
$dbh = new PDO($dsn, $database['user'], $database['pass'], array(PDO::ATTR_PERSISTENT => true));
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
//echo 'Connected to Database<br/>';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
I keep getting this error:
SQLSTATE[HY000] [1040] Too many connections
Even though I have updated the max_connections in my.cnf to 5000
Can someone please help me out?
How would I connect to the demo phpmyadmin server in php? My code looks like this.
<?php
$host = 'http://demo.phpmyadmin.net/STABLE/';
$dbname = 'shubham';
$user = 'root';
$pass = '';
// Attempt to connect to database.
try {
$DBH = new PDO("mysql:host={$host};dbname={$dbname}", $user, $pass);
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
but I get this as my error
QLSTATE[HY000] [2005] Unknown MySQL server host 'http://www.demo.phpmyadmin.net/STABLE/' (1)
You seem to be confusing two things:
the demo phpMyAdmin front-end that is backed by a db server and db/schema
the db server and schema itself
PDO needs the latter, the db server itself.
Inspecting the front-end code of the demo, I don't see anything in there that would give us the actual connection details for the db server. And that's as I would expect: I find it hard to believe that the makers/maintainers of the phpMyAdmin demo would make their actual db server available for public remote connections.
change your hostname from
$host = 'http://demo.phpmyadmin.net/STABLE/';
to your original remote hostname like eg $host = 'ukld.db.5510597.hostedresource.com';
MySQL does not work on HTTP
<?php
$host = 'demo.phpmyadmin.net';
// High chances that this is NOT your mysql hostname.
// It will not even by like /STABLE/ as you mentioned it.
$dbname = 'shubham';
$user = 'root';
$pass = '';
// Attempt to connect to database.
try {
$DBH = new PDO("mysql:host={$host};dbname={$dbname}", $user, $pass);
} catch(PDOException $e) {
echo $e->getMessage();
}
?>