How do I get PHP to talk to a MySQL database?

Problems with the Mac OS X version of XAMPP, questions, comments, and anything related.

How do I get PHP to talk to a MySQL database?

Postby Abusername » 08. March 2009 17:59

Please help! I'm a total n00b at PHP/MySQL and am trying to learn it.
XAMPP has been a great help in getting me to the point where I can run PHP scripts on my local machine. Unfortunately I have no idea how to get PHP to talk to a MySQL database. I copied a sample .sql file into my site's root folder (which I also hear is a bad idea for security reasons - is this true?) and tried to run a PHP script from a tutorial I'm following. Well it didn't give any errors, but none of the data from the database was returned, so obviously it's not working. And if I'm not supposed to put .sql files in the root folder, then where?

It's been surprisingly difficult to find tutorials on how to do this and I am quite frustrated. The PHP tutorial I'm following is helpful but it foolishly assumes the user already knows how to set up PHP and MySQL and to get them to communicate...
Any help would be much appreciated!
Abusername
 
Posts: 11
Joined: 21. February 2009 10:41

Re: How do I get PHP to talk to a MySQL database?

Postby MegaChriz » 09. March 2009 10:29

I can recommend to read a book about PHP and MySQL.

I've learned a lot from the following book:
PHP & MySQL For Dummies
Unlike other books I tried first, this book contains also some specific info for Mac users.

.sql
It's not recommended to put .sql files in the root folder, because if others can read them, they can know how your database structure looks like. Hackers can try to hack your database and they get some extra help if they already know the structure of your tables. It's better to place the SQL-commands inside a PHP-file.

PHP-MySQL Communication
To get PHP and MySQL communicating you need to first create a connection.
Code: Select all
$connection = mysql_connect( $host, $user, $password );

Then, select a database.
Code: Select all
$dbconnect = mysql_select_db($database, $connection);

The host is most likely 'localhost'. Standard with XAMPP, there is a mysql-user 'root' which has no password. The root-user has all privileges to all MySQL-databases you have. It's recommended to give the root-user a password. It's also recommended to create another MySQL-user that has less privileges (for security reasons), but maybe it's better to learn that later.

To execute a query with PHP, here is an example:
Code: Select all
// Write a query
$sQuery = "SELECT `id`, `title` FROM `cds`";
// Execute query
$rResult = mysql_query($sQuery) or die (mysql_error());

// Retrieve the data
$aRows = array();
while ($aRow = mysql_fetch_array($rResult,MYSQL_ASSOC))
{
   $aRows[] = $aRow;
}

// Display the data
foreach ($aRows as $aRow)
{
   echo "id: " . $aRow['id'] . "<br />";
   echo "title: " . $aRow['title'] . "<br />";
}


I hope this helps you a bit. As I said, I recommend to read a book about PHP-MySQL.
MegaChriz
 
Posts: 158
Joined: 16. February 2009 15:04
Location: Amersfoort, The Netherlands
Operating System: Mac OS X

Re: How do I get PHP to talk to a MySQL database?

Postby Abusername » 09. March 2009 18:00

Hey, thanks for the reply. It's funny, I have that exact book! I'm only about 1/5 of the way through it but I intend to complete it.

Anyway, back to the specific issue I'm having at the moment: if not the root folder, where should I put my .sql files? Also how can I tell PHP to look in that location and communicate with my database? Because I don't see anything in the example code that indicates a filepath.

Thanks
Abusername
 
Posts: 11
Joined: 21. February 2009 10:41

Re: How do I get PHP to talk to a MySQL database?

Postby Wiedmann » 09. March 2009 18:18

if not the root folder, where should I put my .sql files? Also how can I tell PHP to look in that location and communicate with my database?

A *.sql file is not a database which you can use with PHP. That's a dump which you must import into the MySQL server.
(Maybe more reading in a PHP/MySQL book...)
Wiedmann
AF Moderator
 
Posts: 17106
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Re: How do I get PHP to talk to a MySQL database?

Postby Abusername » 09. March 2009 19:51

Wiedmann wrote:A *.sql file is not a database which you can use with PHP. That's a dump which you must import into the MySQL server.
(Maybe more reading in a PHP/MySQL book...)


OK, good to know!
Thank you, that's a start.

How does one import to the MySQL server?
I know you're all saying "read the book" but if it's just a matter of entering a command or two I'd rather get up and running asap and start learning PHP than have to first study every intricacy of MySQL. I learn best by doing, and if I cannot do, it makes the learning process quite slow.
Abusername
 
Posts: 11
Joined: 21. February 2009 10:41

Re: How do I get PHP to talk to a MySQL database?

Postby Wiedmann » 09. March 2009 20:00

How does one import to the MySQL server?

http://dev.mysql.com/doc/refman/5.1/en/batch-mode.html

(it's also possible to use phpMyAdmin)
Wiedmann
AF Moderator
 
Posts: 17106
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Re: How do I get PHP to talk to a MySQL database?

Postby MegaChriz » 10. March 2009 09:05

For importing SQL-commands in MySQL I recommend to use phpMyAdmin.

1. Start XAMPP.
2. Go to http://localhost/phpmyadmin in your browser.
3. On the left of the screen a list of databases appears. Select a database.
4. A list of tables appears. On the top of the screen you see a tabbed menu with 'Structure', 'SQL', 'Search' and some other. If you click on 'SQL' you can just type your sql-commands there and execute them. If you click on 'Import', you can import a sql-file to execute.

Great thing about phpMyAdmin is that if the SQL-command is'nt quite right, you see an error message of phpMyAdmin after you've tried to execute the SQL-command.

Hope that this helps you.
MegaChriz
 
Posts: 158
Joined: 16. February 2009 15:04
Location: Amersfoort, The Netherlands
Operating System: Mac OS X

Re: How do I get PHP to talk to a MySQL database?

Postby Abusername » 11. March 2009 10:01

MegaChriz wrote:For importing SQL-commands in MySQL I recommend to use phpMyAdmin.

1. Start XAMPP.
2. Go to http://localhost/phpmyadmin in your browser.
3. On the left of the screen a list of databases appears. Select a database.
4. A list of tables appears. On the top of the screen you see a tabbed menu with 'Structure', 'SQL', 'Search' and some other. If you click on 'SQL' you can just type your sql-commands there and execute them. If you click on 'Import', you can import a sql-file to execute.

Great thing about phpMyAdmin is that if the SQL-command is'nt quite right, you see an error message of phpMyAdmin after you've tried to execute the SQL-command.

Hope that this helps you.


That does help me! Thank you. :D
I think if I study phpMyAdmin I will get the answers I seek - and hopefully before I forget too much of the PHP basics I've learned.
I found a couple of decent YouTube videos on the subject, so we'll see where that leads me.
Abusername
 
Posts: 11
Joined: 21. February 2009 10:41


Return to XAMPP for Mac OS X

Who is online

Users browsing this forum: No registered users and 3 guests