We help IT Professionals succeed at work.

How do you connect to the database on localhost with php

errang
errang asked
on
Medium Priority
418 Views
Last Modified: 2012-05-11
Hey,

       I'm trying to figure out how to connect to the database on my machine with PHP... I have a Mac and I installed XAMPP.

I ran several PHP scrips to determine if I have the proper mysql extensions installed, like:

 
<?php

  function mysqlinstalled (){
    if (function_exists ("mysql_connect")){
      return true;
    } else {
      return false;
    }
  }
  function mysqliinstalled (){
    if (function_exists ("mysqli_connect")){
      return true;
    } else {
      return false;
    }
  }
  
  if (mysqlinstalled()){
    echo "<p>The mysql extension is installed.</p>";
  } else {
    echo "<p>The mysql extension is not installed..</p>";
  }
  if (mysqliinstalled()){
    echo "<p>The mysqli extension is installed.</p>";
  } else {
    echo "<p>The mysqli extension is not installed..</p>";
  }
?>

Open in new window


It told me that I had both the extensions installed, and I tried the specific mysql_connect methods for both extensions... but nothing worked...

Could anyone help me out with this?
Comment
Watch Question

Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
1. Use mysql_connect() to establish connection.
2. Use mysql_select_db() to select your database.

Take a look here:
http://www.siteground.com/tutorials/php-mysql/database_connection.htm
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
If you are using mysqli you can use mysqli_connect() method:
mysqli_connect($host, $user, $pass, $dbname);

Open in new window


http://www.php.net/manual/en/mysqli.connect.php

Author

Commented:
I ran:

 
<?php
	$mysqli = mysqli_connect( "localhost", "root", "" , "CISC474");
	$result = mysqli_query($mysqli, "select from * Hardware");
?>

Open in new window


These are teh errors I get:

Warning: mysqli_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /private/var/folders/2H/2H+D7uHaHQuEjsqEw5Wqfk+++TI/-Tmp-/-QuickProgrammingTMP1_719885386 on line 2

Warning: mysqli_connect(): (HY000/2002): No such file or directory in /private/var/folders/2H/2H+D7uHaHQuEjsqEw5Wqfk+++TI/-Tmp-/-QuickProgrammingTMP1_719885386 on line 2

Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /private/var/folders/2H/2H+D7uHaHQuEjsqEw5Wqfk+++TI/-Tmp-/-QuickProgrammingTMP1_719885386 on line 3
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
You probably have incomplete installation of MySQL.

Try to reinstall MySQL and then try to run the same script again.
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
Change my.ini variable "mysql.default_socket" to:
mysql.default_socket = /tmp/mysql.sock

Open in new window

Author

Commented:
Hm... apparently I don't have a my.ini in XAMPP/xamppfiles/etc folder.

Author

Commented:
I just made a my.ini... that didn't work out so well...
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
Are you sure that MySQL is loading variables from the INI file you created?
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
Oh, I just noticed that the update should be performed on php.ini NOT my.ini.

Open php.ini and update the variables mysql.default_socket and mysqli.default_socket to the following:
mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock

Open in new window

Author

Commented:
I just uninstalled XAMPP, and installed MAMP... still no luck... I'm not sure what else I can do.
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
Upload your php.ini here, I will update it for you.

Author

Commented:
Oh, alright thanks, here it is.

Thought you wanted me to update the my.ini file.
php.ini
Senior Software Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hm... still doesn't work, guess I'll try connecting to another server.

I still get the same errors for some reason:

Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /private/var/folders/2H/2H+D7uHaHQuEjsqEw5Wqfk+++TI/-Tmp-/-QuickProgrammingTMP1_1714636915 on line 4

Warning: mysql_connect(): No such file or directory in /private/var/folders/2H/2H+D7uHaHQuEjsqEw5Wqfk+++TI/-Tmp-/-QuickProgrammingTMP1_1714636915 on line 4

Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /private/var/folders/2H/2H+D7uHaHQuEjsqEw5Wqfk+++TI/-Tmp-/-QuickProgrammingTMP1_1714636915 on line 6
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.