Corbokhan Posted December 10, 2010 Share Posted December 10, 2010 I'm looking to connect to a MySQL localhost on my mac. I've been working for a few hours at this and I still can't figure it out. Below is my code. I've tried a number of different ways to connect to the database including using new mysqli, mysql_connect and PEAR MDB2. Any suggestions as to what I can do? Thanks in advance. <?php $first=$_POST['first']; $last=$_POST['last']; if (!$first || !$last) { echo 'Error: Enter the required data.'; exit; } if (!get_magic_quotes_gpc()) { $first = addslashes($first); $last = addslashes($last); } $hostname = 'localhost:8889'; $username = 'root'; $password = 'root'; $dbname = 'phonebook'; @ $db = new mysqli($hostname,$username.$password,$dbname); if (mysqli_connect_errno()) { echo 'Errer: Could not connect to database'; exit; } echo '.....Connected Successfuly'; $query = "insert into contact values ('null','".$first."','".$last."')"; $result = $db->query($query); if ($result) { echo $db->affected_rows." contact inserted into the database"; echo 'Contact Entered: '.$first.' '.$last.''; } else { echo 'Error: Contact not added.'; exit; } $db->close(); ?> Link to comment https://forums.phpfreaks.com/topic/221171-localhost-connection/ Share on other sites More sharing options...
trq Posted December 10, 2010 Share Posted December 10, 2010 What does mysqli_connect_error say? Link to comment https://forums.phpfreaks.com/topic/221171-localhost-connection/#findComment-1145166 Share on other sites More sharing options...
Corbokhan Posted December 10, 2010 Author Share Posted December 10, 2010 It just echos what I have listed in the If statement. Link to comment https://forums.phpfreaks.com/topic/221171-localhost-connection/#findComment-1145167 Share on other sites More sharing options...
trq Posted December 10, 2010 Share Posted December 10, 2010 I said mysqli_connect_error not mysqli_connect_errno. Link to comment https://forums.phpfreaks.com/topic/221171-localhost-connection/#findComment-1145168 Share on other sites More sharing options...
Corbokhan Posted December 10, 2010 Author Share Posted December 10, 2010 mysqli_connect_error() says" Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) Link to comment https://forums.phpfreaks.com/topic/221171-localhost-connection/#findComment-1145169 Share on other sites More sharing options...
trq Posted December 10, 2010 Share Posted December 10, 2010 Change localhost to 127.0.0.1 Link to comment https://forums.phpfreaks.com/topic/221171-localhost-connection/#findComment-1145187 Share on other sites More sharing options...
Corbokhan Posted December 10, 2010 Author Share Posted December 10, 2010 I changed $hostname to 127.0.0.1 and this error comes back. Can't connect to MySQL server on '127.0.0.1' (111) Is that what you were referring to? Replacing "localhost" with "127.0.0.1"? Link to comment https://forums.phpfreaks.com/topic/221171-localhost-connection/#findComment-1145195 Share on other sites More sharing options...
Corbokhan Posted December 10, 2010 Author Share Posted December 10, 2010 I changed local host to 127.0.0.1 and changed the mysqli() to mysql_connect() and it now works. Thanks a lot for the help! Link to comment https://forums.phpfreaks.com/topic/221171-localhost-connection/#findComment-1145220 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.