1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

PHP - Connecting to MySQL

Discussion in 'Web Design & Development' started by Dt7, Mar 30, 2008.

Thread Status:
Not open for further replies.
  1. Dt7

    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?
     
  2. Sequal7

    Sequal7

    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.
     
  3. skyhigh007

    skyhigh007

    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.
     
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 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/698537

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice