PHP functions for working with MySQL database. Code examples open

PHP functions for working with MySQL database. Code examples

Tested: PHP 5.6 +

mysqli_report – sets the mysqli error reporting mode

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

MYSQLI_REPORT_ERROR – log mysqli function call errors
MYSQLI_REPORT_STRICT – Throw mysqli_sql_exception instead of error messages

Establish a connection to the database if an error occurs to finish the file and output the error

$db = mysqli_connect('localhost', 'root', 'root', 'users') or die('Error');

mysqli_query – Executes a database query

$res = mysqli_query($db, "SELECT Code, Name FROM country LIMIT 10");

Read more – https://www.php.net/manual/ru/mysqli.query.php

mysqli_fetch_all – selects all rows from table the result set and places them in an associative array

mysqli_fetch_all($res, MYSQLI_ASSOC);

mysqli_fetch_assoc – selects one row of data from the result set and returns it as an associative array. Each subsequent call to this function will return the next row in the result set, or null if there are no more rows.

//connect to database
$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world");
//create query
$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC";
//use request
$result = mysqli_query($mysqli, $query);
//create an associative array from our data
mysqli_fetch_assoc($result)

mysqli_real_escape_string – escapes special characters in a string for use in an SQL statement using the connection’s current character set


//create database connection
$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world");

//we write down our variable which then we will insert in SQL query
$city = "'s-Hertogenbosch";

//Let's write our SQL query first by formatting it
$query = sprintf("SELECT CountryCode FROM City WHERE name='%s'",

//escapes special characters like (ASCII 0), \n, \r, \, ', ",
mysqli_real_escape_string($mysqli, $city));

//create request
$result = mysqli_query($mysqli, $query);
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation