Download ppt - OO Mysqli

Transcript
Page 1: OO Mysqli

2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO Mysqli

OO Mysqli

Page 2: OO Mysqli

2010/11 : [2]Building Web Applications using MySQL and PHP (W1)OO Mysqli

Object Orientated mysqli

• All the examples so far have used procedural mysqli functions.

• The mysqli extension also supports an object orientated syntax.

• This object orientated syntax is often more compact and easier to follow.

Page 3: OO Mysqli

2010/11 : [3]Building Web Applications using MySQL and PHP (W1)OO Mysqli

$db = new mysqli(

'mysqlsrv.dcs.bbk.ac.uk',

'my_username',

'my_password',

'my_db_name');

/* check connection */

if($db->connect_errno){

exit($db->connect_error);

}

Connect Object-Orientated

Page 4: OO Mysqli

2010/11 : [4]Building Web Applications using MySQL and PHP (W1)OO Mysqli

/* add student */

$sql = "INSERT INTO students

VALUES ('Jane',26,'female')";

if(!$db->query($sql))

{

exit($db->error);

}

Write Query Object-Orientated

Name Age Sex

Jane 26 female

Table students

Page 5: OO Mysqli

2010/11 : [5]Building Web Applications using MySQL and PHP (W1)OO Mysqli

/* get all students */$result = $db->query("SELECT name,age FROM students");/* check query */if($result === false) {

exit($db->error);}/* fetch associative array */while($row = $result->fetch_assoc()){

echo $row['name'].', '.$row['age'].' yrs old';}/* free result set */$result->close();

Read Query Object-Orientated

Name Age Sex

... ... ...

Table students


Recommended