Jump to content

Single PHP script file to extract and display MySQL data in a report format


smartcard

Recommended Posts

Is there is simple way to use a single PHP file to extract and display MySQL data in a report format?  

 

I want a PHP file in it I will add the database connection credentials (db name, user name, password etc) and I want to run the following query and result the data in a report format.

 



 SELECT * FROM `users` WHERE `allowed_user`=1;


 

 

Link to comment
Share on other sites

This simple library function of mine will do it. You can use CSS styling to make it look prettier.

$db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE);
$sql = "SELECT * FROM users WHERE allowed_user=1";

// call the function
echo query2HTML($db, $sql);

function query2HTML($db, $sql)
{
    $output = "<table border='1' cellpadding='2' style='border-collapse:collapse'>\n";
      // Query the database
    $result = $db->query($sql);
      // check for errors
    if (!$result) return ("$db->error <pre>$sql</pre>");
    if ($result->num_rows == 0) return "No matching records";
      // get the first row and display headings
    $row = $result->fetch_assoc();
    $output .= "<tr><th>" . join('</th><th>', array_keys($row)) . "</th></tr>\n";
    
      // display the data
    do {
       $output .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n"; 
    } while ($row = $result->fetch_assoc());
    $output .= "</table>\n";
    return $output;
}

I suggest you examine each line of code in conjunction with the PHP manual until you understand what each line is doing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.