You are on page 1of 20

PHP/ MYSQL

SHAFIA Lecturer College of CS & IS

phpMyAdmin
A MySQL client written in PHP Via the web you can manage:
Manage Databases Manage MySQL users Submit queries (SQL)

A great way to learn SQL!

PHP MySQL Connect to a Database


Describes how to interact with MySQL databases by establishing a connection, making queries, manipulating the results of those queries, and closing the database connection.

Opening and Closing a MySQL Connection


Before you can access data in a database, you must create a connection to the database. Open a connection to a MySQL database server with the mysql_connect() function The mysql_connect() function returns a positive integer if it connects to the database successfully or FALSE if it does not Assign the return value from the mysql_connect() function to a variable

In PHP, this is done with the mysql_connect() function. Syntax


Parameter

Opening and Closing a MySQL Connection (continued)

Description mysql_connect(servername,username servername Optional. Specifies ,password); the server to connect to. Default value is "localhost:3306" username password

Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process Optional. Specifies the password to log in with. Default is ""

$connection = mysql_connect("host" [, "user", "password"]); Close a database connection using the mysql_close() function mysql_close($DBConnect);

Opening and Closing a MySQL Connection (continued)

Example
<?php $con = mysql_connect("localhost",root"); If (!$con) { die('Could not connect: ' . mysql_error()); } // some code mysql_close($con);

Reporting MySQL Errors


Reasons for not connecting to a database server include:
The database server is not running Insufficient privileges to access the data source Invalid username and/or password

Reporting MySQL Errors (continued)


The mysql_errno() function returns the error code from the last attempted MySQL function call or 0 if no error occurred The mysql_error() Returns the text of the error message from previous MySQL operation The mysql_errno() and mysql_error() functions return the results of the previous mysql*()

Selecting a Database
The syntax for the mysql_select_db() function is:
mysql_select_db(database [, connection]);

The function returns a value of TRUE if it successfully selects a database or FALSE if it does not For security purposes, you may choose to use an include file to connect to the MySQL server and select a database

Executing SQL Statements


Use the mysql_query() function to send SQL statements to MySQL The syntax for the mysql_query() function is:
mysql_query(query [, connection]);

The mysql_query() function returns one of three values:


For SQL statements that do not return results (CREATE DATABASE and CREATE TABLE statements) it returns a value of TRUE if the statement executes successfully

Executing SQL Statements


For SQL statements that return results (SELECT and SHOW statements) the mysql_query() function returns a result pointer that represents the query results
A result pointer is a special type of variable that refers to the currently selected row in a resultset

The mysql_query() function returns a value of FALSE for any SQL statements that fail, regardless of whether they return results

Example
<?php $con = mysql_connect("localhost","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test",$con); $result = mysql_query("SELECT * FROM studentinfo"); while($row = mysql_fetch_array($result)) { echo $row['rno']; echo "<br>".$row['name']; echo "<br>".$row['class'] ; echo "<br>". $row['semester']; } mysql_close($con); ?>

Adding, Deleting, and Updating Records


To add records to a table, use the INSERT and VALUES keywords with the mysql_query() function To add multiple records to a database, use the LOAD DATA statement with the name of the local text file containing the records you want to add To update records in a table, use the UPDATE statement

EXAMPE
<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Peter', 'Griffin', '35')"); mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire', '33')"); mysql_close($con); ?>

the table to update The SET keyword specifies the value to assign to the fields in the records that match the condition in the WHERE clause To delete records in a table, use the DELETE statement with the mysql_query() function Omit the WHERE clause to delete all records in a table

Adding, Deleting, and Updating Records (continued) the name of The UPDATE keyword specifies

EXAMPLE
<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query("UPDATE Persons SET Age = '36' WHERE FirstName = 'Peter' AND LastName = 'Griffin'"); mysql_close($con); ?>

Working with Query Results

Retrieving Records into an Indexed Array


The mysql_fetch_row() function returns the fields in the current row of a result set into an indexed array and moves the result pointer to the next row The mysql_fetch_assoc() function returns the fields in the current row of a resultset into an associative array and moves the result pointer to the next row The difference between mysql_fetch_assoc() and mysql_fetch_row() is that instead of returning the fields into an indexed array, the mysql_fetch_assoc() function returns the fields into an associate array and uses each field name as the array key

Retrieving Records into an Indexed Array


mysql_fetch_array(result) returns an array that is the result row, or NULL if it the last result is reached.
Its results in an array that contains the columns requested both by number and by column name:

while($columns=mysql_fetch_array( $result)) { echo 'name: '.$columns['name']; echo 'first column: .$columns[0];

You might also like