Opening a connection to MySQL
Syntax:
mysqli_connect(host,username,password,dbname,port,socket);
Returns an object representing the connection to the MySQL server.
Example:
$conn = mysqli_connect("localhost","my_user","my_password","my_db");
|
mysqli_error()
Return the last error description for the most recent function call, if any.
Syntax:
mysqli_error(connection);
Example;
echo("Error description: " . mysqli_error($conn));
|
Creating a Databasse
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Create database
$sql = "CREATE DATABASE myDB";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
|
Selecting Data
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
|
mysqli_num_rows()
Return the number of rows in a result set
Syntax:
mysqli_num_rows(result);
Example:
$rowcount=mysqli_num_rows($result);
|
mysqli_fetch_assoc()
Fetch a result row as an associative array.
|
|
|
Closing a connection of MySQL
Syntax:
mysqli_close(connection);
Example:
mysqli_close($conn);
|
mysqli_connect_error()
Return an error description from the last connection error, if any.
Syntax:
mysqli_connect_error();
Example:
die("Connection error: " . mysqli_connect_error());
|
Create a Table
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 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 (mysqli_query($conn, $sql)) {
echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . mysqli_error($conn);
}
mysqli_close($conn);
|
Selecting a Database
Syntax:
mysqli_select_db(connection,dbname);
Example:
mysqli_select_db($conn,"test");
|
mysqli_query()
Perform queries against the database.
For successful SELECT, SHOW, DESCRIBE, or EXPLAIN queries it will return a mysqli_result object. For other successful queries it will return TRUE. FALSE on failure.
Syntax:
mysqli_query(connection,query,resultmode);
Examples:
mysqli_query($con,"SELECT * FROM Persons");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age) VALUES ('Glenn','Quagmire',33)");
|
Inserting into Table
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
|
|