Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
36 views

MySQL Database Using PHP (Day-1)

This document discusses how to connect to and interact with a MySQL database from PHP. It covers connecting to the database, executing queries like SELECT statements, and retrieving and processing the results. Connecting involves calling mysqli_connect() and handling errors if the connection fails. Queries are executed using mysqli_query() and results are retrieved using functions like mysqli_fetch_array().
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

MySQL Database Using PHP (Day-1)

This document discusses how to connect to and interact with a MySQL database from PHP. It covers connecting to the database, executing queries like SELECT statements, and retrieving and processing the results. Connecting involves calling mysqli_connect() and handling errors if the connection fails. Queries are executed using mysqli_query() and results are retrieved using functions like mysqli_fetch_array().
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Accessing MySQL from

PHP
Outline

1. Connecting to MySQL
2. Executing Queries
3. Example: Adding a Record
4. Retrieving Query Results
PHP and Database
 Mysql – popular open-source database
management system
 PHP usually works with Mysql for web-based
database applications
 XAMP applications—Web-based applications
that use Cross Platform, Apache, Mysql, and
PHP.
Connecting to MySQL
PHP offers three different ways to connect to MySQL

• MySQL
• MySQL I. The i stands for improved.
• PDO : PHP Data Objects
Connecting to MySQL
<?php
//connect to database
$host = "localhost";
$username = "root";
$password = "";
$dbname = "students";
$connection = mysqli_connect($host,$username,$password,$dbname);

if(mysqli_connect_errno()){
die("database connection failed. Error Number:" .
mysqli_connect_errno()." Error Type.".mysqli_connect_error());
}
?>
Connecting to MySQL
• mysqli_connect() connects to a MySQL server running at $host
and selects the database $dbname Upon success, $connection
holds a reference to the database which is needed by
subsequent mysqli_* function calls.

• mysqli_connect_errno()  function returns the error code from


the last connection error, if any.

• mysqli_connect_error() returns a string containing a detailed


error message about the failed connection.
Executing Queries
Executing Queries
• Line 2: mysqli_query() sends a query to a server. It returns
TRUE (or a result object for SELECT, SHOW, DESCRIBE, and
EXPLAIN commands) on success or FALSE on failure.

• Line 8: mysqli_error() returns a string containing a detailed


error message about the failed query operation.

• Line 12: mysqli_close() closes the database connection.


Example: Adding a Record
Retrieving Query Results
Retrieving Query Results

• Line 2: A "SELECT" query returns a result set that contains


multiple rows. For such command, mysqli_query() returns a
reference to the result set.

• We can only retrieve one row of results at a time.

• Line 4: mysqli_fetch_array($result) returns the current row as


an array (which can be processed as an associative or a numeric
array). The function returns NULL when no more row is
available.
Retrieving Query Results
• Related functions:
• mysqli_fetch_row() – returns the result set as a numeric array.
• mysqli_fetch_assoc()– returns the result set as an associative array.
• mysqli_fetch_array()– returns the result set as an associative + numeric
array.
• mysqli_fetch_object() – returns the result set as an object

• Use different variables to hold the results of different queries.

You might also like