Database error in Ubuntu - php

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.

Related

Phpmyadmin in remote pc is accessible, but can't connect it with PHP PDO

I'm able to connect phymyadmin of remote pc, but when i try to connect to remote db, I'm getting connection refused error.
I have seen similar kind of question, but not yet answered also it is not active now.SQLSTATE[HY000] [2002] Connection refused with right port
<?php
$servername = "192.168.1.12";
$username = "root";
$password = "root";
try {
$conn = new \PDO("mysql:host=$servername;dbname=my_db", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(\PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
In remote pc, bind address in mysql configuration file was set to 127.0.0.1 instead of 192.168.1.12

I have the error: SQLSTATE[HY000] [2005] Unknown MySQL server host

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 ...'

php database help, database hosted on separate server than web host

Hi I am making a login/register database. I am not hosting the database on the same server so how do I make it point to my separate domain? Also if you can tell me what the root is pointing to... Thx
Here are my files:
db.php
<?php
$connection = mysql_connect('localhost', 'root', '');
if (!$connection){
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db('register');
if (!$select_db){
die("Database Selection Failed" . mysql_error());
}
?>
Links (my scripts and original scripts): https://docs.google.com/document/d/1u60X_mKd9z548qrh_VjdyTx3hziiZiYPLUa_rJX11mQ/edit?usp=sharing
For starters, DON'T USE mysql_connect()! Use either mysqli or PDO. Second, don't have your application log in to MySQL as root; create an application-specific unprivileged user (with a password!) for this purpose.
You'll need to create your unprivileged user with connect privilege from the web server's IP or DNS address, and instead of connecting to localhost your PHP will need to connect to the DB server's IP or DNS address, like so:
$dsn = "mysql:host=mysqlserver.mynetwork.com;dbname=register";
try
{
$conn = new PDO($dsn, $appuser, $apppasswd);
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
You could try the following example
$server = "192.168.1.1"; //server name or IP Address
$username = "root"; //database username
$password = "password"; //database password
$database = "mydatabase"; //database to connect to
$connection = mysqli_connect( $server, $username, $password, $database);
if (!$connection){
die("Database Connection Failed" . mysqli_error());
}
else{
//sql statement
}
mysqli_close($connection);
Thanks

MSSQL using PDO to connect

I am trying to connect to a mssql server on my mac through pdo_dblib. I have the freetds.conf updated to the host I want to connect. My phpinfo tells me that I have all the driver hooked up and good to go. Below is the code that I wrote to test if I can connect to the server.
<?php
$servername = "IP";
$port = "port";
$username = "username";
$password = "password";
$myDB = "database";
try {
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", "$username", "$password");
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
However I get an error:
Connection failed: SQLSTATE[] (null) (severity 0)
I tried using SQL Developer to connect to the mssql server and it worked. I have been trying to solve this problem for a whole day. What might be the problem? Thanks!
Remove the quotes from the variables in the connection string -
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", $username, $password);
I found out the answer!
You have to uncomment TDS version protocol in /usr/local/Cellar/freetds/0.91/etc/freetds.conf.
Found the solution in Why won't my server connect to a remote MSSQL server using PHP mssql_connect?
but why do I have to do this...no idea...wish some one could give some explanation.

Can't connect to database through PHP PDO class

I'm trying to connect to my mySQL database using the PDO class in PHP.
Here is my Code :
// Connects to Our Database via PDO.
if($local) {
try {
$db = new PDO("mysql:=localhost;dbname=bbc_archive;port=3306", "root", "");
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
} catch(Exception $e) {
echo "Connection to the DataBase was not possible. ";
die();
}
} else {
try {
$db = new PDO("mysql:=bbcarchive.db.11505263.hostedresource.com;dbname=bbcarchive", "bbcarchive", "myPassword");
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
} catch(Exception $e) {
echo "Connection to the live DataBase was not possible. ";
die();
}
}
The $local variable is defined before and determines whether or not the script is running on a the live server or a test server.
When running in my local environment everthing works fine but on my live server it echo's out "Connection to the live DataBase was not possible." from the catch block.
I've contacted my host provider (godaddy) and they think it's a coding error. I've also, obviously, checked the hostname, dbname, username and password a 100 times and it's all correct. I just can't see the problem!
How can i do this ?
Your DSN seems to be incorrect. The documentation on MySQL DSNs indicates that it should look somewhat like this:
$db = new PDO("mysql:host=localhost;dbname=bbc_archive;port=3306", "root", "");

Categories