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

Thread: Tutorial: PHP to MySQL

  1. #1
    Jordan Guest

    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 Attached Files

  2. CODECALL Circuit advertisement

     
  3. #2
    xtraze is offline Programming God
    Join Date
    Dec 2006
    Location
    Sri lanka
    Posts
    911
    Rep Power
    0
    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.

  4. #3
    lucky-girl's Avatar
    lucky-girl is offline Newbie
    Join Date
    Dec 2007
    Posts
    29
    Rep Power
    0
    that's what i'm looking for

    u explained the tutorial in simple way

    thanks Jordan
    begginer programmer needs the help

  5. #4
    altoyes is offline Newbie
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    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.

  6. #5
    altoyes is offline Newbie
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    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

  7. #6
    blackplastic is offline Newbie
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Re: Tutorial: PHP to MySQL

    Very helpful.

  8. #7
    gracie20 is offline Newbie
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Re: Tutorial: PHP to MySQL

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

    Gracie Sh

  9. #8
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    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

  10. #9
    Tanchi is offline Newbie
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Re: Tutorial: PHP to MySQL

    thanx

  11. #10
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    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 123 LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Use PDO for mysql or standar mysql functions?
    By lol33d in forum PHP Development
    Replies: 7
    Last Post: 05-07-2011, 08:41 PM
  2. Replies: 213
    Last Post: 04-14-2011, 07:57 PM
  3. Tutorial: Storing Images in MySQL with PHP
    By Jordan in forum PHP Tutorials
    Replies: 47
    Last Post: 01-04-2011, 08:37 PM
  4. Replies: 1
    Last Post: 10-20-2010, 12:38 AM
  5. [C#]MySQL] Host '****' is not allowed to connect to this MySQL server
    By ZaroX in forum Database & Database Programming
    Replies: 2
    Last Post: 02-16-2010, 08:34 PM

Tags for this Thread

Bookmarks

Posting Permissions

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