Jump to content

conflicting error message


kevwood
Go to solution Solved by kevwood,

Recommended Posts

Hello all,

 

Been a while since i have been on here since he lack of using my degree lol.

 

but i am dusting the cob webs of the brain an realising it has all drastically changed since i last used php and mysql.

 

any enough with the blabbing here is my problem!

 

 

i am trying to establish a connection with a database i have set up on a free hosting server.  when i run the code i am getting conflicting messages.

 

here is my code

<?php

$name = "name";
$desc = "description";
$price = "price";
$image = "image";
$prod_type = "prod_type";
$made = "made";
$dist = "distribute";

$server = "localhost";
$db = "a6382499_product";
$user_name = "a6382499_sonic";
$password = "phpfreaks1";

// create connection to db
$conn = new MySQLi($server, $user_name, $password);

// check connection
if (!$conn->connect_error) {
	die("Connection failed: " . $conn->connect_error());
}
echo "Connect successful";

?>

it is giving me this error message when i run the code

 

 

 

Warning: mysqli::mysqli() [mysqli.mysqli]: (28000/1045): Access denied for user 'a6382499_sonic'@'localhost' (using password: YES) in /home/a6382499/public_html/test_connection.php on line 25

 

 

but then i am getting the message that i am connected to the db successfully .

 

any help with this would be great.

Link to comment
Share on other sites

this is from the myslqi connect_error documentation -

 

Warning
The mysqli->connect_error property only works properly as of PHP versions 5.2.9 and 5.3.0. Use the mysqli_connect_error() function if compatibility with earlier PHP versions is required.

 

it's recommended that you use the PDO database library, as it is much more consistent and easier to use.

Link to comment
Share on other sites

 

if (!$conn->connect_error) {

    die("Connection failed: " . $conn->connect_error());

}

echo "Connect successful";

 

Your code "dies" if there is *not* an error!

So you therefore echo "success" but the error gets displayed anyway

 

Note: You can also specify the database with a 4th parameter in your connection.

Link to comment
Share on other sites

thanks for the reply.  i have updated my code with this (this is the PDO correct way i think, please correct me if i am wrong).

// create connection to db
try {
	$conn = new PDO("mysql:host=$servername;$db", $user_name, $password);
// PDO error mode set to exception
	$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	echo "Connected to database";
}
// Set veriable to catch error created
catch(PDOException $error)
	{
	echo "Connection failed: " . $error->getMessage();
}

and i am getting this message

 

 

 

Connection failed: SQLSTATE[28000] [1045] Access denied for user 'a6382499_sonic'@'localhost' (using password: YES)

 

 

my password and user name are 100% correct i have re entered them and updated them a few times to make sure of this

Edited by kevwood
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.