Link to home
Start Free TrialLog in
Avatar of errang
errangFlag for Afghanistan

asked on

How do you connect to the database on localhost with php

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?
Avatar of Brad Brett
Brad Brett
Flag of United States of America image

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
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
Avatar of errang

ASKER

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
You probably have incomplete installation of MySQL.

Try to reinstall MySQL and then try to run the same script again.
Change my.ini variable "mysql.default_socket" to:
mysql.default_socket = /tmp/mysql.sock

Open in new window

Avatar of errang

ASKER

Hm... apparently I don't have a my.ini in XAMPP/xamppfiles/etc folder.
Avatar of errang

ASKER

I just made a my.ini... that didn't work out so well...
Are you sure that MySQL is loading variables from the INI file you created?
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

Avatar of errang

ASKER

I just uninstalled XAMPP, and installed MAMP... still no luck... I'm not sure what else I can do.
Upload your php.ini here, I will update it for you.
Avatar of errang

ASKER

Oh, alright thanks, here it is.

Thought you wanted me to update the my.ini file.
php.ini
ASKER CERTIFIED SOLUTION
Avatar of Brad Brett
Brad Brett
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of errang

ASKER

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