I want to export the data in the gotten from det SQL to a spreadsheet, using PHP.
$sql_export = "SELECT id_employee, firstname, email FROM employees WHERE id_employee = 1";
$export_result = $db->query($sql_export);
This code working for me.Try this code and make some changes as you needed.
**excl_1.php**
<table class="table table-bordered">
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>Postal Code</th>
<th>Country</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
echo '
<tr>
<td>'.$row["CustomerName"].'</td>
<td>'.$row["Address"].'</td>
<td>'.$row["City"].'</td>
<td>'.$row["PostalCode"].'</td>
<td>'.$row["Country"].'</td>
</tr>
';
}
?>
</table>
<br />
<form method="post" action="export.php">
<input type="submit" name="export" class="btn btn-success" value="Export" />
</form
**export.php**
<?php
$connect = mysqli_connect("localhost", "root", "", "testing");
$output = '';
if(isset($_POST["export"]))
{
$query = "SELECT * FROM tbl_customer";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '
<table class="table" bordered="1">
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>Postal Code</th>
<th>Country</th>
</tr>';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["CustomerName"].'</td>
<td>'.$row["Address"].'</td>
<td>'.$row["City"].'</td>
<td>'.$row["PostalCode"].'</td>
<td>'.$row["Country"].'</td>
</tr>';
}
$output .= '</table>';
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=download.xls');
echo $output;
}
}
?>
Test Result
There are a couple of libraries for that, first and foremost https://packagist.org/packages/phpoffice/phpspreadsheet
Related
I want to get data from a MySQL Database;
EDIT:
I want to be able to modify a query in order to obtain different rows corresponding to a specific date, using a datapicker.
I need to generate a report (an Excel file) from a query, make it user-friendly, using HTML and PHP but I don't know how to do that.
EDIT: Sorry, I am going to edit this question:
<?php
$connect = mysqli_connect("localhost", "root", "", "testing");
$sql = "SELECT * FROM tbl_customer";
$result = mysqli_query($connect, $sql);
?>
<html>
<head>
<title>Attempt to get data</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<br />
<br />
<br />
<div class="table-responsive">
<h2 align="center">Export MySQL data to Excel in PHP</h2><br />
<table class="table table-bordered">
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>Postal Code</th>
<th>Country</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
echo '
<tr>
<td>'.$row["CustomerName"].'</td>
<td>'.$row["Address"].'</td>
<td>'.$row["City"].'</td>
<td>'.$row["PostalCode"].'</td>
<td>'.$row["Country"].'</td>
</tr>
';
}
?>
</table>
<br />
<form method="post" action="export.php">
<input type="submit" name="export" class="btn btn-success" value="Export" />
</form>
</div>
</div>
</body>
</html>
And
<?php
//export.php
$connect = mysqli_connect("localhost", "root", "", "testing");
$output = '';
if(isset($_POST["export"]))
{
$query = "SELECT * FROM tbl_customer";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '
<table class="table" bordered="1">
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>Postal Code</th>
<th>Country</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["CustomerName"].'</td>
<td>'.$row["Address"].'</td>
<td>'.$row["City"].'</td>
<td>'.$row["PostalCode"].'</td>
<td>'.$row["Country"].'</td>
</tr>
';
}
$output .= '</table>';
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=download.xls');
echo $output;
}
}
?>
Take a look at this:
$sql = "SELECT * FROM tbl_customer";
there is a column called CREATED_AT in my table and I want the user to be able to dynamically change the value of that in the page.
I mean:
SELECT * FROM tbl_customer
where CREATED_AT >= (here, the user should be able to adjust this field in the view of the page, otherwise he Will get several rows from different dates). I don't know how to do that :(
Also, I would like to display a load bar when clicking on export to Excel.
I am pulling information from my table. It is setup as a varchar. I can't change it because it 0's everything out when I do. I need to be able to divide the result by 12. It is the MY-TotalPrem that needs to be divided.
I tried
<td>'.$row["MS-TotalPrem" / 12].'
</td>, <td>'.$row["MS-TotalPrem"] / 12.'</td>,
<td>'.$row[int("MS-TotalPrem") / 12].
'</td>
and none work.
function fetch_customer_data($connect)
{
$query = "SELECT * FROM mytable";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$output = '
<div class="table-responsive">
<table class="table table-striped table-bordered">
<tr>
<th>Name</th>
<th>Address</th>
<th>Address</th>
<th>Policy Number</th>
<th>Total Monthly</th>
<th>Expiration Date</th>
</tr>
';
foreach($result as $row)
{
$output .= '
<tr>
<td>'.$row["AC-AcctName"].'</td>
<td>'.$row["PM-PropertyAddr1"].'</td>
<td>'.$row["PO-Addr1"].'</td>
<td>'.$row["PO-PrefixPolNum"].'</td>
<td>'.$row["MS-TotalPrem"].'</td>
<td>'.$row["PO-ExpDate"].'</td>
</tr>
';
}
$output .= '
</table>
</div>
';
return $output;
}
I just want the output to be a division of 12, or any other number I put in. Thank you.
function fetch_customer_data($connect)
{
$query = "SELECT * FROM mytable";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$output = '
<div class="table-responsive">
<table class="table table-striped table-bordered">
<tr>
<th>Name</th>
<th>Address</th>
<th>Address</th>
<th>Policy Number</th>
<th>Total Monthly</th>
<th>Expiration Date</th>
</tr>
';
foreach($result as $row)
$output .= '
<tr>
<td>'.$row["AC-AcctName"].'</td>
<td>'.$row["PM-PropertyAddr1"].'</td>
<td>'.$row["PO-Addr1"].'</td>
<td>'.$row["PO-PrefixPolNum"].'</td>
<td>'.($row["MS-TotalPrem"] / 12).'</td>
<td>'.$row["PO-ExpDate"].'</td>
</tr>
';
$output .= '
</table>
</div>
';
return $output;
}
I design a table with html (bootstrap style) now i want to get data from my database table and fetch it in table using php, here is my html code:
Thanks for you help. :)
<div class="ibox-content">
<table class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Category</th>
<th>Register Date</th>
<th>Occurrence Date</th>
<th>Province</th>
<th>User</th>
</tr>
</thead>
<tbody>
<tr class="gradeX">
<td>test</td>
<td>test</td>
<td>test</td>
<td class="center">test</td>
<td class="center">test</td>
</tr>
</tfoot>
</table>
</div>
</div>
PHP code i tried but i didnt get any result it gives me errors:
<?php
$username = "root";
$password = "=";
$host = "localhost";
$connector = mysql_connect($host,$username,$password)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("user", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM table_one ");
?>
<div class="ibox-content">
<table class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Category</th>
<th>Register Date</th>
<th>Occurrence Date</th>
</tr>
</thead>
<tbody>
<tr class="gradeX">
<td>test</td>
<td>test</td>
<td>test</td>
<td class="center">test</td>
<td class="center">test</td>
</tr>
</tfoot>
</table>
</div>
</div>
<?php
while( $row = mysql_fetch_assoc( $result ) ){
echo
"<tr>
echo "<td>" . $row['Category'] . "</td>";
<td>{$row\['regdate'\]}</td>
<td>{$row\['occdate'\]}</td>
</tr>\n";
}
?>
Since mysql_* is depreciated I will answer this using mysqli_*.
You have already closed the table in your example before looping through the fetched data so I am unsure where you want these rows to populate however I made a guess.
Another error with your attempt is you have echo " ... echo "";
<?php
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
$query = 'SELECT * FROM table_one';
$data = mysqli_query($mysqli, $query);
?>
<div class="ibox-content">
<table class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Category</th>
<th>Register Date</th>
<th>Occurrence Date</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($data))
{
echo " <tr>
<td>" . $row['Category'] . "</td>
<td>" . $row['regdate'] . "</td>
<td>" . $row['occdate'] . "</td>
</tr>";
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
I have indented the echo inside the while loop to keep it inline with the other html.
Isset($_POST['submit) is not working but when I add the not operation(!) it worked. i don't where to find the error.
here is my code
$get = isset($_GET['id']) ? $_GET['id'] : "";
$query = "SELECT * FROM `crew_info` WHERE `id` = '$get'";
$query_run = mysqli_query($conn,$query);
if(!isset($_POST['submit'])) {
$query_update = "UPDATE `crew_info` SET `crew_status` = 'NEW' WHERE `id` = '$get'";
if ($query_update_run = mysqli_query($conn,$query_update)) {
echo 'Crew Accepted';
}
}
here is the submit form
<form action="review.php" method="POST">
<table border="2" align="center" width="600" cellspacing="3" cellpadding="4">
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>Date Added</th>
<th>Status</th>
</tr>
<tr>';
while($record = mysqli_fetch_assoc($query_run)) {
echo "<tr>";
echo "<th>".$record['first_name']."</th>";
echo "<th>".$record['middle_name']."</th>";
echo "<th>".$record['last_name']."</th>";
echo "<th>".$record['date_added']."</th>";
echo "<th>".$record['crew_status']."</th>";
echo '</tr>';
}
echo '</table>';
echo '<br><center><input type="submit" name="submit"></center>
</form>
You are not submitting any values.
<input type="submit" name="submit">
Try something like this:
<input type="submit" name="submit" value="ok">
This piece of PHP checks if the form element with the name "submit" has any value:
"if isset($_POST['submit'])"
But since you haven't set any value for "submit", it won't validate. However, it will validate when you set the exclamation mark in front of it:
!if isset($_POST['submit'])"
This is because then you are saying, "if 'submit' has no value set, do the following".
Hope that makes sense. :)
Update: You also have several other errors that you need to fix to get the whole thing working. This should fix most of it. Compare it to your current code:
<?php $get = isset($_GET['id']) ? $_GET['id'] : "";
$query = "SELECT * FROM crew_info WHERE id = '$get'";
$query_run = mysqli_query($conn,$query);
if(isset($_POST['submit'])) {
$query_update = "UPDATE crew_info SET crew_status = 'NEW' WHERE id = '$get'";
if ($query_update_run = mysqli_query($conn,$query_update)) {
echo 'Crew Accepted';
}
} ?>
<form action="review.php" method="POST">
<table border="2" align="center" width="600" cellspacing="3" cellpadding="4">
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>Date Added</th>
<th>Status</th>
</tr>
<tr>
<?php
while($record = mysqli_fetch_assoc($query_run)) {
echo "<tr>";
echo "<th>".$record['first_name']."</th>";
echo "<th>".$record['middle_name']."</th>";
echo "<th>".$record['last_name']."</th>";
echo "<th>".$record['date_added']."</th>";
echo "<th>".$record['crew_status']."</th>";
echo '</tr>';
}
echo '</table>';
echo '<br><center><input type="submit" name="submit" value="ok"></center>';
?>
</form>
I am trying to display my table from my MySQL database. It doesn't seem to work, I guess it's most likely something simple. If possible I would like to have the table look like just a normal table with no frills; just a border and to be able to fit in a normal sized page. This is what I have so far:
<html>
<head>
<title>Profile Database </title>
</head>
<body>
<?
$connection = "connect.php";
require $connection;
mysql_select_db("bank");
$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ))
{
echo <tr>
<td>{$row\['id'\]}</td>
<td>{$row\['fname'\]}</td>
<td>{$row\['lname'\]}</td>
<td>{$row\['email'\]}</td>
<td>{$row\['dob'\]}</td>
<td>{$row\['age'\]}</td>
<td>{$row\['city'\]}</td>
<td>{$row\['state'\]}</td>
<td>{$row\['zip'\]}</td>
<td>{$row\['offence'\]}</td>
<td>{$row\['notes'\]}</td>
</tr>\n;
}
?>
</tbody>
</table>
<?php
mysql_close($connection);
?>
</body>
</html>
Give this a go:
<table>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
<?php
require "connect.php";
mysql_select_db("bank");
$result = mysql_query("SELECT * FROM profiles");
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo '<tr>';
foreach($row as $column) {
echo '<td>', $column, '</td>';
}
echo '</tr>';
}
?>
</table>
WHILE($row = mysql_fetch_array($result))
should be either
WHILE($row = mysql_fetch_array($result, MYSQL_ASSOC))
OR
WHILE($row = mysql_fetch_assoc($result))
Also the mysql_* functions have been deprecated and relying on them is HIGHLY discouraged.
Kindly check the below discussions :-
Deprecated MySql Functions
How to successfully rewrite old mysql-php code with deprecated mysql_* functions?
http://php.net/manual/en/migration55.deprecated.php
Use either MySQLi or PDO.
<center><table border="1">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
<?
$connection = "connect.php";
require $connection;
mysql_select_db("bank");
$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
WHILE($row = mysql_fetch_array($result))
{
$id = $row ['id'];
$firstname = $row ['fname'];
$lastname = $row ['lname'];
$email = $row ['email'];
$dob = $row ['dob'];
$age = $row ['age'];
$city = $row ['city'];
$state = $row ['state'];
$zip = $row ['zip'];
$offence = $row ['offence'];
$nots = $row ['notes'];
echo '<tr>'; // change here
echo '<td>'.$id.'</td>';
echo '<td>'.$firstname.'</td>';
echo '<td>'.$lastname.'</td>';
echo '<td>'.$email.'</td>';
echo '<td>'.$dob.'</td>';
echo '<td>'.$age.'</td>';
echo '<td>'.$city.'</td>';
echo '<td>'.$state.'</td>';
echo '<td>'.$zip.'</td>';
echo '<td>'.$offence.'</td>';
echo '<td>'.$notes.'</td>';
echo '</tr>'; // change here
}
mysql_close();
?>
</table></center>
Try this code:
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";
$dbconnect = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$select=mysql_select_db("bank",$dbconnect) or die("Could not select bank");
$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
?>
<center><table border="1">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th></tr>
<?php
while($row = mysql_fetch_array($result))
{
$id = $row ['id'];
$firstname = $row ['fname'];
$lastname = $row ['lname'];
$email = $row ['email'];
$dob = $row ['dob'];
$age = $row ['age'];
$city = $row ['city'];
$state = $row ['state'];
$zip = $row ['zip'];
$offence = $row ['offence'];
$nots = $row ['notes'];
echo '<tr>';
echo '<td>'.$id.'</td>';
echo '<td>'.$firstname.'</td>';
echo '<td>'.$lastname.'</td>';
echo '<td>'.$email.'</td>';
echo '<td>'.$dob.'</td>';
echo '<td>'.$age.'</td>';
echo '<td>'.$city.'</td>';
echo '<td>'.$state.'</td>';
echo '<td>'.$zip.'</td>';
echo '<td>'.$offence.'</td>';
echo '<td>'.$notes.'</td>';
echo '</tr>';
}
?>
</table></center>
<?php
mysql_close();
?>
Corrected Code:
<html>
<head>
<title>Profile Database </title>
</head>
<body>
<?php
$connection = "connect.php";
require $connection;
mysql_select_db("bank");
$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ))
{
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['fname'];?></td>
<td><?php echo $row['lname'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['dob'];?></td>
<td><?php echo $row['age'];?></td>
<td><?php echo $row['city'];?></td>
<td><?php echo $row['state'];?></td>
<td><?php echo $row['zip'];?></td>
<td><?php echo $row['offence'];?></td>
<td><?php echo $row['notes'];?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
mysql_close($connection);
?>
</body>
</html>