Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How do you connect to the database on localhost with php

Posted on 2011-04-24
14
Medium Priority
?
386 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?
0
Comment
Question by:errang
  • 8
  • 6
14 Comments
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35457541
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
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35457545
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
 

Author Comment

by:errang
ID: 35457587
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35457593
You probably have incomplete installation of MySQL.

Try to reinstall MySQL and then try to run the same script again.
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35457599
Change my.ini variable "mysql.default_socket" to:
mysql.default_socket = /tmp/mysql.sock

Open in new window

0
 

Author Comment

by:errang
ID: 35457613
Hm... apparently I don't have a my.ini in XAMPP/xamppfiles/etc folder.
0
 

Author Comment

by:errang
ID: 35457620
I just made a my.ini... that didn't work out so well...
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35457638
Are you sure that MySQL is loading variables from the INI file you created?
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35457649
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
 

Author Comment

by:errang
ID: 35457650
I just uninstalled XAMPP, and installed MAMP... still no luck... I'm not sure what else I can do.
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35457673
Upload your php.ini here, I will update it for you.
0
 

Author Comment

by:errang
ID: 35457680
Oh, alright thanks, here it is.

Thought you wanted me to update the my.ini file.
php.ini
0
 
LVL 12

Accepted Solution

by:
Mohamed Abowarda earned 1500 total points
ID: 35457696
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
 

Author Closing Comment

by:errang
ID: 35457746
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question