You are on page 1of 5

DCIT111 – Advanced Programming

hayperaktib Lecture # 9

Connecting to a MySQL Database


Before you can access and work with data in a database, you must create a connection to the
database.

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

mysqlii_connect() opens a connection to a Mysqli server.


mysqlii_pconnect() opens a persistent connection.
mysqlii_select_db() selects the default database.
mysqlii_query() send a query or command to a Mysqli connection.
mysqlii_query_db() send a query or command to a Mysqli connection and get the
database name.
mysqli_change_user() changes the identity of the user logged on.
mysqli_list_dbs lists databases for this Mysqli server.
mysqli_list_tables lists tables in the database

Query Functions Affecting Rows

mysqli_fetch_assoc() returns one result row, as an associative array.

Example:

$query = “SELECT ID, lastname, firstname from users WHERE status=1”;


$result = mysqlii_query($query);
while($name_row = mysqlii_fetch_assoc($result)){
echo “$name_row[“ID”] $name_row[“lastname”] $name_row[“firstname”]
<br>”;
}

mysqli_fetch_row() returns one result row, as an enumerated array.

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

mysqli_fetch_object() returns a result row, as an object.

Example:

$query = “SELECT ID, lastname, firstname from users WHERE status=1”;


$result = mysqli_query($query);
while($row = mysqli_fetch_object($result)){
echo “$row->ID $row->lastname $ row->firstname <br>”;
}

mysqli_fetch_array() returns a result row, as an associative or an enumerated array.

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:

$offset_row = mysqli_fetch_array($result, MYSQLI_NUM);


or
$associative_row = mysqli_fetch_array($result, MYSQLI_ASSOC);

mysqli_result() returns only one piece of data from Mysqli.

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”)

mysqli_affected_rows() returns number of rows affected by query.


the query must be INSERT, UPDATE, DELETE, CREATE TABLE,
or DROP TABLE

mysqli_num_rows() returns number of rows selected.


the query must be SELECT statement.

2
DCIT111 – Advanced Programming
hayperaktib Lecture # 9
Query Functions Affecting Columns

mysqli_fetch_field() gets column information from a result and returns as an object

mysqli_field_name() gets the name of the specified field in a result.

mysqli_list_fields() sets result pointer to a specified field offset.

mysqli_num_fields() gets number of fields in a result.

mysqli_field_seek() sets result pointer to a specified field offset.

mysqli_field_type() gets the type of the specified field in a result.

mysqli_field_len() returns the length of the specified field.

mysqli_field_table() gets name of the table the specified field is in.

mysqli_tablename() gets table name of field.

Functions for Error Handling

mysqli_errno() returns the numerical value of the error message from


previous Mysqli operation.

mysqli_error() returns the text of the error message from previous


Mysqli operation.

3
DCIT111 – Advanced Programming
hayperaktib Lecture # 9
C O D E S:

POPULATE SELECT TAG

<select name="severity" id="severity">


<?php
$query = "SELECT * FROM table1";
print_list($query,0)
?>
</select>

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>";
}
}

ALTERNATE COLOR OF THE FETCH ROW

<?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 = "";
?>

<tr bgcolor="<?php echo $bg ?>">


<td><?php echo $row[0] ?></td>
<td><?php echo $row[1] ?> </td>
<td><?php echo $row[2] ?> </td>
</tr>
<?php
$ctr++;
}
?>

mysqli_num_fields and mysqli_field_name

<?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>“;
}
}
?>

You might also like