PHP MySqli Basic usage (select, insert & update) - Mostlikers

31 July, 2013

PHP MySqli Basic usage (select, insert & update)

Mysqli is a database system used on the web its runs on a server side. Mysql query used to transaction the client data to store the server table. This is sort database storage it’s not possible millions of people access database so PHP team announced plans to deprecate MySQL extension alternatives on introduce Mysqli - "i" is standing improvement its based object oriented implementation.  MySQLi supports prepared statements, transactions and multiple statements. 

PHP MySqli Basic usage (select, insert & update)

How to connect database using Mysqli


In Previously using Mysql query connect  on database watch below coding difference on mysql and mysqli

Mysql Database connection
<?php 
$conn=new mysqli('localhost','user','password','database');
if($conn)
{
    echo "good";
}
else
{
    die(mysql_error());
}
?>

Mysqli Database connection
<?php 
$conn = new mysqli('localhost', 'user', 'password', 'database');if($conn->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}
?>

Mysqli Query extension similar to Mysql Query extension watch below Insert,View,Update,Delete coding its connections using on object oriented way.

Mysqli insert statement
Insert Post value in database this coding same to Mysql syntax using oops concept directly link on database file $conn->query("statement") in Mysql syntax as Mysql_query("statement") so just simple change the coding on oops based.

$insert=$conn->query("insert into tablename(field name) values('$post value')");
    if(!$insert)
    {
        die($insert->error);
    }

Mysqli fetch statement
Fetch syntax also same but object based fetch the statement little bit changed if you like fetch on Array method change  mysqli_fetch_array($sql) 

<?php 
$sql=$conn->query("SELECT * FROM `tablename`");
while($page=mysqli_fetch_object($sql))
{
    echo $page->table_feld;
}   
?>
Delete and update coding on same syntax way connect to change the query method

Escaping Characters
Without using escape string post  value  in  database $menu=$_POST['menu'];  its  not secure if any users enter single quotes or backslash on post value produce error, so avoid this type way using mysqli_real_escape_string($_POST['menu']); this is a correct statement method.
        
$name=mysqli_real_escape_string($_POST['name']);

Advantages of Mysqli
  • Mysqli is  support to the Embedded server 
  • concept based on oops interface 
  • Mysqli support multiple statement 
  • It's Enhanced debugging capabilities  
Don't using mysql functions its fully deprecate extension. try to mysqli function or PDO function its object based statement simplify the coding statement and improving the coding performance.

8 comments:

  1. Good overview, for security its better to use prepared statements then escaping them.

    ReplyDelete
  2. Try to install this widget in your blogger you get more response and share and comment easily dear brother.....

    http://disqus.com/admin/blogger/

    ReplyDelete
  3. Failed to connect to MySQL: User user_name already has more than 'max_user_connections' active connections

    how to solve this problem

    ReplyDelete