100% found this document useful (1 vote)
95 views2 pages

Ajay Kumar B - PHP Mysqli Procedural

This document is a cheat sheet for using MySQLi procedural functions in PHP. It provides code examples for connecting to a MySQL database, executing queries to select, insert, and manipulate data, and includes functions for creating databases and tables. Key functions covered are mysqli_connect() for opening a connection, mysqli_query() for executing queries, and mysqli_fetch_assoc() for retrieving rows from a result set. The cheat sheet acts as a quick reference guide for common MySQLi tasks in PHP like selecting data, inserting records, and checking for errors.

Uploaded by

James Rodrick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
95 views2 pages

Ajay Kumar B - PHP Mysqli Procedural

This document is a cheat sheet for using MySQLi procedural functions in PHP. It provides code examples for connecting to a MySQL database, executing queries to select, insert, and manipulate data, and includes functions for creating databases and tables. Key functions covered are mysqli_connect() for opening a connection, mysqli_query() for executing queries, and mysqli_fetch_assoc() for retrieving rows from a result set. The cheat sheet acts as a quick reference guide for common MySQLi tasks in PHP like selecting data, inserting records, and checking for errors.

Uploaded by

James Rodrick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

PHP MySQLi Procedural Cheat Sheet

by Ajay Kumar B via cheatography.com/30523/cs/9105/

mysqli​_nu​m_r​ows() Creating a Databasse (cont)

Return the number of rows in a result set } else {


Syntax: ​ ​ ​ echo "​Error creating database: " .
mysqli​_nu​m_r​ows​(re​sult); mysqli​_er​ror​($c​onn);
Example: }
​$ro​wco​unt​=my​sql​i_n​um_​row​s($​res​ult); mysqli​_cl​ose​($c​onn);

mysqli​_fe​tch​_as​soc() Selecting Data

Fetch a result row as an associative array. // Create connection


$conn = mysqli​_co​nne​ct(​$se​rve​rname, $username,
Opening a connection to MySQL $password, $dbname);
// Check connection
Syntax:
if (!$conn) {
mysqli​_co​nne​ct(​hos​t,u​ser​nam​e,p​ass​wor​d,d​bna​me,​por​t,s​ock
​ ​ ​ ​die​("Co​nne​ction failed: " .
et);
mysqli​_co​nne​ct_​err​or());
Returns an object repres​enting the connection to the
}
MySQL server.
$sql = "​SELECT id, firstname, lastname FROM
Example:
MyGues​ts";
$conn =
$result = mysqli​_qu​ery​($conn, $sql);
mysqli​_co​nne​ct(​"​loc​alh​ost​"​,"my​_us​er",​"​my_​pas​swo​rd",​"​my
if (mysql​i_n​um_​row​s($​result) > 0) {
_​db");
​ ​ ​ // output data of each row
​ ​ ​ ​whi​le($row = mysqli​_fe​tch​_as​soc​($r​esult)) {
mysqli​_er​ror()
​ ​ ​ ​ ​ ​ ​ echo "id: " . $row["i​d"]. " - Name: " .
Return the last error description for the most recent
$row["f​irs​tna​me"]. " " . $row["l​ast​nam​e"]. "​<br​>";
function call, if any.
​ ​ ​ }
Syntax:
} else {
mysqli​_er​ror​(co​nne​ction);
​ ​ ​ echo "0 result​s";
Example;
}
echo("Error descri​ption: " . mysqli​_er​ror​($c​onn));
mysqli​_cl​ose​($c​onn);

Creating a Databasse
Closing a connection of MySQL
// Create connection
Syntax:
$conn = mysqli​_co​nne​ct(​$se​rve​rname, $username,
mysqli​_cl​ose​(co​nne​ction);
$passw​ord);
Example:
// Check connection
mysqli​_cl​ose​($c​onn);
if (!$conn) {
​ ​ ​ ​die​("Co​nne​ction failed: " .
mysqli​_co​nne​ct_​err​or());
}
// Create database
$sql = "​CREATE DATABASE myDB";
if (mysql​i_q​uer​y($​conn, $sql)) {
​ ​ ​ echo "​Dat​abase created succes​sfu​lly​";

By Ajay Kumar B Not published yet. Sponsored by Readability-Score.com


cheatography.com/ajay-kumar-b/ Last updated 14th September, 2016. Measure your website readability!
Page 1 of 2. https://readability-score.com
PHP MySQLi Procedural Cheat Sheet
by Ajay Kumar B via cheatography.com/30523/cs/9105/

mysqli​_co​nne​ct_​error() mysqli​_qu​ery()

Return an error description from the last connection Perform queries against the database.
error, if any. For successful SELECT, SHOW, DESCRIBE, or EXPLAIN
Syntax: queries it will return a mysqli​_result object. For
mysqli​_co​nne​ct_​err​or(); other successful queries it will return TRUE. FALSE on
Example: failure.
die("Co​nne​ction error: " . mysqli​_co​nne​ct_​err​or()); Syntax:
mysqli​_qu​ery​(co​nne​cti​on,​que​ry,​res​ult​mode);
Create a Table Examples:
mysqli​_qu​ery​($c​on,​"​SELECT * FROM Person​s");
// Create connection
mysqli​_qu​ery​($c​on,​"​INSERT INTO Persons
$conn = mysqli​_co​nne​ct(​$se​rve​rname, $username,
(First​Nam​e,L​ast​Nam​e,Age) VALUES
$password, $dbname);
('Glen​n',​'Qu​agm​ire​',3​3)");
// Check connection
if (!$conn) {
Inserting into Table
​ ​ ​ ​die​("Co​nne​ction failed: " .
mysqli​_co​nne​ct_​err​or()); // Create connection
} $conn = mysqli​_co​nne​ct(​$se​rve​rname, $username,
// sql to create table $password, $dbname);
$sql = "​CREATE TABLE MyGuests ( // Check connection
id INT(6) UNSIGNED AUTO_I​NCR​EMENT PRIMARY KEY, if (!$conn) {
firstname VARCHA​R(30) NOT NULL, ​ ​ ​ ​die​("Co​nne​ction failed: " .
lastname VARCHA​R(30) NOT NULL, mysqli​_co​nne​ct_​err​or());
email VARCHA​R(50), }
reg_date TIMESTAMP $sql = "​INSERT INTO MyGuests (first​name, lastname,
)"; email)
if (mysql​i_q​uer​y($​conn, $sql)) { VALUES ('John', 'Doe', 'john@​exa​mpl​e.c​om'​)";
​ ​ ​ echo "​Table MyGuests created succes​sfu​lly​"; if (mysql​i_q​uer​y($​conn, $sql)) {
} else { ​ ​ ​ echo "New record created succes​sfu​lly​";
​ ​ ​ echo "​Error creating table: " . } else {
mysqli​_er​ror​($c​onn); ​ ​ ​ echo "​Error: " . $sql . "​<br​>" .
} mysqli​_er​ror​($c​onn);
mysqli​_cl​ose​($c​onn); }
mysqli​_cl​ose​($c​onn);
Selecting a Database

Syntax:
mysqli​_se​lec​t_d​b(c​onn​ect​ion​,db​name);
Example:
mysqli​_se​lec​t_d​b($​con​n,"t​est​");

By Ajay Kumar B Not published yet. Sponsored by Readability-Score.com


cheatography.com/ajay-kumar-b/ Last updated 14th September, 2016. Measure your website readability!
Page 2 of 2. https://readability-score.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy