Results 1 to 3 of 3
  1. #1
    Senior Member sp00f's Avatar
    Join Date
    Aug 2006
    Location
    Lithuania
    Posts
    117

    [PHP/MySQL Guide] Connecting, inserting and reading content from MySQL database.

    Hello, in this tutorial I will teach you how to connect into MySQL database, create new table inside of it, put information inside the created table and later read the information from the table. So, lets start!

    First of all, you should make MySQL database if you haven't already. After you have MySQL database ready we should make connection to it using the following code:

    Code:
    <?php
    mysql_connect("hostname","username","password");
    ?>
    The following code creates connection to the MySQL server using your given parameters. First parameter "hostname" is used for server address, ip or hostname. Mine is localhost, since i'm running local server. Second parameter is your MySQL username, default is "root", just like mine. Third parameter is the password of your MySQL username, if you don't use password for your MySQL user just leave this field blank. You may also want to add some checks, to check if connection were successful. Using this:

    Code:
    <?php
    $connection = mysql_connect("hostname","username","password");
    
    if(!$connection)
    {
    die("Cannot create connection to MySQL server: ".mysql_error()."");
    }
    ?>
    The following code will check if the connection were successful, if it wasn't, this code will print the causing reason. If you get any errors with this part, feel free to post here. I will help. Anyway, if you do have errors you should not continue reading.

    Okay, so after we made connection to MySQL server we want to select database, right? To do so we need to use "mysql_select_db" function, like this:

    Code:
    <?php
    $connection = mysql_connect("hostname","username","password");
    
    if(!$connection)
    {
    die("Cannot create connection to MySQL server: ".mysql_error()."");
    }
    
    mysql_select_db("database",$connection);
    ?>
    This code will connect to MySQL server(Just like previous) and select database using "mysql_select_db" function, at your parameters. First parameter of this function is our database name and the second one is connection. After we successfully made connection to MySQL server and selected our database we want to create new table inside of database. It can be done running "mysql_query" function:

    Code:
    <?php
    $connection = mysql_connect("localhost","root","gacioniz01");
    
    if(!$connection)
    {
    die("Cannot create connection to MySQL server: ".mysql_error()."");
    }
    
    mysql_select_db("Tutorials",$connection);
    
    $query = mysql_query("CREATE TABLE tablename ( firstname TEXT, lastname TEXT, nick TEXT )");
    ?>
    This code will create connection to MySQL server, select database and run MySQL query, which creates table named "tablename" with three fields: "firstname", "lastname" and "nick". After we have created database table, we want to have something inside of it, yes? We can fill in details to table using the same function we used to create the table. Yes, function called "mysql_query". As for tutorial lets fill in some simple details into database table. Like first name, last name and nick:

    Code:
    <?php
    $connection = mysql_connect("localhost","root","gacioniz01");
    
    if(!$connection)
    {
    die("Cannot create connection to MySQL server: ".mysql_error()."");
    }
    
    mysql_select_db("Tutorials",$connection);
    
    $query = mysql_query("CREATE TABLE tablename ( firstname TEXT, lastname TEXT, nick TEXT )");
    $query2 = mysql_query("INSERT INTO tablename VALUES ('MyName','MyLastname','MyNick')");
    ?>
    This code will connect to MySQL server, select database, create new table and insert details into that table. Next thing we should want is to read content from database table. To read content from database table we will use "mysql_query" and "mysql_result" functions. As you have noticed, we will use new function, called the "mysql_result". That function gets the result(output) from the query, in our case, result of details request query:

    Code:
    <?php
    $connection = mysql_connect("localhost","root","gacioniz01");
    
    if(!$connection)
    {
    die("Cannot create connection to MySQL server: ".mysql_error()."");
    }
    
    mysql_select_db("Tutorials",$connection);
    
    $query = mysql_query("CREATE TABLE tablename ( firstname TEXT, lastname TEXT, nick TEXT )");
    $query2 = mysql_query("INSERT INTO tablename VALUES ('MyName','MyLastname','MyNick')");
    $query3 = mysql_query("SELECT nick FROM tablename WHERE nick='MyNick'");
    echo mysql_result($query3,0);
    
    mysql_close($connection);
    ?>
    So, this, the last code will CONNECT TO MySQL server, SELECT the database, INSERT the table, SELECT content WHERE column name equals to "MyNick" and print the result of content selection from database table. And after we get the result it will close the connection to MySQL server using "mysql_close" function.

    That should be all for this tutorial. Hope this will help someone. If you have any question regarding this, don't be shy to ask!

    Update: m0wl suggested me to insert usage of "mysql_fetch_array" function. That's a wonderful idea! Thanks, m0wl!

    "mysql_fetch_array" function is used to get the result of MySQL query in the array. That is what we are going to do! This function contains two parameters, like this:

    array mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )
    For this tutorial we will use only the first parameter, about the second we will talk in later tutorials. Okay, so in this tutorial we will use the first parameter as our MySQL query - "$query3 = mysql_query("SELECT * FROM tablename");"
    Did you noticed the difference ?
    - Yes, we changed our MySQL query! With this query we will select everything from the table, not just the "nick" column, so we can use it for our new function (mysql_fetch_array). So, in order to use this, we should have already changed the MySQL query and deleted the (mysql_result), like this:

    Code:
    <?php
    $connection = mysql_connect("hostname","username","password");
    
    if(!$connection)
    {
    die("Cannot create connection to MySQL server: ".mysql_error()."");
    }
    
    mysql_select_db("database",$connection);
    
    $query = mysql_query("CREATE TABLE tablename ( firstname TEXT, lastname TEXT, nick TEXT )");
    $query2 = mysql_query("INSERT INTO tablename VALUES ('MyName','MyLastname','MyNick')");
    $query3 = mysql_query("SELECT * FROM tablename");
    
    mysql_close($connection);
    ?>
    Okay, so this code right now will not print anything, but just simply connect to MySQL server, select database, create new table, insert content into the table and close MySQL connection. But that is just what we want, in order to use "mysql_fetch_array" function.
    Right, now we will use the function that, i've talked about, so long!
    Our new function should look like this:

    Code:
    $row = mysql_fetch_array($query3);
    Explanation: This code will "put" the result of MySQL query to an array, so we can display content of the MySQL database table like this:

    Code:
    echo $row['firstname']."<br>";
    echo $row['lastname']."<br>";
    echo $row['nick'];
    That should be all for this short update.
    By the way, here is the full example code:

    Code:
    <?php
    $connection = mysql_connect("localhost","root","gacioniz01");
    
    if(!$connection)
    {
    die("Cannot create connection to MySQL server: ".mysql_error()."");
    }
    
    mysql_select_db("Tutorials",$connection);
    
    $query = mysql_query("CREATE TABLE tablename ( firstname TEXT, lastname TEXT, nick TEXT )");
    $query2 = mysql_query("INSERT INTO tablename VALUES ('MyName','MyLastname','MyNick')");
    $query3 = mysql_query("SELECT * FROM tablename");
    $row = mysql_fetch_array($query3);
    
    echo $row['firstname']."<br>";
    echo $row['lastname']."<br>";
    echo $row['nick'];
    
    mysql_close($connection);
    ?>

  2. #2
    Coders
    Join Date
    Nov 2009
    Location
    Belgium
    Posts
    990

    Re: [PHP/MySQL Guide] Connecting, inserting and reading content from MySQL database.

    That should be enough for somebody who wants to code a basic login system. Thanks for this tut!

    Regards,
    mOwl

  3. #3
    Senior Member sp00f's Avatar
    Join Date
    Aug 2006
    Location
    Lithuania
    Posts
    117

    Re: [PHP/MySQL Guide] Connecting, inserting and reading content from MySQL database.

    Will make a tutorial about sessions later.

Similar Threads

  1. Replies: 14
    Last Post: May 23rd, 2013, 06:50
  2. [PHP]Snippet - MYSQL
    By sil#s in forum Html, Php, Xml, Css, ...
    Replies: 4
    Last Post: July 8th, 2010, 19:31
  3. Offering help on Mysql, php and css
    By michael1987 in forum Html, Php, Xml, Css, ...
    Replies: 1
    Last Post: January 27th, 2009, 19:40
  4. Log in at website (MySQL?)
    By Square in forum Html, Php, Xml, Css, ...
    Replies: 6
    Last Post: December 8th, 2008, 18:39
  5. Replies: 3
    Last Post: September 11th, 2007, 10:48

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
  •