You are on page 1of 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