There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
PHP - Connecting to MySQL


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Dt7's Avatar
Dt7 Dt7 is offline
Member with 67 posts.
 
Join Date: Jul 2005
Experience: Intermediate
30-Mar-2008, 09:20 AM #1
PHP - Connecting to MySQL
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/...ector-php.html) and it says the same thing.

So... anyone know how I can get this working?
Sequal7's Avatar
Computer Specs
Distinguished Member with 2,369 posts.
 
Join Date: Apr 2001
Location: Around the corner!
Experience: Including today?
05-Apr-2008, 11:23 PM #2
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 Code:
<?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 Code:
<?
// 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 Code:
<?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.
__________________
Good Luck on your fix

My real hobby..JoyCo
My real Job..(Second Hobby) IAFF Local 1865
Like the sites? My hobby is the one that created them!
skyhigh007's Avatar
Senior Member with 387 posts.
 
Join Date: Jun 2004
05-Apr-2008, 11:55 PM #3
If you hate to putting all 3 together then you might want to consider downloading Wamp (windows) Lamp(linux) or xamp.
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:00 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.