PHP can pull information from a MySQL database by linking to the database, setting a query and performing a PHP operation. The query can be anything that updates, amends or selects data from the database. Output of the information can then be placed on a Web page, or output as a PHP dialogue or JavaScript function. The most common method is to output data to a Web page using HTML, but occasionally other display outputs are used.
Instructions
- 1
Perform a query by connecting to the database, calling the query and then checking the result. The following is an example script utilizing an HTML form:
form name="invoice_form" method="POST" action="Invoices.php" class="formcss">
include("db.dat");
$Connecthandle = mysqli_connect($hostserver,$userid,$pass,$databname)
or die ("couldn't connect to server");
$querytext = "SELECT invoutput FROM invoicelist ORDER BY invoutput ASC";
$result = mysqli_querytext($connecthandle,$querytext)
or die ("Couldn't execute querytext.");
while ($row=mysqli_fetch_array($result))
$Invoutput=$row["invoutput"];
/* Various output methods can now be allocated to an HTML element in this loop. */
2Output the query using HTML to a Web page. This is the standard method of output, and data can be placed in any element. The following HTML display types illustrate different elements of a Web page, in the loop outputting the data row by row:
Paragraph:
echo "
The invoice number is $invoutput";
Pull-down select element in HTML form:
$options.="
Checkbox from a PHP array:
echo "$invoutput";
Data in a table:
echo "
echo "
Via a JavaScript dialogue box:
echo "";
3Display MySQL data with echo and printf. The following examples demonstrate how echo and printf are used to display information.
echo:
echo "Invoice number is $invoutput\n";
printf:
printf allows more options in terms of formatting the numbers, but this example shows a simple case:
printf("Invoice number is %d\n",$invoutput);
Powered by Blogger
Blogger Theme by IdBlogDesign
0 comments:
Post a Comment