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?
errangAsked:
Who is Participating?
 
Mohamed AbowardaSoftware EngineerCommented:
Try replacing your php.ini with the following INI file.
 php.ini

Then RESTART the apache.

After that, use the following PHP code to connect to the database:
<?php

// Change username and password with your mysql login information
$cnn = mysql_connect("localhost", "username", "password");
// Change dbname with your database name
mysql_select_db("dbname", $cnn);
?>

Open in new window

0
 
Mohamed AbowardaSoftware EngineerCommented:
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
0
 
Mohamed AbowardaSoftware EngineerCommented:
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
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
errangAuthor 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
0
 
Mohamed AbowardaSoftware EngineerCommented:
You probably have incomplete installation of MySQL.

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

Open in new window

0
 
errangAuthor Commented:
Hm... apparently I don't have a my.ini in XAMPP/xamppfiles/etc folder.
0
 
errangAuthor Commented:
I just made a my.ini... that didn't work out so well...
0
 
Mohamed AbowardaSoftware EngineerCommented:
Are you sure that MySQL is loading variables from the INI file you created?
0
 
Mohamed AbowardaSoftware EngineerCommented:
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

0
 
errangAuthor Commented:
I just uninstalled XAMPP, and installed MAMP... still no luck... I'm not sure what else I can do.
0
 
Mohamed AbowardaSoftware EngineerCommented:
Upload your php.ini here, I will update it for you.
0
 
errangAuthor Commented:
Oh, alright thanks, here it is.

Thought you wanted me to update the my.ini file.
php.ini
0
 
errangAuthor 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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.