php 5th unit
php 5th unit
After entering all the relevant conditions, you need to click on Update
Query button for doing the SQL operation. Next step is to select all the tables
from Use Tables section used in the query and then click on Submit Query.
phpMyAdmin Operations: In the phpMyAdmin tool, different operations can
be performed on the database or on a separate table. Select the database for which
you want to perform any operation and click the Operations tab. It is allowed to
create a new table under the selected database by using Create table section. You
can also rename the selected database by using Rename database to section.
Similarly you also have Remove database to delete database and Copy database
to sections which can be used as required for any SQL operation.
SELECT column_name
FROM table_name;
UPDATE table_name
[COLLATE collation_name ]
column_1 datatype,
column_2 datatype,
column_3 datatype
);
Procedural style:
mysqli_affected_rows(connection)
Example:
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
Procedural style:
mysqli_autocommit(connection, mode)
Example:
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
// Commit transaction
if (!$mysqli -> commit()) {
echo "Commit transaction failed";
exit();
}
Procedural style:
mysqli_character_set_name(connection)
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
4)The commit() / mysqli_commit() function: commits the current transaction for the specified
database connection.
Procedural style:
mysqli_commit(connection, flags, name)
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
// Commit transaction
if (!$mysqli -> commit()) {
echo "Commit transaction failed";
exit();
}
5)The error / mysqli_error() function:returns the last error description for the most recent
function call.
Procedural style:
mysqli_error(connection)
Example:
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
6)The fetch_all() / mysqli_fetch_all() function:fetches all result rows and returns the result-
set as an associative array, a numeric array, or both.
Procedural style:
mysqli_fetch_all(result, resulttype)
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
// Fetch all
$result -> fetch_all(MYSQLI_ASSOC);
Procedural style:
mysqli_fetch_assoc(result)
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
// Associative array
$row = $result -> fetch_assoc();
printf ("%s (%s)\n", $row["Lastname"], $row["Age"]);
Procedural style:
mysqli_get_charset(connection);
Example:
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
Procedural style:
mysqli_stmt_init(connection)
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
$city="Sandnes";
// Execute query
$stmt -> execute();
// Fetch value
$stmt -> fetch();
// Close statement
$stmt -> close();
}
10) The rollback() / mysqli_rollback() function rolls back the current transaction for the
specified database connection.
Procedural style:
mysqli_rollback(connection, flags, name)
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
// Commit transaction
if (!$mysqli -> commit()) {
echo "Commit transaction failed";
exit();
}
// Rollback transaction
$mysqli -> rollback();
In this, and in the following chapters we demonstrate three ways of working with PHP and
MySQL:
MySQLi (object-oriented)
MySQLi (procedural)
PDO
<?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";
?>
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
The connection will be closed automatically when the script ends. To close the
connection before, use the following:
$conn->close();
*MySQLi Procedural:
mysqli_close($conn);
You will need special CREATE privileges to create or to delete a MySQL database.
// 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();
?>
A database table has its own unique name and consists of columns and rows.
We will create a table named "MyGuests", with five columns: "id", "firstname", "lastname",
"email" and "reg_date":
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (mysqli_query($conn, $sql)) {
echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
After a database and a table have been created, we can start adding data in them.
*The INSERT INTO statement is used to add new records to a MySQL table:
The following example selects the id, firstname and lastname columns from the MyGuests
table and displays it on the page:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
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";
}
$conn->close();
?>
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
Example (MySQLi Object-oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
In any case, the PHP function for executing a query is mysql_query(): $result =
mysql_query($query); For simple queries like INSERT, UPDATE, DELETE, etc. (which do
not return records), the $result variable will be either TRUE or FALSE based upon the
successful execution of the query on the database.
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
// Check connection
if ($mysqli -> connect_errno) {
// Perform query
if ($result = $mysqli -> query("SELECT * FROM Persons")) {
echo "Returned rows are: " . $result -> num_rows;
// Free result set
$result -> free_result();
}
Syntax
Object oriented style:
$mysqli -> query(query, resultmode)
Procedural style:
mysqli_query(connection, query, resultmode)