php mysqli tutorial for beginners
Now a days to develop complex web sites you must need database interaction. With PHP mostly we use MySQL. To access mysql database we can use mysqli or PDO libraries available in php. In this tutorial we are going to explore php mysqli library functions.
Mysqli extension can be used either in procedural or object oriented way. In procedural way functions are called, while in object oriented way a class object is used to perform operations on database. Mysqli extension was introduced in php version 5.PHP mysqli extension provides methods to perform different operations on database. In newer versions of PHP mysqli functions are recommended to connect, retrieve or save data to database. php mysqliextension supports object-oriented interface, prepared statements, multiple statements etc.
Connect to database using php mysqli
In order to connect to MySQL database using mysqli, mysqli_connect function is used, you need to provide host name, username, password and database name.
Connect to database using php mysqli – procedural way
In procedural way mysqli_connect function is used. mysql_connect function opens a connection to database server and connection object is returned. If connection to database fails mysqli_connect_errno()throws an error.
Connect to database using php mysqli – object oriented way
Object oriented way to connect to database, mysqli function is called and an object is returned. In case database connection is failed error is thrown.
Selecting records from database using php mysqli
Records from database can be selected using procedural way or object oriented way. mysqli_queryfunction is used to perform query on database.
First connect to database, connection object and a query is passed to mysqli_query and it returns a result set. Result set is passed to mysqli_fetch_assoc function and it returns a data row as an associative array.
Select records using mysqli_fetch_assoc – procedural way
mysql_fetch_assoc returns a result set as an associative array and names of column in result set represents the keys of array. Null is returned if result set is empty.
Fetch records using fetch_assoc – object oriented way
After database connection using new mysqli method, database connection object is returned. A query is passed to connection object‘s query method. This function returns a result set. Likewise procedural way a row from result set is fetched using fetch_assoc() method.
This method returns a single row of result, so we use a while loop to fetch all rows in result set. Column names are used as array indexes to access result like $row[‘first_name’].
Fetch records using mysqli_fetch_array – procedural way
mysqli_fetch_array function is used to fetch records from database. It fetches a single row from result set as numeric array using mysql_num, associative array using mysqli_assoc or both using mysqli-both. To display records from data row, array index or column name is used.
Fetch records using mysqli_result::fetch_array – object oriented way
fetch_array function can fetch result row as associative, numeric array or both. It is recommended way and works same as procedural way.Fetch all records using mysqli_fetch_all
If you need to fetch all records from database, mysqli_fetch_all function is used. First connection to database is created using mysqli_connect. Next we pass connection object and SQL query to mysqli_queryfunction.
mysqli_query function returns result set. mysqli_fetch_all function returns all result rows as an associative, numeric or both.Prepared statements in php mysqli library
php mysqli library supports prepared or parameterized statements. A prepared statement works in two steps, step 1 is prepare a statement while step 2 involves execution of a prepared statement.
In prepare stage a statement template is sent to server, syntax is validated and internal resources are allocated while in execution step client binds the parameter values and sends it to server.
A statement is created by server and values are bind and using assigned internal resources statement is executed. It can execute repeated statement repeatedly with efficiency.
Insert records to database using php mysqli prepared statements
First we make a connection to database. Then prepare method is used to prepares SQL statement. Parameter markers ” ? ” are used in prepared statements. Then prepare function returns a statement handle that is used for further processing.
bind_param method binds variables to prepared statement. In this function we pass data types of variables. Data types of variable can be i as integer, d as double, s as string and b as blob. Second parameters are the variables. The number of parameters and string type length should be same.
Prepared statement are executed by execute method. When this function is called ? placeholders or parameters markers are replaced by variable values.
Update records using php mysqli prepared statements
To update records first create a connection to database. Next SQL query is prepared using preparefunction for execution. Finally records are updated by execute function.
Delete records using prepared statement php mysqli
To delete records from database using prepared statements first connect to database. Next SQL deletestatement is prepared for execution. In where clause the ? parameter marker is used.
bind_param function binds value of $id parameter. Next prepared statement is executed by executemethod and record is deleted.
0 comments:
Post a Comment