I'm having trouble connecting to a MySQL database on my host OS (Win 7) from my Oracle VirtualBox running Ubuntu. I wrote this php code in order to connect but I keep receiving an error connecting:
<?php
$dsn = 'mysql:host=192.168.1.9;dbname=finance';
$db_username = 'sdb_user';
$db_password = 'password';
try {
$db = new PDO($dsn, $db_username, $db_password);
}catch (PDOException $e) {
$error_message = $e->getMessage();
include('../errors/database_error.php');
exit();
}
$query = 'SELECT * FROM books';
$books = $db->query($query);
foreach ($books as $book) {
echo $book['title'];
}
?>
I know the code works, because I can get it to run from my Windows 7 host OS using localhost instead of the IP address, and I know that both Linux on VirtualBox and Windows 7 are on the same subnet because I can successfully ping the other from each side.
I'm fairly certain that I opened port 3306 correctly on the windows side also, but I have no idea how to check if that's the problem or not.
What else should I be doing to get the code to connect to the MySQL db?
EDIT: The problem was that the port was not open for remote connections. After I opened the port I got the error message stating I was not allowed to connect to the sql server. From there I created a new user on the Windows MySQL with the username "home" and password "home" and was able to connect to it using:
$ mysql -h 192.168.1.9 -u home -p
However, when I run my file, which now reads:
$dsn = 'mysql:host=192.168.1.9;dbname=finance';
$db_username = 'home';
$db_password = 'home';
I still get the 'database error'
Any suggestions?
Related
im having a problem, my project is working in my windowsOS, and i try it to transfer in Ubuntu16.04 Operating system, with static ip address. now when i trying to access the project it keep saying Database Connection Error!
<?php
$db_password = "Welcome1";
$server = "192.168.2.218";
try{
$pdo_conn = new
PDO("mysql:host=$server;dbname=boldr","phpmyadmin","
{$db_password}");
} catch (PDOException $e){
die("Database Connection Error!");
}
$conn = mysqli_connect("192.168.2.218","phpmyadmin","{
$db_password}") or die(mysqli_error($conn));
mysqli_select_db($conn,'boldr') or die("cannot select DB");
?>
is your database on the same server? If yes try 127.0.0.1 or localhost.
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 ...'
When trying to connect to my mssql database, I get the error
"SQLSTATE[01002] Adaptive Server connection failed (severity 9)"
Below is the php code I'm running
<?php
try {
$hostname = "hostname.database.windows.net";
$port = 1433;
$dbname = "database-dev";
$username = "dbuser";
$pw = "dbpassword";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
$stmt = $dbh->prepare("select name from master..sysdatabases where name = db_name()");
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
unset($dbh); unset($stmt);
?>
And below is my odbc.ini, odbcinst.ini and freetds.conf, you can see my phpinfo() here ("http://wingedw.com/matiks/connect.php") the driver is set to freetds and the pdo and pdo_dlib modules have been added to php 5, any clues as to why im getting error, im sure the credentials are right.
odbc.ini
[MSSQLServer]
Driver = FreeTDS
Description = Any description
Trace = No
Server = servername
Port = 1433
Database = dbname
wTDS_Verison = 7.1
odbcinst.ini
[FreeTDS]
Driver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount = 1
freetds.conf
[global]
# TDS protocol version
; tds version = 7.1
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 100
; connect timeout = 100
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
# A typical Sybase server
[egServer50]
host = symachine.domain.com
port = 5000
tds version = 7.1
# A typical Microsoft server
[MSSQLServer]
host = servername
port = 1433
tds version = 7.1
Turns out everything was configured correctly, the issue was using dblib over odbc, see code below.
try {
$hostname = "hostname.database.windows.net";
$port = 1433;
$dbName = "databasename";
$dbuser = "dbuser#hostname";
$dbpass = "password";
$dbh = new PDO('odbc:DRIVER=FreeTDS;SERVERNAME=mssql;DATABASE=' . $dbName,
$dbuser, $dbpass);
//echo "COnnected";
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
2021 - BACK TO THE FUTURE:
I know that this post is old, but my reply can point a desperate soul in the right direction:
You are using odbc and the command "isql" works just fine
You are using FreeTDS and the command "tsql" works just fine
Trouble is running the php scripts that have calls to odbc via FreeTDS -> keeps giving errors
Kindly read:
https://www.linuxquestions.org/questions/blog/tix-592494/freettds-libiodbc-iodbc-php-7-4-21-on-slackware-current-5-13-5-38621/
Cheers happy souls!
I am attempting to connect to a mysql database using the c9.io development environment. I have followed their documentation and have seen multiple links, 1, 2 and 3.
I verified the mysql service is running. I also verified the PDO extension was installed via phpinfo(). Here is my current code:
$ip = getenv("REMOTE_ADDR");
$port = '3306';
$user = "username";
$db = "c9";
try{
$con = new PDO("mysql:host=$ip;port=$port;dbname=$db;charset=utf8",$user,"");
}
catch(Exception $e){
echo $e->getMessage();
}
I get the error Can't connect to MySQL server on '10.240.x.x' (111)
If i try localhost as host, I get the error Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I also followed a comment from the second link above: echo $IP in the terminal which returns 0.0.0.0
Any assistance appreciated.
You were on the right track. On https://docs.c9.io/setting_up_mysql.html it says use $IP for host. You can use getenv("IP") instead or use its value: 0.0.0.0. That should work.
Please try something like:
$dbname = 'c9';
$ip = getenv('IP');
$user = getenv('C9_USER');
mysql_connect($ip, $user, '') or die('Could not connect to mysql');
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();
}
?>