IIS Web Server
PHP Tutorials
Connect, Execute a Query, Print Resulting Rows and Disconnect from a MySQL Database.
Example 1
<html>
<body>
<?php
$db = mysql_connect("localhost", "root");//localhost and root are default values for IIS
mysql_select_db("mydb",$db);// mydb or the database you created
$result = mysql_query("SELECT * FROM employees",$db);
echo "<table border=1>\n";
echo "<tr><td>Name</td><td>Position</tr>\n";
while ($myrow = mysql_fetch_row($result)) { // fetches a row from the table in an array
printf("<tr><td>%s %s</td><td>%s</td></tr>\n", $myrow[1], $myrow[2], $myrow[3]);
// Prints one row at a time
// Each %s corresponds to each $myrow[number] respectively in printf()
}
echo "</table>\n";
?>
</body>
</html>
Example 2
<?php
/*Connecting, selecting database - the host can be localhost for wwwroot on IIS or determined by your ISP or yourself*/
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password');
mysql_select_db('my_database');
// Performing SQL query
$query = 'SELECT * FROM my_table'; //Select all the rows from the my_table
$result = mysql_query($query);
// Printing results in HTML
echo "<table>\n";// echo simply 'prints text to the browser'
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
// similar to mysql_fetch_row above. See below for explaination of MYSQL_ASSOC
echo "\t<tr>\n";
foreach ($line as $col_value) {// retreives as $col_value each entry in array $line
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>
mysql_fetch_array( result, result_type)
mysql_fetch_array -- Fetch a result row as an associative array, a numeric array, or both.
Parameters
- result
-
The result resource that is being evaluated. This result comes from a call to mysql_query().
- result_type
-
The type of array that is to be fetched. It's a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and the default value of MYSQL_BOTH.
Return Values
Returns an array that corresponds to the fetched row, or FALSE if there are no more rows. The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).
Example 3. mysql_fetch_array() with MYSQL_ASSOC
|
<?php |
Example 4. mysql_fetch_array() with MYSQL_BOTH
|
<?php |
