Web Programming - Week 7 Flashcards
PHP connect to MySQL
$mysqli = new mysqli(“localhost”,”my_user”,”my_password”,”my_db”);
How to check if PHP’s connection to MySQL has failed
if (!$conn) {
die(“Connection failed: “ . mysqli_connect_error());
}
Create a select query
SELECT column_name(s) FROM table_name
OR
SELECT * FROM table_name
How to execute a query
$result = mysql_query($conn, $sql);
How to get data from a query
while ($row = mysqli_fetch_array($result)){
}
Convert PHP to JSON
$alldata = array()
while ($row = mysqli_fetch_array($result, MYSQLI,ASSOC)){
$alldata[] =$row;
}
How to close an opened database
mysqli_close($conn);
Extract data from database
SELECT, FROM, WHERE
What is WHERE used for
To filter records
WHERE operators
AND/OR/NOT
What is the LIKE operator used for
To search for a specified pattern in a where clause
What is the COUNT function used for
To count the number of rows that matches a specific criterion
Useful maths functions
AVG, Sum, MAX, MIN
What is ORDER BY used for
To sort the result set in ascending or descending order
What is JOIN used for
To combine rows from two or more tables