MySQL Record Operations

17

Click here to load reader

Transcript of MySQL Record Operations

Page 1: MySQL Record Operations

MySQL Record Operations

Jamshid HashimiTrainer, Cresco Solution

http://www.jamshidhashimi.com [email protected] @jamshidhashimi ajamshidhashimi

Afghanistan Workforce Development Program

Page 2: MySQL Record Operations

Agenda

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

Page 3: MySQL Record Operations

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.

Page 4: MySQL Record Operations

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

Page 5: MySQL Record Operations

MySQLi Introduction

• mysql_query() – Deprecated!

Page 6: MySQL Record Operations

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”]);}

Page 7: MySQL Record Operations

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”]);

Page 8: MySQL Record Operations

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);}

Page 9: MySQL Record Operations

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.

Page 10: MySQL Record Operations

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,...)

Page 11: MySQL Record Operations

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

Page 12: MySQL Record Operations

DELETE

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

• Syntax:

DELETE FROM table_nameWHERE some_column = some_value

Page 13: MySQL Record Operations

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.

Page 14: MySQL Record Operations

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();

Page 15: MySQL Record Operations

Managing Data

Page 16: MySQL Record Operations

DEMO

Page 17: MySQL Record Operations

QUESTIONS?