Professional Documents
Culture Documents
hayperaktib Lecture # 9
Syntax
mysqlii_connect(servername,username,password);
Parameter Description
Servername Optional. Specifies the server to connect to.
Username Optional. Specifies the username to log in with. Default value is the name of
the user that owns the server process
Password Optional. Specifies the password to log in with. Default is ""
Mysqli Functions
Database Function What it does
Example:
Example:
$query = “SELECT ID, lastname, firstname from users WHERE status=1”;
$result = mysqli_query($query);
while($name_row = mysqli_fetch_row($result)){
echo “$name_row[0] $name_row[1] $name_row[2] <br>”;
}
field offsets = integers in brackets in enumerated array.
1
DCIT111 – Advanced Programming
hayperaktib Lecture # 9
Example:
Example:
$query = “SELECT ID, lastname, firstname from users WHERE status=1”;
$result = mysqli_query($query);
while($row = mysqli_fetch_array($result)){
echo “$row[‘ID’], $row[‘lastname’], $ row[‘firstname’] <br>”;
}
Remember that mysqli_fetch_array can also be used exactly the same way as
mysqli_fetch_row – with numerical identifiers rather than field names. By using this
function, you leave yourself the option. If you want to specify offset or field name rather
than making both available, you can do it like this:
Examples:
$query = “SELECT count(*) FROM personal_info”;
$db_result = mysqli_query($query);
$datapoint = mysqli_result($db_result, 0, 0);
The mysqli_result function takes three arguments: result identifier, row identifier, and
field. Field can take the value of the field offset as above, or its name as in an associative
array(“surname”), or its Mysqli field-dot-table name(“personal_info.surname”)
2
DCIT111 – Advanced Programming
hayperaktib Lecture # 9
Query Functions Affecting Columns
3
DCIT111 – Advanced Programming
hayperaktib Lecture # 9
C O D E S:
function:
function print_list($query,$fldvalue) {
$result = mysqli_query($query);
while($row = mysqli_fetch_array($result)){
echo "<option value=\"$row[$fldvalue]\"";
echo ">$row[$fldvalue]</option>";
}
}
<?php
$q =“select * from table1";
$rs = mysqli_query($q);
$ctr = 0;
while($row = mysqli_fetch_array($rs)){
if($ctr % 2 == 0)
$bg = "#99FF99";
else
$bg = "";
?>
<?php
$rs = mysqli_query(“select * from table1”);
for($c=0;$c<mysqli_num_fields($rs);$c++) {
print “<th>” . mysqli_field_name($rs,$c) . “</th>”;
}
4
DCIT111 – Advanced Programming
hayperaktib Lecture # 9
while($row = mysqli_fetch_row($rs)){
foreach($rs as $value){
echo “<tr>“;
echo “<td>$value </td>”;
echo “<tr>“;
}
}
?>