MySQL Record Operations

Post on 10-May-2015

400 views 1 download

Transcript of MySQL Record Operations

MySQL Record Operations

Jamshid HashimiTrainer, Cresco Solution

http://www.jamshidhashimi.com jamshid@netlinks.af @jamshidhashimi ajamshidhashimi

Afghanistan Workforce Development Program

Agenda

• MySQLi Introduction• mysqli() Overview• mysqli Methods and Properties• INSERT, UPDATE & DELETE• mysqli Prepared Statements• Inserting, Updating and Deleting Records• Managing Data

MySQLi Introduction

• MySQLi, often called MySQL Improved, has several advantages over regular MySQL, including support for prepared statements (which helps prevent SQL injection, a common security issue) and object-oriented code.

MySQLi Introduction

• The mysqli extension has a number of benefits, the key enhancements over the mysql extension being:– Object-oriented interface– Support for Prepared Statements– Support for Multiple Statements– Support for Transactions– Enhanced debugging capabilities– Embedded server support

MySQLi Introduction

• mysql_query() – Deprecated!

MySQLi

• mysqli_fetch_assoc (fetch_assoc())– Fetch a result row as an associative array

/* fetch associative array */while ($row = $result->fetch_assoc()) {

printf ("%s \n", $row["Name”]);}

MySQLi

• mysqli_fetch_array (fetch_array())– Fetch a result row as an associative, a numeric

array, or both– MYSQLI_NUM– MYSQLI_ASSOC– MYSQLI_BOTH

/* associative array */$row = $result->fetch_array(MYSQLI_ASSOC);printf ("%s\n", $row["Name”]);

MySQLi

• mysqli_fetch_object (fetch_object())– Returns the current row of a result set as an

object

/* fetch object array */while ($obj = $result->fetch_object()) {

printf ("%s \n", $obj->Name);}

MySQLi

• Do we need to close the connection?– Open connections (and similar resources) are

automatically destroyed at the end of script execution. However, you should still close or free all connections, result sets and statement handles as soon as they are no longer required. This will help return resources to PHP and MySQL faster.

INSERT

• The INSERT INTO statement is used to insert new records in a table.

• Syntax:

INSERT INTO table_name (column1, column2, column3,...)VALUES (value1, value2, value3,...)

UPDATE

• The UPDATE statement is used to modify data in a table.

• Syntax:

UPDATE table_nameSET column1=value, column2=value2,...WHERE some_column=some_value

DELETE

• The DELETE statement is used to delete records in a table.

• Syntax:

DELETE FROM table_nameWHERE some_column = some_value

Prepared Statements

• Prepared statements provide developers with the ability to create queries that are more secure, have better performance, and are more convenient to write.

Prepared Statementsif($rs = $mysqli->prepare("SELECT first_name FROM actor WHERE first_name = ?")){

//binding parameteres$rs->bind_param("s",$name);$name = "BON";//Execute it$rs->execute();//Bind results$rs->bind_result($name);//Fetch records$rs->fetch();printf("%s", $name);

$rs->close();}else{

echo $mysqli->error;}$mysqli->close();

Managing Data

DEMO

QUESTIONS?