smartcard Posted December 9, 2015 Share Posted December 9, 2015 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; Quote Link to comment Share on other sites More sharing options...
Barand Posted December 9, 2015 Share Posted December 9, 2015 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. Quote Link to comment Share on other sites More sharing options...
smartcard Posted December 11, 2015 Author Share Posted December 11, 2015 Many thanks the above is working. I want to know how can I output / display only few columns of the database. My database has about 20 columns but I need to display only 3-4 columns in the table. Quote Link to comment Share on other sites More sharing options...
Barand Posted December 11, 2015 Share Posted December 11, 2015 Specify just the columns you want in the SELECT (which you should do anyway, instead of using "SELECT * ") Quote Link to comment Share on other sites More sharing options...
smartcard Posted December 11, 2015 Author Share Posted December 11, 2015 Thanks it works fine Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.