

Make sure you change, your-username, and your-password to the values for your own database.īut what about the die bit? Sounds a little gruesome (like Lord of the Flies gruesome, not Twilight teen-angst gruesome). In your script, type the following lines: Error connecting to database: ". This script will be as simple as possible, because all you need it to do is connect to your database, USE the right database, and then run a sample SQL query to make sure things are working correctly. You need those same pieces of information to give PHP so it can connect: your database host, your username, and a password.įire up your text editor and create a new script call it connect.php.

When you connected to your web server’s database, you probably used a command like the followng: mysql -host=
Mysql php connection mac os#
This process is basically telling PHP to do what you did when you started up your MySQL command-line client ( MySQL on Mac OS X).
Mysql php connection how to#
All that in one chapter? Yes indeed.įirst, you have to tell your PHP script how to connect to a database. This script will take all the information from the forms you’ve built, add that information to a database, and then add one more form to let your users search for another user by name. Then, to put a cherry on top of your towering sundae of PHP and MySQL goodness, you’ll write another script. When you’re a PHP programmer you can go beyond the mysql command-line tool. In fact, you’re going to build a simple form that lets you enter SQL and run it against your MySQL database. You just need to learn a few new commands and how to deal with the problems that can come up when you’re working with a database. PHP isn’t like that though it comes ready to connect to MySQL from the moment you run the php command.Įven though you’ve only recently begun your journey to PHP mastery, you’re ready to use a database from your scripts.

With many programming languages, anytime you want to talk to a database, you have to download and install extra code, or install little plug-in models that give your programs support for talking to that database. Now that you’ve seen a bit of the power both of PHP and MySQL, it’s time to bring these two juggernauts together.
