Connecting to mySQL using PHP/Apache

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
RyanP
Forum Newbie
Posts: 5
Joined: Tue Oct 09, 2007 12:26 pm

Connecting to mySQL using PHP/Apache

Post by RyanP »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm a beginner learning PHP/mySQL (if there's a better forum for beginner's, please let me know!).  All I'm trying to do is connect to a local mySQL database using PHP (so I can really start learning PHP), but I'm having trouble getting that far.  It's no problem if I use a db on a web server because I can easily find the database name, user name, and password (usually the hosting providers make that easy to get to).  I'd rather do it locally though, but am having trouble.

Where do I need to put the database to be able to access it?  How do I reference it using the code below?  I'm on a Windows machine and running the files from the htdocs folder in the c:\apache2 directory.  The problem is that I don't know where/how to put the database.

This is what I tried that did not work (and for what it's worth, it didn't even show the die message):

Code: Select all

$dbHost = "localhost"; 
$dbUser = "root"; 
$dbPass = "password"; 
$dbDatabase = "mobro"; 

//connet to the database 

$db = mysql_connect($dbHost, $dbUser, $dbPass) or die ("Error connecting to database.");
Thanks for any guidance!
Ryan


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Hi Ryan,

This is a good place to ask your questions. The typical Windows installation of MySQL creates a directory named MYSQL in the C:\ directory, and within that will be a subdirectory for the version of MySQL that is installed, and within that, a directory named data, which contains subdirectories for each database you create. Each table is represented by a .frm file in the appropriate subdirectory.

That said, you don't need to worry about that. If your Apache is configured correctly, all you need to do in PHP is exactly what you showed. If you have run that script and received the error message, it probably means that MySQL isn't running. Usually it is run as a Service in WIndows. You can tell if it's running by going to the Windows command line and trying to logon: mysql -hlocalhost -uroot -ppassword. If you get a mysql> prompt, it's running. If it isn't running, you have to start it. If you're going to be using it a lot, you'll probably want to set it up as a startup service so that it runs every time you boot Windows. Go to the Start menu, choose Run and enter msconfig. Go to the Services tab and look for MYSQL. If it's not there, then MySQL wasn't installed correctly. If it's there, but not checked, you can start it or set it up for start on bootup.

If MySQL is running and Apache is running and configured for PHP (for example, if you create a script test.php that contains this:

Code: Select all

<?php
echo "Hello World";
?>
and you can open that in your browser and it displays "Hello World", that will demonstrate that PHP and Apache are working), then you must have something else wrong that I can't diagnose, at the moment.

So start with that and come back here if you can't get it running.
Post Reply