Connecting to MySQL on 1&1

I’ve created a database on my 1&1 server, I can connect to it through the Plesk control panel with the user I created. I’ve even “reset” the user password to the one I have in my script and can still connect through WebAdmin. However, my PHP script keeps returning the Unable to connect error. I’ve looked over this code time and again and can’t see what it is I am missing. Hopefully someone can show me the error of my ways :smiley:

// Database connection string
require_once(‘inc_dblogin.inc’);
$db_server = mysqli_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysqli_error());

mysqli_select_db($db_database)
or die("Unable to select database: " . mysqli_error());

The inc_dblogin.inc file just contains the values for my connection, which I’ve also echoed to the screen from this file to make sure they are properly being chosen and they are good. I’m at a loss as to what it is I am doing wrong. :nono: Any suggestions would be greatly appreciated.
Greg

What error code does it show?

That’s one of the things I don’t get, it doesn’t show any error code, just simply my notice - "Unable to connect to MySQL: " (without the quotes, but no error code follows it.
Greg

Give this a whirl.


<?php
error_reporting(-1);
ini_set('display_errors', true);

$config = array(
  'host'    => '127.0.0.1',
  'port'    => 3306,
  'user'    => 'AnthonySterling',
  'pass'    => 'MyS3cr37P@5s',
  'schema'  => 'databaseName'
);

$con = mysqli_connect(
  $config['host'],
  $config['user'],
  $config['pass'],
  $config['schema'],
  $config['port']
);

if(false === $con){
  echo 'Error: ' . mysqli_connect_error() ;
  exit;
}

echo '<h1>Connected!</h1>';
echo '<p>Info: ', mysqli_get_host_info($con), '</p>' ;
exit;
?>

Anthony.

I am also having an issue getting to a 1and1 database.

This is as far as I get with my code.


error_reporting(-1);
ini_set('display_errors', true);
$user="dbo#########";
$password="*********";
$database="db#########";
mysql_connect(localhost,$user,$password);

1and1 gave me a user of dbo######### and I have verified that the numbers are correct. Same for the database. I am using the correct password.

I get
Notice: Use of undefined constant localhost - assumed ‘localhost’ in /homepages …

and I also get

Warning: mysql_connect() [function.mysql-connect]: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2) in /homepages…

I am guessing I should either define localhost, or use something else such as
/homepages…

Found the solution.


error_reporting(-1);
ini_set('display_errors', true);
$server= "db####.perfora.net";
$user="dbo#########";
$password="*********";
$database="db#########";
mysql_connect($server,$user,$password);

All the information is the same except the $server which is part of the information 1and1 provides.

I managed to find a 1and1 FAQ at http://faq.1and1.com/scripting_languages_supported/php/11.html which gives the PHP scripts to create a table, add data, etc.

This was a major step for me in my learning. Now, I can start populating the database and learn how to display the information.

You guys ROCK! It was a little of this and that, but I finally figured out my problem. Turns out I wasn’t declaring WHICH database connection to use.

i.e. mysqli_select_db($db_database)
SHOULD HAVE BEEN
mysqli_select_db($db_server, $db_database)

I also had problems with the format for my server location and username, but I found those because of adding these lines:

error_reporting(-1);
ini_set(‘display_errors’, true);

to the top of the script I was currently working on and it would then display (access denied for user blahblahblah) which then pointed me in the direction to look instead of just guessing.

All stuff I should remember from all the books I’ve read that have told me that over and over, but now that I am actually working on a real world app, maybe it will finally start to sink in.

Thanks guys! Hope this helps someone else down the line.

Greg