MySQL Queries Made Easy With PHP Functions Library


This entry is part 3 of 5 in the series Database Playground

Making Updates in MySQL. How Many are Affected?

When you need to update a record in your database you'll use a MySQL UPDATE query. If you want to know how many rows were affected you could you use this next addition to our Query class in the functions-db.php file.

PHP

function Affected() {
    return mysql_affected_rows($this->connect);
}

I'll create a new sample script to use this function, along with some of the ones we've already covered.

PHP

<?php
    # sample2.php file

    include_once('includes/config.php');
    include_once('includes/functions-db.php');

    $updateQuery = 'UDATE `sample_table` SET `date` = NOW() WHERE `id` > 10';

    $result = new Query($updateQuery,$sql);
    if($result->Error()) echo $result->Error();
    else {
        // this will tell us how many rows were updated
        echo $result->Affected();
        $result->Free();
    }

    $result->Close($sql);
?>

Super easy eh?


How about pulling data from the database? That's just as easy!

Counting Records

To find out how many records a query returns we would add this function to our Query class in the functions-db.php file.

PHP

function NumRows() {
    return mysql_num_rows($this->action);
}

If I wanted to see how many records have an id larger than 5 I could use this new sample file:

PHP

<?php
    # sample3.php file

    include_once('includes/config.php');
    include_once('includes/functions-db.php');

    $selectQuery = 'SELECT * FROM `sample_table` WHERE `id` > 5';

    $result = new Query($selectQuery,$sql);
    if($result->Error()) echo $result->Error();
    else {
        // this will tell us how many rows were selected
        echo $result->NumRows();
        $result->Free();
    }

    $result->Close($sql);
?>

Using The Data Retrieved

Last but not least let's retrieve the actual data being pulled from the database. First add one last function to the Query class in the functions-db.php file.

PHP

function FetchAssoc() {
    return mysql_fetch_assoc($this->action);
}

And in two more sample files I'll demonstrate using the function to retrieve, first, one record; then, looping through an unknown number of records.

PHP

<?php
    # sample4a.php file
    # retrieving one record

    include_once('includes/config.php');
    include_once('includes/functions-db.php');

    $selectQuery = 'SELECT * FROM `sample_table` WHERE `id` = 3 LIMIT 1';

    $result = new Query($selectQuery,$sql);
    if($result->Error()) echo $result->Error();
    else {
        // print our results
        $row = $result->FetchAssoc();
        echo "id is: " . $row['id'] . "<br />\n";
        echo "name is: " . $row['name'] . "<br />\n";
        echo "date is: " . $row['date'] . "<br />\n";
        $result->Free();
        unset($row);
    }

    $result->Close($sql);
?>

PHP

<?php
    # sample4b.php file
    # retrieving and displaying multiple record

    include_once('includes/config.php');
    include_once('includes/functions-db.php');

    $selectQuery = 'SELECT * FROM `sample_table` WHERE `date` < NOW()';

    $result = new Query($selectQuery,$sql);
    if($result->Error()) echo $result->Error();
    else {
        // print our results
        echo "<table><tr><th>id</th><th>name</th><th>date</th></tr>";
        while($row = $result->FetchAssoc()){
            echo "<tr>";
            echo "<td>" . $row['id'] . "<td>\n";
            echo "<td>" . $row['name'] . "<td>\n";
            echo "<td>" . $row['date'] . "<td>\n";
            echo "</tr>";
        }
        echo "</table>";
        $result->Free();
        unset($row);
    }

    $result->Close($sql);
?>

Quick and easy. Just the way I like it!

Download

Download all the sample files and the functions-db.php file to play with! (zip)
BONUS: Now includes functions for constructing easy insert and update queries from Making MySQL Update and Insert Easier

NOTE: Download does not include the sample database or table used in the PHP sample scripts, those are reference of how to call the functions in the class.

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Similar Entries
PHP Serialize, What’s it Do? What’s it For?
This entry is part 3 of 5 in the series Database PlaygroundDatabase Playground Series IndexUS Census Data as a MySQL Database PlaygroundPopulating the Regions, Divisions and States MySQL TablesMySQL Queries Made Easy With PHP Functions LibraryDatabase Playground - Temporary HiatusMaking MySQL Update and Insert EasierAn explanation of how the PHP serialize() and unserialize() functions can
Making MySQL Update and Insert Easier
This entry is part 3 of 5 in the series Database PlaygroundDatabase Playground Series IndexUS Census Data as a MySQL Database PlaygroundPopulating the Regions, Divisions and States MySQL TablesMySQL Queries Made Easy With PHP Functions LibraryDatabase Playground - Temporary HiatusMaking MySQL Update and Insert EasierAn addition to our PHP/MySQL Query class created last month now
Populating the Regions, Divisions and States MySQL Tables
This entry is part 3 of 5 in the series Database PlaygroundDatabase Playground Series IndexUS Census Data as a MySQL Database PlaygroundPopulating the Regions, Divisions and States MySQL TablesMySQL Queries Made Easy With PHP Functions LibraryDatabase Playground - Temporary HiatusMaking MySQL Update and Insert EasierThe relationships between states, divisions and regions are simple but need
US Census Data as a MySQL Database Playground
This entry is part 3 of 5 in the series Database PlaygroundDatabase Playground Series IndexUS Census Data as a MySQL Database PlaygroundPopulating the Regions, Divisions and States MySQL TablesMySQL Queries Made Easy With PHP Functions LibraryDatabase Playground - Temporary HiatusMaking MySQL Update and Insert EasierSomehow I ended up at the 1990 US census and I
Birthday MySQL Query Hates Timestamps
This entry is part 3 of 5 in the series Database PlaygroundDatabase Playground Series IndexUS Census Data as a MySQL Database PlaygroundPopulating the Regions, Divisions and States MySQL TablesMySQL Queries Made Easy With PHP Functions LibraryDatabase Playground - Temporary HiatusMaking MySQL Update and Insert EasierIn order to query the users with birthdays from my MySQL
Next Post
.htaccess Redirect a Directory to a Subdomain and Force WWW
Previous Post
Populating the Regions, Divisions and States MySQL Tables

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

Be the first to leave a comment!