mysql connect not working with PHP - php

Any idea why the following code is not working
mysql credentials are correct, have verified from command line.
The second connection fails i.e., $conn1 . I am clueless !!!
$conn = mysql_connect($hostname,
$username, $password)
or die("Connecting to MySQL failed" . mysql_error());
$conn1 = mysql_connect($hostname,
$username1, $password1)
or die("Connecting to MySQL failed" . mysql_error());

Test it with hardcoded values first to be sure...
//Connect to database from here
$link = mysql_connect("localhost", "dbaadmin", "sqlpassword");
if (!$link) {
die('Could not connect: ' . mysql_error());
}

have verified from command line.
There's so many questions here that you have not answered.
The command line where the PHP script is running?
The same chroot environment where the PHP script is running?
The OS user (not database user) is the same as the webserver runs as?
The hostname you are connecting to is on a seperate machine from where the PHP script (and the CLI) is running? (localhost will use a unix domain socket rather than a network socket)
...and just what is the error message you get?
C.

It's a guess, but is the second connection supposed to be to $hostname, or perhaps $hostname1 instead.

Related

Using PHP to connect to AWS Lightsail MYSQL Database instance

I hope this question falls within the scope of this site.
I have created a Lightsail instance in AWS and added a MYSQL database to it. I have been able to connect to the database with Workbench, but not with a PHP file hosted in my instance.
I have checked the servername, username, password and dbname 20 times. Every time I try to connect I get the error message: "Connection failed 6: Unknown database 'Databasename-1'". Does anybody know of a guide on troubleshooting connecting to a MYSQL database on Lightsail?
<?php
$servername = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.rds.amazonaws.com";
$username = "xxxxxx";
$password = "xxxxxx";
$dbname = "Databasename-1";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed 6: " . $conn->connect_error);
}
So, i think it turns out I did not know how to get the database name from my lightsail instance and possibly not understand how to use Workbench.
I was using a database name that did not exist.
I just started over and created everything from scratch from the command prompt and everything works as expected.

Connect to remote MySql server with XAMPP

I have an script that i want it to run for more that 5 minutes. But my current hosting service does not allow me to modify the max_time_limit in the php.ini (That is set to 2 minutes)...
So i thought that i could run the script with XAMPP and send the data to my database but i cant connect to the remote database.
This is the code for the connection:
$servername = "217.70.186.108"; //I've also tried with the name of the webpage (metagame.gg)
$username = "the_username"; //the username is not root since I've read that root can only connect from localhost. This user has all privileges. This user was created with the permision to be connected from any server (%)
$password = "the_password";
$dbname = "the_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
It displays this error:
Connection failed: An error occurred during the attempt to connect because the connected party did not properly responded after a period of time, or failed in the established connection because connected host has failed to respond.
Any help would be highly appreciated

How to get mysqli_connect to work?

I am trying to use mysqli_connect as such:
$host = "ftp.tatiana-renae.com";
$user = "tatiana";
$password = "********";
$dbname = "********";
$connection = mysqli_connect($host, $user, $password, $dbname);
but I get this error:
Database connection failed: Can't connect to MySQL server on 'ftp.tatiana-renae.com' (111) (2003)
I know mysqli_connect works differently than mysql_connect but I can't figure out what to do for mysqli_connect to make it happy. Any ideas?
Error 111 is "connection refused". One candidate explanation is that MySQL is not "listening" on the network, but only the TCP loopback address.
Check the my.cnf for the server, and check if it contains lines like this:
skip-networking
or
bind_address=127.0.0.1
If your PHP is running on the same server as MySQL, you can try using '127.0.0.1' for the host, in place of 'ftp.tatiana-renae.com'.
Is the MySQL server running? Can you connect to it using the mysql command line interface?
There's several other possibilities; but there's not enough information provided about you environment for us to accurately diagnose the problem. We're just guessing.

Why php PDO uses a different from hostname to the one used by mysql_connect() when connecting to a remotely hosted mysql database?

I've mysql database and php/apache on two different servers: let's say hostphp.domain.com and hostmysql.domain.com.
On the mysql server I've set a user "my_user" with permissions to connect to "my_database" db from the specific host "hostphp.domain.com".
When I connect to it using mysql_connect it does right. But when I do it via php PDO I get this error:
SQLSTATE[42000] [1044] Access denied for user 'my_user'#'%' to database 'my_database'
I've done some tests and I found the problem is ...#'%', mysql is refusing that connection because "my_user" does not have permission to connect from any host.
Also I've tried to connect using mysql_connect with a wrong password to see the error and I get this:
Could not connect: Access denied for user 'my_user'#'hostphp.domain.com' (using password: YES).
The difference is in ..#'%' and ...#'hostphp.domain.com'.
So that's my question, why php pdo do not declare hostname when connecting to a remote host? (or is doing that wrong).
Thanks and sorry for my english.
Edit.
Some code example, this does not work:
try {
$pdo = new PDO(
'mysql:host=hostmysql.domain.com;port=3306;dbname=my_database',
'my_user',
'my_pass'
);
} catch (PDOException $e) {
die($e->getMessage());
}
but this works ok:
$conn = mysql_connect('hostmysql.domain.com', 'my_user', 'my_pass');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
You have successfully connected to the host using mysql_connect, but you got no errors because you did not attempt to select the database.
Your user probably doesn't have access to your database.
Try running mysql_select_db("my_database"); after connected to the host and you should get the same error.

Can't connect to mysql, have tried a lot of things

I've read a ton of threads and solutions, I'm not purposely trying to make yet another one of these posts.
Now that thats out of the way
if i run this in my local osx folder (with web sharing on), i get a could not connect error:
<?php
$db = mysql_connect("localhost", "root", "password");
if (!$db) {
die('Could not connect' . mysql_error());
}
echo 'Connected successfully';
yet when i go to terminal and run the following it works fine:
mysql -u root -h 'localhost' -p
password<enter>
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1030
Server version: 5.5.25a MySQL Community Server (GPL)
i have updated php.ini to point to /tmp/mysql.sock as well
If you're just starting out and are using PHP5, you should not use mysql_connect, but instead mysqli_connect (use of the mysql extension is discouraged).
Check connect_error to get more information about why the connection is failing.
(Example taken from the above doc)
<?php
$mysqli = #new mysqli('localhost', 'root', 'password', 'THE DB NAME');
// Works as of PHP 5.2.9 and 5.3.0.
if ($mysqli->connect_error) {
die('Connect Error: ' . $mysqli->connect_error);
}
Try to display your php configuration with a phpinfo();, you might learn a lot from that.
Also, check the port number used by MySQL, I've seen many example where it wasn't standard on OSX installation.

Categories