errang
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:
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?
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>";
}
?>
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?
If you are using mysqli you can use mysqli_connect() method:
http://www.php.net/manual/en/mysqli.connect.php
mysqli_connect($host, $user, $pass, $dbname);
http://www.php.net/manual/en/mysqli.connect.php
ASKER
I ran:
These are teh errors I get:
Warning: mysqli_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.so ck) in /private/var/folders/2H/2H +D7uHaHQuE jsqEw5Wqfk +++TI/-Tmp -/-QuickPr ogrammingT MP1_719885 386 on line 2
Warning: mysqli_connect(): (HY000/2002): No such file or directory in /private/var/folders/2H/2H +D7uHaHQuE jsqEw5Wqfk +++TI/-Tmp -/-QuickPr ogrammingT MP1_719885 386 on line 2
Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /private/var/folders/2H/2H +D7uHaHQuE jsqEw5Wqfk +++TI/-Tmp -/-QuickPr ogrammingT MP1_719885 386 on line 3
<?php
$mysqli = mysqli_connect( "localhost", "root", "" , "CISC474");
$result = mysqli_query($mysqli, "select from * Hardware");
?>
These are teh errors I get:
Warning: mysqli_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.so
Warning: mysqli_connect(): (HY000/2002): No such file or directory in /private/var/folders/2H/2H
Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /private/var/folders/2H/2H
You probably have incomplete installation of MySQL.
Try to reinstall MySQL and then try to run the same script again.
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
ASKER
Hm... apparently I don't have a my.ini in XAMPP/xamppfiles/etc folder.
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:
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
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.
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.so ck) in /private/var/folders/2H/2H +D7uHaHQuE jsqEw5Wqfk +++TI/-Tmp -/-QuickPr ogrammingT MP1_171463 6915 on line 4
Warning: mysql_connect(): No such file or directory in /private/var/folders/2H/2H +D7uHaHQuE jsqEw5Wqfk +++TI/-Tmp -/-QuickPr ogrammingT MP1_171463 6915 on line 4
Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /private/var/folders/2H/2H +D7uHaHQuE jsqEw5Wqfk +++TI/-Tmp -/-QuickPr ogrammingT MP1_171463 6915 on line 6
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.so
Warning: mysql_connect(): No such file or directory in /private/var/folders/2H/2H
Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /private/var/folders/2H/2H
2. Use mysql_select_db() to select your database.
Take a look here:
http://www.siteground.com/tutorials/php-mysql/database_connection.htm