You are on page 1of 17

PHP AND MYSQL

PHP is the most popular scripting language for web development. It


is free, open source and server-side (the code is executed on the
server).
MySQL is a freely available open source Relational Database
Management System (RDBMS) that uses Structured Query
Language (SQL). SQL is the most popular language for adding,
accessing and managing content in a database. It is most noted for its
quick processing, proven reliability, ease and flexibility of use.
PHP + MySQL Database System
PHP combined with MySQL are cross-platform (you can develop in
Windows and serve on a Unix platform)

mysql_affected_rows Get number of affected rows in previous MySQL operation


mysql_close Close MySQL connection
mysql_connect Open a connection to a MySQL Server
mysql_create_db Create a MySQL database
mysql_db_name Retrieves database name from the call to mysql_list_dbs
mysql_db_query Selects a database and executes a query on it
mysql_drop_db Drop (delete) a MySQL database
mysql_errno Returns the numerical value of the error message from previous
MySQL operation
mySql_error Returns the text of the error message from previous MySQL
operation
mysql_fetch_array Fetch a result row as an associative array, a numeric array, or
bothmysql_fetch_object Fetch a result row as an object
mysql_fetch_row Get a result row as an enumerated array
mysql_field_flags Get the flags associated with the specified field in a result
mysql_field_len Returns the length of the specified field
mysql_field_name Get the name of the specified field in a result

mysql_field_type Get the type of the specified field in a result


mysql_free_result Free result memory
mysql_get_client_info Get MySQL client info
mysql_get_host_info Get MySQL host info
mysql_get_proto_info Get MySQL protocol info
mysql_get_server_info Get MySQL server info
mysql_info Get information about the most recent query
mysql_insert_id Get the ID generated in the last query
mysql_list_dbs List databases available on a MySQL server
mysql_list_fields List MySQL table fields
mysql_list_processes List MySQL processes
mysql_list_tables List tables in a MySQL database
mysql_num_fields Get number of fields in result
mysql_num_rows Get number of rows in result
mysql_pconnect Open a persistent connection to a MySQL server
mysql_ping Ping a server connection or reconnect if there is no connection
mysql_query Send a MySQL query
mysql_real_escape_string Escapes special characters in a string for use in
an SQL statement
mysql_result Get result data

<?php
// we connect to example.com and port 3307
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
// we connect to localhost at port 3307
$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

MySQLi extension (the "i" stands for improved)


PDO (PHP Data Objects)

Open a Connection to MySQL


Example (MySQLi Object-Oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Example (MySQLi Procedural)


<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

Example (PDO)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username,
$password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>

Close the Connection


The connection will be closed automatically when the script ends. To close
the connection before, use the following:
Example (MySQLi Object-Oriented)
$conn->close();
Example (MySQLi Procedural)
mysqli_close($conn);
Example (PDO)
$conn = null;

Create a MySQL Database


Example (MySQLi Object-oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>

Create a MySQL Table Using MySQLi


SYNATX:
CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
reg_date TIMESTAMP
)
// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
if ($conn->query($sql) === TRUE) {
echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . $conn->error;
}

Insert Data Into MySQL Using MySQLi


Here are some syntax rules to follow:
The SQL query must be quoted in PHP
String values inside the SQL query must be quoted
Numeric values must not be quoted
The word NULL must not be quoted
SYNATX:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

Select Data From a MySQL Database


SYNTAX

The SELECT statement is used to select data from one or more tables:
SELECT column_name(s) FROM table_name
or we can use the * character to select ALL columns from a table:
SELECT * FROM table_name
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " .
$row["lastname"]. "<br>";
}
} else {
echo "0 results";
}

Delete Data From a MySQL Table Using MySQLi


The DELETE statement is used to delete records from a table:
SYNTAX
DELETE FROM table_name WHERE some_column = some_value
// sql to delete a record
$sql = "DELETE FROM MyGuests WHERE id=3";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}

Update Data In a MySQL Table Using MySQLi and PDO


The UPDATE statement is used to update existing records in a table:
SYNTAX
UPDATE table_name SET column1=value, column2=value2,...
WHERE some_column=some_value
$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}

Basically when you run httpClient.execute it will return a response, you need
to use that response.
Client side:
HttpResponse resp = httpClient.execute( post );
DataInputStream is = new DataInputStream( resp.getEntity().getContent() );
Server side depends on what programming language you use. For
example using python:
self.response.headers['Content-Type'] = 'text/vnd.aexp.json.resp'
self.response.set_status( 200,"OK" )
self.response.out.write( json.dumps( responseList ) )

THANK YOU

You might also like