Using the mysqli Object-Oriented API : mysqli « MySQL Database « PHP

Home
PHP
1.Chart
2.Class
3.Components
4.Cookie Session
5.Data Structure
6.Data Type
7.Date
8.Design Patterns
9.Development
10.DNS
11.Email
12.File Directory
13.Form
14.Functions
15.Graphics Image
16.HTML
17.Language Basics
18.Login Authentication
19.Math
20.MySQL Database
21.Network
22.Operator
23.PDF
24.Reflection
25.Statement
26.String
27.Utility Function
28.Web Services SOAP WSDL
29.XML
PHP » MySQL Database » mysqli 
Using the mysqli Object-Oriented API
 
<?php 
$mysqli = new mysqli ("localhost","root","","mydatabase")
try 
    if (mysqli_connect_errno()){ 
        throw new exception ("Error: " . mysqli_connect_errno() " - ". mysqli_connect_error())
    
    else 
        if ($cdquery = $mysqli->query ("SELECT * FROM mytable ORDER BY id ASC")){ 
    
            while ($cddata = $cdquery->fetch_array ()){ 
                echo "ID: " . $cddata['id'] "<br />"
                echo "Title: " . stripslashes ($cddata['title']) "<br />"
                echo "MyValue: " . stripslashes ($cddata['MyValue']) "<br />"
            
            $cdquery->close()
        else 
            echo $mysqli->errno . " - " . $mysqli->error; 
        
    
        $prep = $mysqli->prepare ("INSERT INTO mytable(id,title,myvalue) VALUES ('0',?,?)")
        $prep->bind_param ('ss',$title,$myvalue)
        
        $title = "AA"
        $myvalue = 20
        
        $prep->execute()
        
        echo $prep->affected_rows . " row(s) affected."
        $prep->close()
        if ($result = $mysqli->prepare ("SELECT title, myvalue FROM mytable WHERE id > 2")){ 
            $result->execute ()
            $result->bind_result ($title,$myvalue)
            while ($result->fetch ()){ 
                echo "Title: " . stripslashes ($title"<br />"
                echo "MyValue: " . stripslashes ($myvalue"<br />"
            
            $result->close ()
        else 
            echo $mysqli->errno . " - " . $mysqli->error; 
        
        $mysqli->close()
    
catch (exception $e) { 
echo $e->getmessage()

?>
  
  
Related examples in the same category
1.Multiple Queries Using MySQLi
2.Parameter Binding in MySQLi
3.Use mysqli to fetch data from mysql
4.Using the Object-Oriented Syntax in MySQLi
5.mysqli_fetch_object.php
6.mysqli_fetch_result-2.php
7.mysqli_fetch_result.php
8.mysqli_fetch_row.php
9.mysqli_free_result.php
10.mysqli_multi_query.php
11.mysqli_stmt_prepare.php
12.mysqli_store_result.php
13.retrieving_multiple_rows.php
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.