Displaying multiple rows from a table using looping with mysqli_fetch_array()
Using and Creating Functions
Lecture
MySQL Data Types
Numeric Data Types
INT
A normal-sized integer that can be signed or unsigned. If signed, the allowable range is from –2147483648 to 2147483647. If unsigned, the allowable range is from 0 to 4294967295. You can specify a width of up to 11 digits.
FLOAT(M,D)
A floating-point number that cannot be unsigned. You can define the display length (M) and the number of decimals (D). This is not required and will default to 10,2, where 2 is the number of decimals and 10 is the total number of digits (including decimals). Decimal precision can go to 24 places for a FLOAT.
DOUBLE(M,D)
A double precision floating-point number that cannot be unsigned. You can define the display length (M) and the number of decimals (D). This is not required and will default to 16,4, where 4 is the number of decimals. Decimal precision can go to 53 places for a DOUBLE. REAL is a synonym for DOUBLE.
DECIMAL(M,D)
An unpacked floating-point number that cannot be unsigned. In unpacked decimals, each decimal corresponds to one byte. Defining the display length (M) and the number of decimals (D) is required. NUMERIC is a synonym for DECIMAL.
A fixed-length string between 1 and 255 characters in length (for example CHAR(5)), right-padded with spaces to the specified length when stored. Defining a length is not required, but the default is 1.
VARCHAR(M)
A variable-length string between 1 and 255 characters in length; for example VARCHAR(25). You must define a length when creating a VARCHAR field.
Date and Time Types
DATE
A date in YYYY-MM-DD format, between 1000-01-01 and 9999-12-31. For example, December 30th, 1973 would be stored as 1973-12-30.
DATETIME
A date and time combination in YYYY-MM-DD HH:MM:SS format, between 1000-01-01 00:00:00 and 9999-12-31 23:59:59. For example, 3:30 in the afternoon on December 30th, 1973 would be stored as 1973-12-30 15:30:00.
TIMESTAMP
A timestamp between midnight, January 1, 1970 and 03:14:07 UTC on January 19, 2038. This looks like the previous DATETIME format, only without the hyphens between numbers; 3:30 in the afternoon on December 30th, 1973 would be stored as 19731230153000 ( YYYYMMDDHHMMSS ).
TIME
Stores the time in HH:MM:SS format.
YEAR(M)
Stores a year in 2-digit or 4-digit format. If the length is specified as 2 (for example YEAR(2)), YEAR can be 1970 to 2069 (70 to 69). If the length is specified as 4, YEAR can be 1901 to 2155. The default length is 4.
Each parameter can be either a string containing the name of the variable, or an array of variable names
The array can contain other arrays of variable names inside it
12345678
$city="San Francisco";$state="CA";$event="SIGGRAPH";$location_vars=array("city","state");$result=compact("event","nothing_here",$location_vars);print_r($result);//Array ( [event] => SIGGRAPH [city] => San Francisco [state] => CA )
Displaying mulitple rows from a table using looping with mysqli_fetch_array()
mysqli_fetch_array() allows you to process each row from a query from beginning to end. If there is a next row, the row is returned, otherwise NULL is returned.
<html> <head> <title>Fetching Multiple Rows</title> </head> <body><?php// Get all the products from the Product database// (just the name, price, and number in stock)$dbc=mysqli_connect('localhost','student','student','northwind')ortrigger_error("Error connecting to MySQL server: ".mysqli_error($dbc),E_USER_ERROR);$query="SELECT ProductName, UnitPrice, UnitsInStock FROM Products";$result=mysqli_query($dbc,$query)ortrigger_error("Query Error description: ".mysqli_error($dbc),E_USER_WARNING);mysqli_close($dbc);?> <table border='1px solid;'> <tr><th>Product Name </th><th>Unit Price </th><th>Units in Stock</th></tr><?php// Display every product row in a formatted tablewhile($row=mysqli_fetch_array($result)){echo'<tr><td>'.$row['ProductName'].'</td><td>'.$row['UnitPrice'].'</td><td>'.$row['UnitsInStock'].'</td></tr>';}?> </table> </body></html>
Using and Creating Functions
Functions allow you to organize code into modules that can be easily reused
Functions can have zero or more parameters, and may return a value
$first_num=5;$second_num=7;$sum_of_two_numbers=sum($first_num,$second_num);echo"The result of adding $first_num and $second_num is $sum_of_two_numbers<br/>";