PHP Classes

File: index.php

Recommend this page to a friend!
  Packages of Martinescu Petrica   MySQL object approach   index.php   Download  
File: index.php
Role: Example script
Content type: text/plain
Description: Some general examples for using this class
Class: MySQL object approach
Execute common MySQL database queries
Author: By
Last change:
Date: 19 years ago
Size: 3,424 bytes
 

Contents

Class file image Download
<?
    # include mysql class
    require 'mysql.class.php';

    # mysql connection variables
    $db_server="localhost";
    $db_user="root";
    $db_pass="";
    $db_name="mysql_class";

    # declare an instance of our class and make the connection to our database
    $db=new mysql();
    $db->connect($db_server,$db_user,$db_pass);
    if ($db->errno==0)
    {
        $db->select($db_name);
        if ($db->errno!=0)
        {
            echo "No database!";
            exit();    
        }
    }
    else
    {
        echo "No access rights!";
        exit();
    }


    ##########################################################
    # example 1
    echo "Example 1<br/>";

    # insert a row
    
    # declare a new instance of our class
    $mysql=new mysql('first_table');

    # executing an insert query
    $mysql->sql("insert into %tbl (name,field) values ('Peter','IT')");

    # checking for errors
    if ($mysql->errno==0)
    {
        # displaying the last inserted id
        $last_insert=$mysql->insert_id;
        echo "Your new row has id=".$last_insert;
    }
    else
    {
        # we have an error in our query
        echo "MySQL error: ".$mysql->error;
    }

    echo "<br/>";
    echo "<br/>";


    ###########################################################
    # example 2
    echo "Example 2<br/>";

    # fetch a single row
    
    # declare a new instance of our class
    $mysql=new mysql('first_table');

    # fetch the row with id=2
    $mysql->load(2);

    # display fields content
    echo $mysql->id;
    echo "<br/>";
    echo $mysql->name;
    echo "<br/>";
    echo $mysql->field;

    echo "<br/>";
    echo "<br/>";


    ##########################################################
    # example 3
    echo "Example 3<br/>";

    # delete a row
    
    # declare a new instance of our class
    $mysql=new mysql('first_table');

    # delete the row inserted last, the first example
    $mysql->delete($last_insert);

    echo "Deleted the row with id=".$last_insert;

    echo "<br/>";
    echo "<br/>";


    ##########################################################
    # example 4
    echo "Example 4<br/>";

    # select more rows
    
    # declare a new instance of our class
    $mysql=new mysql('first_table');

    # proceed with the query
    $mysql->sql("select * from %tbl where name='john' || name='alex'");

    if ($mysql->errno==0)
    {
        # echo the number of rows affected by the last query
        echo "Rows affected: ";
        echo $mysql->count;

        echo "<br/>";

        # now we go through all the selected rows and display the name
        foreach($mysql->rows as $k=>$row)
        {
            echo $k.". ".$row->name."<br/>";
        }
    }
    else
    {
        # we have an error in our query
        echo "MySQL error: ".$mysql->error;
    }

    echo "<br/>";
    echo "<br/>";


    ##########################################################
    # example 5
    echo "Example 5<br/>";

    # select one row with the sql function
    
    # declare a new instance of our class
    $mysql=new mysql('first_table');

    # execute query
    $mysql->sql("select count(*) as total from %tbl where field='IT'");

    # nou echo the result
    echo $mysql->total;

    #this final example shows you that when your result is only one row, 
    #then the fields returned by the query ar directly included in the object,
    #but also in the rows array.

    echo "<br/>";
    echo "<br/>";


    ##########################################################
    # example 6
    echo "Example 6<br/>";

    # shows you a way to display all the content of the object
    echo "<pre>";
    print_r($mysql);
    echo "</pre>";
?>