PHP - Connecting to MySQL

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Dt7

Thread Starter
Joined
Jul 25, 2005
Messages
74
I'm working through a PHP/MySQL introductory book, and developing everything locally on my own PC. I've got Apache 2.2.8, PHP 5.2.5, and MySQL 5.0 installed.

My problem is actually getting PHP to communicate with MySQL. The book I'm reading says to go to http://dev.mysql.com/downloads/connector/php/ and download what is there- problem is I get a 404 error when I go there. I checked the MySQL documentation on the site (http://dev.mysql.com/doc/refman/5.0/en/connector-php.html) and it says the same thing.

So... anyone know how I can get this working?
 
Joined
Apr 14, 2001
Messages
2,382
It is probably outdated book and the page no longer exists?
You can use this;
http://dev.mysql.com/downloads/connector/php-mysqlnd/

If you want to connect to your MySQL database with PHP all you need is a connection script. There are lots of simple ways to connect to a MySQL database;
PHP:
<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
echo "Connected to MySQL<br />";
?>
will return an erro if not connected, and display Connected to MySQL if it is successful.

But the idea of the connection is to get and display the content of a database, so this is commonly written.
PHP:
<?
// hostname or ip of server, more than likely localhost
$servername='localhost';

// username and password to log onto db server, enter your values
$dbusername='username';
$dbpassword='password';

// name of database on the MySQL server
$dbname='db_name';

////////////// Do not  edit below/////////

connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
?>
Then obviously, you want to print a value stored in the database, use this type;
PHP:
<?php
// define database to connect to
$database="yourdbname";

//define connection variables
mysql_connect ("localhost", "yourusername", "yourpassword");

//return error if wrong database or doent exist
@mysql_select_db($database) or die( "Unable to select database");

//get results of table
$result = mysql_query( "SELECT field1, field2, field3 FROM yourtable" )

// return error if tables are wrong or dont exist
or die("SELECT Error: ".mysql_error());

// define rows
$num_rows = mysql_num_rows($result);

// print heading and table to show results
print "There are $num_rows records.<P>";
print "<table width=200 border=1>\n";
while ($get_info = mysql_fetch_row($result)){
print "<tr>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=1/>$field</font></td>\n";
print "</tr>\n";
}
print "</table>\n";
?>
YOu will obviously need to change some of the commented variables in each sample to your own user name and passwords, tables and databases.

Hope these samples help.
 
Joined
Jun 16, 2004
Messages
592
If you hate to putting all 3 together then you might want to consider downloading Wamp (windows) Lamp(linux) or xamp.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top