Practical Web Development by Unknown

Practical Web Development by Unknown

Author:Unknown
Language: eng
Format: mobi, epub
Publisher: Packt Publishing


Fetching the result

Now, if we typed in everything correctly—at first you may have mistakenly omitted one of the semicolons on either side of the closing single quote—MySQL will have returned the title and price of every single book in our table. It has done so by returning an object that we named $result, which we can apply a few methods to.

The simplest one will give us the number of rows that were found, the most powerful one being fetch_assoc(), which will create for us an associative array that we can instantly use in a while loop. So, here is how we can generate an HTML table with a list of all book titles and prices with just a few lines of code:

<?php $numbooks = $result->num_rows; $htmlstring = '<table><thead><tr><th>Book title</th><th>Price</th></tr></thead><tbody>'; while ($row = $result->fetch_assoc()) { $htmlstring .= '<tr><td>'.$row['title'].'</td><td>'.$row['price'].'</td></tr>'; } $htmlstring .= '</tbody></table>'; echo $htmlstring; ?>



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.