SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    learning mysql in php

    I am having trouble learning php databases. I simply want a very straight forwards example that shows hoqw to

    1. add records to an existing database
    2. loop through a database and print out a particular field to the page
    3. append a record
    4. delete a record or records

    Does anyone know any very simple examples. That will work. I have tried a few and they need some code changing and have not made that clear.

  2. #2
    SitePoint Zealot t0WL337's Avatar
    Join Date
    Mar 2007
    Location
    Ottawa, Ontario!
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You just posted virtually the same thing in another thread.
    Check at this site here: http://dev.mysql.com/doc/refman/5.1/en/insert.html

  3. #3
    SitePoint Enthusiast greg_dalrymple's Avatar
    Join Date
    Mar 2006
    Location
    Northern Ireland
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Firstly you'll need to set up your database connection if you haven't done so already. There are many examples out there on the web that will show you how to set this up, most important thing to remember is to store it outside of your root directory.

    Simple Connection

    // Database connection string
    $db_connect = @mysql_connect('yourHost', 'yourUsername', 'yourPassword');

    // Connect to the database and check for errors
    if (!$db_connect) {
    exit('<p>Unable to connect to the database server at this time.</p>'.mysql_error().'');
    }

    // Select the database and check for errors
    if (!@mysql_select_db('databaseName')) {
    exit('<p>Unable to select the chosen database at this time.</p>');
    }

    Adding Records to an existing database

    // Define the SQL query
    $query = "INSERT INTO tableName (field1, field2) VALUES ("value1", "value2")";

    // Check that the query ran OK
    if (!$query) {
    exit ("Sorry, the data couldn't be added to the database");
    }

    Loop through the database

    // Use a while loop to run through and print each record
    while ($row = @mysql_fetch_array($query)) {
    $variable1 = $row['value1'];
    $variable2 = $row['value2'];

    // Print the values
    echo $variable1;
    echo $variable2;
    }

    Editing a record

    // Define the query to edit the record
    $query = "UPDATE tableName SET field1 = '$newValue' WHERE .....";

    Deleting Records

    // Define a query to delete records
    $query = "DELETE * FROM tableName WHERE row_id = '$value_id'";

    Hope this help, this is very basic syntax but it should give you an idea of how things work.
    Greg Dalrymple
    MULTIMEDIA DESIGNER ~ WEB DEVELOPER
    www.gregdalrymple.co.uk

  4. #4
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    w3 Schools has good tutorials on the basics:

    http://www.w3schools.com/sql/default.asp
    Ryan Price
    Subscribe to my blog for regular tips and tricks

  5. #5
    SitePoint Addict
    Join Date
    Sep 2005
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You may want to consider buying a used book at amazon.com on php and mysql for beginners.
    PHP Shopping Cart Software Easy Ecommerce Shopping Cart Script.
    PHP Super Cart is 100% template driven.

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much everyone.


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
  •