PHP Script – Using MySQLi

MySQLi is improved extension of MySQL extension. It suits well for InnoDB MySQL database because MySQLi provides transaction processing functions to keep your database accurate and reliable. In this article, we will show you how to utilize most of this MySQLi extension, such as: connecting and disconnecting from database server, perform transaction processing, executing sql queries, retrieving database records, counting selected records.

Source Code

<?php
    //By    : Xhanch Studio
    //URL   : http://xhanch.com/

    //Specify your MySQL database server (Can be the server name or IP Address)
    $db_server = 'localhost';
    //Specify your database name
    $db_name = 'database_name';
    //Specify your database user
    $db_user = 'username';
    $db_pass = 'password';

    //Set up a connection to MySQL database server
    $conn = @mysqli_connect(
        $db_server,
        $db_user,
        $db_pass,
        $db_name
    );

    //Checking connection state
    if(!$conn){
        //Failed to connect to MySQL database server. May be caused by incorrect server name,
        //database name, database user or password
        die('Cannot connect to database server !');
    }

    //Executing an SQL query
    $sql = 'write your query here';
    $res = @mysqli_query($conn,$sql);
    if(!$res){
        //Failed to execute query, may be caused by false query, incorrect query syntax
        die('Query error');
    }

    //If you want to know how many records were affected beacuse of the execution
    //of a query
    echo mysqli_affected_rows($conn);

    //If you are inserting a new record to a table that have an auto number field
    //you can get the value for that field after a record has been inserted
    echo mysqli_insert_id($conn);

    //Retrieving database records
    $sql = 'write your select query here';
    $res = @mysqli_query($conn,$sql);
    if(!$res){
        //Failed to execute query, may be caused by false query, incorrect query syntax
        die('Query error');
    }
    //Query successfully executed
    //If you want to know the result count
    echo mysqli_num_rows($res);
    //Start reading results
    while($row = mysqli_fetch_array($res)){
        //Dumping/displaying current record/row
        var_dump($row);

        //To display a particular field, you may use this syntax
        echo $row['field_name'];
    }
    //Free memory from storing query result
    mysqli_free_result($res);

    //Perform a transaction processing
    //Disable auto commit
    mysqli_autocommit($conn, false);
    try{
        $sql = 'write query 1 here';
        $res = @mysqli_query($conn,$sql);
        if(!$res){
            //Failed to execute query, may be caused by false query, incorrect query syntax
            throw new Exception(mysqli_error($conn));
        }

        $sql = 'write query 2 here';
        $res = @mysqli_query($conn,$sql);
        if(!$res){
            //Failed to execute query, may be caused by false query, incorrect query syntax
            throw new Exception(mysqli_error($conn));
        }

        $sql = 'write a query 3 here';
        $res = @mysqli_query($conn,$sql);
        if(!$res){
            //Failed to execute query, may be caused by false query, incorrect query syntax
            throw new Exception(mysqli_error($conn));
        }

        //All queries have been successfully executed
        //Commit all queries/operations
        mysqli_commit($conn);
    }catch(Exception $e){
        //Error occurred in one of your queries
        //Roll back all executed queries/operations
        mysqli_rollback($conn);
        //Displaying error
        die($e->getMessage());
    }
    //Enable auto commit
    mysqli_autocommit($conn, true);

    //Closing MySQL connection
    mysqli_close($conn);
?>

Incoming Search Terms

start readingresults com, mysqli disconnect, mysqli_disconnect, mysqli script, mysqli count(*), mysqli_query syntax, mysqli recordcount, mysqli disconnect php, manga php script, mysqli_query exception, php mysqli disconnect, mysqli record count, start readingresult com, using mysqli, mysqli auto free, mysqli retrieve records, mysqli count(*) php, php mysqli script, php script mysqli, mysqli connect script, how to properly disconnect with mysqli, script mysqli, start readingresult, php mysqli count(*), php mysqli count *, php manga script free, mysqli_disconnect php, mysql_disconnect php, using mysqli queries, php mysqli recordcount, php mysqli_disconnect, php mysqli_query exception, php recordcount mysqli, php script for online recording studio, php script using mysqli, recordcount using mysqli, recourd count mysqli, retrieve records using mysqli, user script mysqli, $res = mysqli_query($conn $sql);, mysqli count -1, a free project write with mysqli, calling mysqli::disconnect(), count(*) php mysqli, espace membre php 5 mysqli, free php MySQLi script, free script php mysqli, fungsi syntak php, how to select row mysqli php, manga php mysql code, manga script php free, mysqli auto rollback, mysqli count, mysqli_disconnect disconnect, mysqli disconnect all connections, mysqli disconnect server, mysqli disconnect(), mysqli execute script, mysqli how to rollback, mysqli php, mysqli php manual, mysqli php test script, mysqli scripte, mysqli sql script, mysqlicount, mysqli_ recordcount, Xhanch, Xhanch Studio


Posted on 2010-03-18 by Susanto BSc in Free Script, PHP Script
Data, Free, Function, Database, Script, Code, Guide, Connect, Connection, Mysqli, Class, Mysql, Query, Sql, Php, Xhanch, Xhanch Studio

Tagged as: , , , , , , , , , , , , , ,

Leave a Reply

You must be logged in to post a comment.