+ Reply to Thread
Page 1 of 5 123 ... LastLast
Results 1 to 10 of 44

Thread: [PHP] MySQL and PHP

  1. #1
    Bryon is offline Administrator Bryon has disabled reputation
    Join Date
    Apr 2005
    Location
    Northfield, NH
    Posts
    7,582

    [PHP] MySQL and PHP

    Introduction: This tutorial is going to be about communicating with a MySQL server with PHP. I am going to try and cover connecting to database servers, querying servers (SELECT/UPDATE/INSERT/DELETE queries), and a few other related things. This tutorial will assume that you have *at least basic* PHP knowledge, and maybe even some HTML knowledge. Ok, now to begin.

    In order to use anything in this tutorial, you will need access to a MySQL database, as well as MySQL support compiled into PHP. Most web host's have MySQL enabled and compiled, so it should work most of the time. If you are unsure, ask your host.

    Section 1 ~All About MySQL:

    What is MySQL? MySQL is a multithreaded, multi-user, SQL (Structured Query Language) relational database server (RDBMS). MySQL is open source software available either under the GNU General Public License (GPL) or under other licenses when the GPL is inapplicable to the intended use. (Definition provided by http://en.wikipedia.org/wiki/Mysql)

    Why would I want to use MySQL on my website? There are many reasons why you would need/want to use a MySQL database for your site. Using a database means that you can easily store, retrieve, and update data, for basically anything you want. Databases allow you to create scripts on your site that can for example, let visitors register an account, and log in to it, and update their personal information for it. Once you learn how to communicate with a MySQL server fluently, a lot of new possibilities open up on what you can do with PHP.


    Section 1 ~MySQL Database Set Up/Layout/Arrangement:

    I'm going briefly explain how MySQL databases are set up, so you can get an understanding on how data is stored, and where. Each "part" I am going to talk about here goes in order, the first being the "highest", the second being "inside" the first, and so on.

    First, MySQL servers contain databases. Think of the database as a "main storage" area. Second, the tables. Each database consists of tables. Each table consists of rows of data that each different "value" is seperated into columns. Data is stored in tables in "rows". I probably have confused you, so here is a small "diagram" of how databases are set up.



    That image shows a database, named "Database" that contains 3 tables, Table 1, Table 2, and Table 3. I only showed Table 1, as an example, but table 2, 3, and so on would also have columns and rows in it also. Column names can be anything, Within reason ;). Columns have different types, which determine what kind of data is stored in them. I will explain a few of the basic column types here:

    Varchar (Num): The varchar column type can hold any type of data, up to "Num" length. (Letters, numbers, meta-characters) The max length of a string a varchar column can hold is 255 characters long.

    Int (Num): The int (Integer.. ) column type can hold any integers, up to "Num" length. The max length of a string a int column can hold is 255 characters long.

    Text: The text column type can hold as many characters as you want, and does not have a "limit" on the number of characters that can be in it. (Well it does when you get up to high numbers of GB's.. But never mind that for now.)

    Those are three of the most basic column types for a MySQL table. For a beginner, I would just recommend using these for now. If you want to check out all of the column types, take a look at: http://dev.mysql.com/doc/refman/4.1/...-overview.html

    Section 2 ~The Basics:

    Ok so now, you know what MySQL is, and a little about MySQL. I'll now begin to teach you how to use PHP and MySQL together.

    A - Connecting To The MySQL Server

    To connect to a MySQL database, you use the php function mysql_connect(). This function establishes a connection to the MySQL server and enables you to send and receive data to and from the server. The basic syntax to use this function is:

    PHP Code:
    <?php
    $username 
    ""// MySQL Username
    $password ""//MySQL Password
    $server ""// MySQL server you wish to connect to. Usually "localhost"
    $mysqlconnection mysql_connect($server$username$password);
    if (!
    $mysqlconnection) {
       die(
    'There was a problem connecting to the mysql server. Error returned: 'mysql_error());
    }
    ?>
    That "snippet" of code right there will establish a connection to $server, using the account $username and password $password. The reason that the result's are placed into the variable $mysqlconnection is quite simple. If you ever come across the need to connect to more then 1 MySQL server, you use the returned data from mysql_connect() to differentiate between the multiple connections. Also, the returned data is checked to be true/false, to have a simple check to make sure the connection was successful. If it was not, an error will be displayed that will contain the exact error returned from the server. I will talk about the mysql_error() function later on in the tutorial.

    B - Selecting a MySQL Database To Use:

    Now that you are connected to a MySQL Server, you need to select a database to send queries to, get data from, etc. To do this, you use the php function mysql_select_db(). Mysql_select_db() takes two arguments, the first being the database you wish to use, and the second, the mysql_connect() resource link. (Remember the $mysqlconnection variable from before? That is what I mean by "resource link".) The "resource link" is optional. The only reason will need it is if you are dealing with multiple MySQL server connections, each one being in a seperate "resource link". Here is an example using mysql_select_db() with one connection:

    PHP Code:
    <?php
    $mysqlconnection 
    mysql_connect($server$username$password);
    $databaseconnection mysql_select_db($database$mysqlconnection);
    if (!
    $databaseconnection) {
       die(
    'There was a problem using that mysql database. Error returned: 'mysql_error());
    }
    ?>
    Remember that the $mysqlconnection being used in that mysql_select_db() is only required if you need to have more then one server connection at a time. Any other time, you don't need it and can omit it completely. The mysql_select_db() function will use the current connection and select the database under that. Example:

    PHP Code:
    <?php
    $mysqlconnection 
    mysql_connect($server$username$password);
    $databaseconnection mysql_select_db($database);
    if (!
    $databaseconnection) {
       die(
    'There was a problem using that mysql database. Error returned: 'mysql_error());
    }
    ?>
    You should be able to understand that completly by now. If you do not, go back and read the last few paragraphs.

    One more thing that I wish to talk about in this section is the use of mysql_error(). Whenever you use a mysql function in PHP, if it errors at all, the error returned from the MySQL server will be placed into mysql_error(). When you echo this function, that is what you will see. The reason I am telling you this is because this function is very helpful when debugging mysql_query()'s, which you will learn about later in the tutorial. Mysql_error() holds only the last error returned from a MySQL server, and gets "overwritten" when a new error is triggered.



    ....


    The rest of this tutorial is on my site. The reason I'm only posting the first third of it is because 1, the tutorial is too long to put in one post, and 2, to gain more hits on my site. :-D

    The URL to the rest of this tutorial is:
    http://www.nedren.elementfx.com/view...JOZVZOUlRBPT0=
    Last edited by Bryon; 11-23-2005 at 02:04 AM.

  2. #2
    Bryon is offline Administrator Bryon has disabled reputation
    Join Date
    Apr 2005
    Location
    Northfield, NH
    Posts
    7,582

    Re: [PHP] MySQL and PHP

    *sigh*

    No one wants to read myyyyy tutorial? :-(

    Haha, come on people.. Learn!

  3. #3
    Phil is offline Retired Staff Phil is an unknown quantity at this point
    Join Date
    Aug 2005
    Location
    kingston, Ontairo, Canada
    Posts
    3,672

    Re: [PHP] MySQL and PHP

    I have read it, It's on your site two is it not I think I read a couple there to. Great job man!

  4. #4
    n4tec's Avatar
    n4tec is offline Lord Of The Keys n4tec is an unknown quantity at this point
    Join Date
    Feb 2005
    Location
    GeT NOTICED via n4tec
    Posts
    1,656

    Re: [PHP] MySQL and PHP

    Nice informative tutorial.. Keep it up..
    .::: Regards, n4tec :::.


  5. #5
    alvinxxx is offline x10Hosting Member alvinxxx is an unknown quantity at this point
    Join Date
    Oct 2006
    Posts
    1

    Re: [PHP] MySQL and PHP

    Finally , I learn MYSQL !! Thank you very much !! Keep it up

  6. #6
    Chris Z's Avatar
    Chris Z is offline x10 Spammer Chris Z is an unknown quantity at this point
    Join Date
    Sep 2005
    Location
    Alabama, USA
    Posts
    2,802

    Re: [PHP] MySQL and PHP

    wow, i never saw this, it looks like a great tutorial though, bryon, thanks for it ;)
    -Chris Z
    Retired Account Manager


  7. #7
    Bonekhan's Avatar
    Bonekhan is offline x10 Sophmore Bonekhan is an unknown quantity at this point
    Join Date
    Sep 2006
    Posts
    226

    Re: [PHP] MySQL and PHP

    Very informative.

  8. #8
    daniloedusalazar's Avatar
    daniloedusalazar is offline x10Hosting Member daniloedusalazar is an unknown quantity at this point
    Join Date
    Aug 2007
    Location
    Ecuador
    Posts
    20

    Re: [PHP] MySQL and PHP

    Yep, thanks for the tutorial...
    May the source be with you
    From Ecuador Origo.ec

  9. #9
    venu123 is offline x10Hosting Member venu123 is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    4

    Re: [PHP] MySQL and PHP

    Very informative dude...thanks

  10. #10
    admintwo's Avatar
    admintwo is offline x10 Sophmore admintwo is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    New Jersey, USA
    Posts
    185

    Re: [PHP] MySQL and PHP

    I get an error when trying to get to the rest of your toturial No input file specified. ???

    Anyone know how to get to it?
    http://earnedmoney.elementfx.com
    Yea that is right, the site to visit, when you want to know how to buy your home, with the amount of money you buy groceries
    with.....unbelievable? BELIEVE IT.
    Site includes other ways of making money



+ Reply to Thread
Page 1 of 5 123 ... LastLast

Similar Threads

  1. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 10:46 AM
  2. tons of PHP Resources
    By Chris S in forum Scripts & 3rd Party Apps
    Replies: 10
    Last Post: 01-16-2009, 11:07 AM
  3. Some help with some php mysql communication.
    By Sheepoholics in forum Scripts & 3rd Party Apps
    Replies: 10
    Last Post: 04-01-2006, 04:34 PM
  4. Php And Mysql Forum Rephrased!
    By maddude in forum Scripts & 3rd Party Apps
    Replies: 7
    Last Post: 04-05-2005, 01:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers