kevwood Posted February 18, 2015 Share Posted February 18, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/294696-conflicting-error-message/ Share on other sites More sharing options...
mac_gyver Posted February 18, 2015 Share Posted February 18, 2015 this is from the myslqi connect_error documentation - WarningThe 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. Quote Link to comment https://forums.phpfreaks.com/topic/294696-conflicting-error-message/#findComment-1505998 Share on other sites More sharing options...
Barand Posted February 18, 2015 Share Posted February 18, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/294696-conflicting-error-message/#findComment-1506001 Share on other sites More sharing options...
kevwood Posted February 18, 2015 Author Share Posted February 18, 2015 (edited) 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 February 18, 2015 by kevwood Quote Link to comment https://forums.phpfreaks.com/topic/294696-conflicting-error-message/#findComment-1506002 Share on other sites More sharing options...
Barand Posted February 18, 2015 Share Posted February 18, 2015 No surprises there. If the username and password were wrong for mysqli then they will still be wrong for a PDO connection. You need to check the usernames and password on the server. Quote Link to comment https://forums.phpfreaks.com/topic/294696-conflicting-error-message/#findComment-1506003 Share on other sites More sharing options...
kevwood Posted February 18, 2015 Author Share Posted February 18, 2015 i have checked the user name and password a number of times and even changed the password to phpfreaks1 while i was uploading it on here for security reasons even though it only for testing anyway. Quote Link to comment https://forums.phpfreaks.com/topic/294696-conflicting-error-message/#findComment-1506005 Share on other sites More sharing options...
Solution kevwood Posted February 18, 2015 Author Solution Share Posted February 18, 2015 it was a problem with the server name, i had to specify the name of the server my db was being held on. is this simply because it is a free hosting site and my site and database are being held on different servers? Quote Link to comment https://forums.phpfreaks.com/topic/294696-conflicting-error-message/#findComment-1506007 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.