I created a database with MySQL Workbench, and now I need to access it. So I've written a php script to access it:
<?
$db = mysql_connect("127.0.0.1:3306","root", "");
if (!$db){
echo "Could not connect to database";
exit();
}
$db_name = "pfc_db";
if (!mysql_select_db($db_name, $db)){
die ("Could not select database");
}
$sql=mysql_query("select * from CAPAS");
while($row=mysql_fetch_assoc($sql)){
$output[]=$row;
if (isset($output)){
echo "yes";
echo $output[0];
}
else{echo "no";}
}
mysql_close();
?>
I have doubts about MAMP and MySQL Community Server and I need a push on the right path.
I've installed MAMP on my mac and switched the ports to the default mysql ones 3306. I've placed the PHP test script in a folder "Api" I've created under htdocs. So to try it out i type on my browser http://127.0.0.1/api/test.php and the result is "Could not connect to databse". Am I doing it right?
And what about Community Server? Is it better than MAMP? Should it be running for MAMP to work? Can I just use Community Server? If so, where should I place the PHP scripts? Which folder? Because when I try to do the same test with Community Server, instead of showing the error message, the browser starts downloading the php script to my downloads folder. Why happens that?
As you can see I have a bit of a mess in my mind with these server stuff and I need some help to figure it out.
You do not need to install MySQL Community Server. MAMP already comes with MySQL.
You need to ouput more specific errors within your code by using the mysql_error()
function.
if (!$db){
echo "Could not connect to database" . mysql_error();
exit();
}
$db_name = "pfc_db";
if (!mysql_select_db($db_name, $db)){
die ("Could not select database" . mysql_error());
}
mysql_error will return the error message(s) from MySQL.
Following the asnwer I was giving to user775263, i get this table of users:
+
--------------------+------+----------+
| Host | User | Password |
+--------------------+------+----------+
| localhost | root | |
| SquirrellJoe.local | root | |
| 127.0.0.1 | root | |
| ::1 | root | |
| localhost | | |
| SquirrellJoe.local | | |
+--------------------+------+----------+
As you can see, any of the users has any password set. But then I found out there's a config.inc.php script under bin/phpmyadmin where there are these sentences:
$cfg['Servers'][$i]['auth_type'] = 'config'; // Authentication method (config, http or cookie based)?
$cfg['Servers'][$i]['user'] = 'root'; // MySQL user
$cfg['Servers'][$i]['password'] = 'root'; // MySQL password (only needed
I tried again to connect with $db = mysql_connect("127.0.0.1:3306","root", "root");
and the connexion was successful. Why the users table in mysql command line doesn't reflect this? Is it normal? Should I change it?
And then I run into another problem. It can't select the database 'pfc_db' I created on MySQL Workbench. I get the error message Unknown database 'pfc_db'. However, when I type mysql> show databases;
the mentioned db does appear on the table:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| pfc_db |
| test |
+--------------------+
So what might be the problem here?
Well, I'm gonna answer myself so that if someone runs into the same problem.
The error of password happened because I had two installations of mysql in the same computer, so that I was checking the passwords in one of them and the browser was looking at the other.
Therefore, if you have problems with password access, check that you don't have two mysql installed and also check the file config.inc.php
I also changed TCP/IP by a socket connexion, which improves the speed if you're using your localhost as server.
This worked for me
sudo mkdir /var/mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock
I ran into this problem and fixed it by using the default TCP/IP port instead of the socket port.
So in my case instead of 3306, I used 8889
$db = mysql_connect("127.0.0.1:8889","root", "");