+ Reply to Thread
Page 1 of 3
1 2 3 LastLast
Results 1 to 10 of 21

Thread: Tutorial: PHP to MySQL

  1. #1
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Tutorial: PHP to MySQL

    Creating a connection between PHP and MySQL is fairly simple. PHP provides all of the functions you need to connect and extract data.

    Before we begin have a database setup in MySQL and know the username, password and name of the database.

    Create a table and add some default values into it. Remember the name of this table.

    1) To connect to MySQL you really only need a few variables. To start, open notepad and add these lines:

    Code:
    <?php
    // mysql config
    $mysql_username "yourname";
    $mysql_password "password";
    $mysql_database "db";
    $mysql_server "localhost";
    These are the values that we will use to connect to MySQL from PHP. Replace all of these values with your MySQL values. $mysql_server = "localhost" can generally be as is.

    2) Now we will connect to MySQL using the variables above:

    Code:
    $link mysql_connect($mysql_server$mysql_username$mysql_password);
    mysql_select_db($mysql_database$link); 
    As simple as that you have a connection to MySQL.

    3) Lets pull some data out of our DB. You should have the table name as stated above and I named mine tbl_tutorial so replace this with your table name. We now need to build a query

    Code:
    $query "SELECT * FROM tbl_tutorial"
    4) Execute the query

    Code:
    $results mysql_query($query$link); 
    This will actually make the query on the MySQL database. Once this has been done all we need to do is fetch the results and manipulate them.

    5) Display our results
    Code:
    // Fetch results
    while ($row mysql_fetch_row($results)) {

       
    // Data will be displayed in an array $row[0]  
       
    print "$row[0]<br>";


    The $row array will contain all of the fields in your table starting with 0 and ending with the number of rows - 1.

    6) Close our connection

    Code:
    // Close our connection
    mysql_close($link);

    ?> 
    Thats it. I've attached the PHP file from this tutorial if you need to reference it.
    Attached Files

  2. #2
    Programming God xtraze is an unknown quantity at this point
    Join Date
    Dec 2006
    Location
    Sri lanka
    Posts
    908
    Really nice, I think I can extract data from my database of my Forum and display and If I knew a bit more, I think I can integrate the forum myself.

  3. #3
    Newbie lucky-girl is an unknown quantity at this point lucky-girl's Avatar
    Join Date
    Dec 2007
    Age
    25
    Posts
    27
    that's what i'm looking for

    u explained the tutorial in simple way

    thanks Jordan
    begginer programmer needs the help

  4. #4
    Newbie altoyes is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    2

    Re: Tutorial: PHP to MySQL

    this code looks good, i think it is what i need. i have followed your previous tutorial, but i did not know how to use the console.... i do not know about the binary settings ...


    Quote Originally Posted by Jordan View Post
    Creating a connection between PHP and MySQL is fairly simple. PHP provides all of the functions you need to connect and extract data.

    Before we begin have a database setup in MySQL and know the username, password and name of the database.

    Create a table and add some default values into it. Remember the name of this table.

    1) To connect to MySQL you really only need a few variables. To start, open notepad and add these lines:

    Code:
    <?php
    // mysql config
    $mysql_username "yourname";
    $mysql_password "password";
    $mysql_database "db";
    $mysql_server "localhost";
    These are the values that we will use to connect to MySQL from PHP. Replace all of these values with your MySQL values. $mysql_server = "localhost" can generally be as is.

    2) Now we will connect to MySQL using the variables above:

    Code:
    $link mysql_connect($mysql_server$mysql_username$mysql_password);
    mysql_select_db($mysql_database$link); 
    As simple as that you have a connection to MySQL.

    3) Lets pull some data out of our DB. You should have the table name as stated above and I named mine tbl_tutorial so replace this with your table name. We now need to build a query

    Code:
    $query "SELECT * FROM tbl_tutorial"
    4) Execute the query

    Code:
    $results mysql_query($query$link); 
    This will actually make the query on the MySQL database. Once this has been done all we need to do is fetch the results and manipulate them.

    5) Display our results
    Code:
    // Fetch results
    while ($row mysql_fetch_row($results)) {

       
    // Data will be displayed in an array $row[0]  
       
    print "$row[0]<br>";


    The $row array will contain all of the fields in your table starting with 0 and ending with the number of rows - 1.

    6) Close our connection

    Code:
    // Close our connection
    mysql_close($link);

    ?> 
    Thats it. I've attached the PHP file from this tutorial if you need to reference it.

  5. #5
    Newbie altoyes is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    2

    Re: Tutorial: PHP to MySQL

    hi jordon

    it fetches the data ok
    but instead of displaying an image,
    it displays a number.
    is this right?
    have i worked the script correctly?

    alto

  6. #6
    Newbie blackplastic is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    2

    Re: Tutorial: PHP to MySQL

    Very helpful.

  7. #7
    Newbie gracie20 is an unknown quantity at this point
    Join Date
    Aug 2008
    Posts
    4

    Re: Tutorial: PHP to MySQL

    can anyone tell me from where i can get the complete infomation about php and mysql database...

    Gracie Sh

  8. #8
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    Re: Tutorial: PHP to MySQL

    Get a book, or try W3Schools Online Web Tutorials for tutorials. MySQL :: MySQL Documentation and PHP: Hypertext Preprocessor provide the documentation.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  9. #9
    Newbie Tanchi is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    1

    Re: Tutorial: PHP to MySQL

    thanx

  10. #10
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    Re: Tutorial: PHP to MySQL

    Er, welcome.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

+ Reply to Thread
Page 1 of 3
1 2 3 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. MySQL Resources
    By dirkfirst in forum Database & Database Programming
    Replies: 2
    Last Post: 08-25-2009, 04:46 AM
  2. John's Java Tutorial Index
    By John in forum Java Tutorials
    Replies: 0
    Last Post: 01-11-2007, 03:05 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts