How to Display MySQL Data With PHP


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.

  • 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">

    <select name="invoutput" class="invoutput">



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


    / Various output methods can now be allocated to an HTML element in this loop. /

  • Output 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:


    echo "<p>The invoice number is $invoutput";

    Pull-down select element in HTML form:

    $options.="<OPTION VALUE=\"$invoutput\">" . $Invoutput;

    Checkbox from a PHP array:

    echo "<input type=\"checkbox\" name=\"inv[]\" value=\"$invoutput\" />$invoutput";

    Data in a table:

    echo "<TR>";

    echo "<TD>$invoutput</TD>";

    Via a JavaScript dialogue box:

    echo "<script>alert('Value is $invoutput')</script>";

  • Display MySQL data with echo and printf. The following examples demonstrate how echo and printf are used to display information.


    echo "Invoice number is $invoutput\n";


    printf allows more options in terms of formatting the numbers, but this example shows a simple case:

    printf("Invoice number is %d\n",$invoutput);


Promoted By Zergnet


You May Also Like

Related Searches

Submit Your Work!