HubsInfo Network

PHP MySQLi Procedural and Object-Oriented Interface

0 Members and 1 Guest are viewing this topic.

PHP MySQLi Procedural and Object-Oriented Interface
« on: February 15, 2016, 08:51:00 AM »

Object Oriented Interface
Code: [Select]
//connect to database
$mysqli = new mysqli("localhost", "username", "password", "database");

//check for errors
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error;

//write query
$res = $mysqli->query("SELECT  * from tablename");

//fetch query
$row = $res->fetch_assoc();

//display results
echo $row['table_id'];

Mysqli Procedural
Code: [Select]
//Connect to the database
$con=mysqli_connect("localhost", username", "password", "database") or die(mysqli_connect_error());

//Write query
$query="SELECT * from tablename";

//Fetch results
$result=mysqli_query($con, $query);

//Display results
//use row to fetch the element of each column

//close connection

Old way of accessing mysql database
Code: [Select]
//connect to database
$con = mysql_connect("localhost", "username", "password");

// select database
//$db= mysql_select_db("database") or die(mysql_error());

//Write query
$query= "SELECT* from tablename";

//Fetch Results

//Display result
// acccess the table elements and display it

//mysql close connection