retrieve data from mysql and display it in a table using php - php

I am trying to retrieve all data from my database and displaying it in a table. But i could not do that, am facing some problem.I am getting error as,
This page isn’t working.
localhost is currently unable to handle this request.
Here is my code,
<html>
<body>
<table style="width:100%">
<tr>
<th>Driverid</th>
<th>Truckid</th>
<th>Imagecount</th>
<th>Trainingstatus</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "IDdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT DriverID, TruckID, Imagecount, Trainingstatus FROM IDs";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];?>
<tr>
<td><?php echo $driverid; ?></td>
<td><?php echo $truckid; ?></td>
<td><?php echo $imagecount; ?></td>
<td><?php echo $trainingstatus; ?></td>
</tr>
</table>
<?php}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>

please replace your code
from
driverid = $row["Driverid"];
truckid = $row["Truckid"];
imagecount = $row["Imagecount"];
trainingstatus = $row["Trainingstatus"];
to
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];

please reader the section about how to define variables in php on the PHP Manual ,after that, check the variables defined in your code.

you have not started table tag, also not in loop, and main part is sdd space <?php} to <?php }
if ($result->num_rows > 0) {
echo '<table>';
// output data of each row
while($row = $result->fetch_assoc()) {
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];?>
<tr>
<td><?php echo $driverid; ?></td>
<td><?php echo $truckid; ?></td>
<td><?php echo $imagecount; ?></td>
<td><?php echo $trainingstatus; ?></td>
</tr>
<?php }
echo '</table>';
} else {
echo "0 results";
}

Related

MySQLi Displaying Results in a Table

I'm trying to display generated data from PHPMyAdmin into a table using MySQLi and I can't seem to figure it out.
<tr>
<th scope="col">Link</th>
<th scope="col">Category</th>
</tr>
<?php
//Connection Information
$connection = mysqli_connect('localhost','root',''); //establish connection to db
$selected = mysqli_select_db($connection, 'sample'); //select db
//SQLi Statements
$viewQuery = "select * from link JOIN categories";
$execute = mysqli_query($connection,$viewQuery);
if($execute)
{
while($row = mysqli_fetch_array($execute))
{
$link = $row['link'];
$category = $row['category'];
}
}
?>
<tr>
<td><?php echo $link; ?></td>
<td><?php echo $category; ?></td>
</tr>
</table>
Is there something I'm missing? I'm new to MySQLi
You are overwriting the variables without using it. It should be like this i guess
while($row = mysqli_fetch_array($execute)){
$link = $row['link'];
$category = $row['category'];
?>
<tr>
<td><?php echo $link; ?></td>
<td><?php echo $category; ?></td>
</tr>
<?php
}

Large HTML from SQL Server Using PHP

I've got an MSSQL server running with several large tables. I'm trying to place them into an HTML table so that I can display all of the data in a nice CSS modified webpage. I'm using PHP to ferry the information from SQL to my HTML script, but the only way I've found so far to generate such a table is by hardcoding all of the SQL column names into my PHP-SQL query.
As you can see below, this is the setup required for just one such table. Is there any more concise way to get the information from SQL into a formatted HTML table? I've looked around for a number of hours perhaps for some sort of PHP scripted loop that can iterate through all of the SQL columns, but I haven't found anything. I greatly appreciate any input!
<table id="capacitors">
<thead>
<tr>
<td>Part Number</td>
<td>Capacitance</td>
<td>Capacitance Tolerance</td>
<td>Case Package</td>
<td>Case Package SI</td>
<td>Dielectric Char.</td>
<td>Dielectric Mat.</td>
<td>Halogen Free Stat.</td>
<td>Insulation Resistance</td>
<td>Lead Free Stat.</td>
<td>Lifecycle Stat.</td>
<td>Mounting Style</td>
<td>Operating Temp.</td>
<td>Packaging</td>
<td>Pin Count</td>
<td>Reach SVHC Comp.</td>
<td>Rohs Stat.</td>
<td>Size Height</td>
<td>Size Length</td>
<td>Size Thickness</td>
<td>Size Width</td>
<td>Temp. Coefficient</td>
<td>Voltage Rating DC</td>
</tr>
</thead>
<tbody>
<?php
foreach ($db->query($sql) as $rows){
?>
<tr>
<td><?php echo $rows['part_number']?></td>
<td><?php echo $rows['capacitance']?></td>
<td><?php echo $rows['capacitance_tolerance']?></td>
<td><?php echo $rows['case_package']?></td>
<td><?php echo $rows['case_package_si']?></td>
<td><?php echo $rows['dielectric_characteristic']?></td>
<td><?php echo $rows['dielectric_material']?></td>
<td><?php echo $rows['halogen_free_status']?></td>
<td><?php echo $rows['insulation_resistance']?></td>
<td><?php echo $rows['lead_free_status']?></td>
<td><?php echo $rows['lifecycle_status']?></td>
<td><?php echo $rows['mounting_style']?></td>
<td><?php echo $rows['operating_temperature']?></td>
<td><?php echo $rows['packaging']?></td>
<td><?php echo $rows['pin_count']?></td>
<td><?php echo $rows['reach_svhc_compliance']?></td>
<td><?php echo $rows['rohs_status']?></td>
<td><?php echo $rows['size_height']?></td>
<td><?php echo $rows['size_length']?></td>
<td><?php echo $rows['size_thickness']?></td>
<td><?php echo $rows['size_width']?></td>
<td><?php echo $rows['temperature_coefficient']?></td>
<td><?php echo $rows['voltage_rating_dc']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
This is basic example using PHP Driver for SQL Server:
<?php
# Settings
$server = 'server\instance,port';
$database = 'database';
$user = 'user';
$password = 'password';
$tablename = 'tablename';
# Connection
$cinfo = array(
"Database" => $database,
"ReturnDatesAsStrings" => true,
"UID" => $user,
"PWD" => $password
);
$conn = sqlsrv_connect($server, $cinfo);
if($conn === false)
{
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
}
# SQL statement
$sql = "SELECT * FROM [".$tablename."]";
$stmt = sqlsrv_prepare($conn, $sql);
if($stmt === false) {
echo "Error (sqlsrv_prepare): ".print_r(sqlsrv_errors(), true);
exit;
}
# Columns names
echo '<table id="'.$tablename.'">';
echo "<thead>";
echo "<tr>";
$metadata = sqlsrv_field_metadata($stmt);
foreach($metadata as $field) {
echo "<td>".$field['Name']."</td>";
}
echo "</tr>";
echo "</thead>";
# Table rows
echo "<tbody>";
if (!sqlsrv_execute($stmt)) {
echo "Error (sqlsrv_execute): ".print_r(sqlsrv_errors(), true);
exit;
}
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo "<tr>";
foreach($row as $value) {
echo "<td>".$value."</td>";
};
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
# End
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
?>
Maybe use SHOW COLUMNS or DESCRIBE
$columns = "SHOW COLUMNS FROM <table>";
$output = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
echo $row['Field']."<br>";
}

how to add values for each colum for each day?

I have a Table which Input all the necessary data to do statistics. i now face the issue that i dont know how to add up the values for each colum but that it only adds the values up for each day. and then Displays it in a row or another Table.
this Code is what i use to call out all of the values and insert them into a table
<html>
<head>
<title>Your Home Page</title>
</head><body>
<table border="1">
<tr>
<th>Date</th>
<th>Participant ID</th>
<th>Gender</th>
<th>Hand</th>
<th>Udnder 18</th>
<th>Adult</th>
<th>R</th>
<th>C</th>
<th>L</th>
<th>Cash</th>
<th>Card</th>
<th>Ammount</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "***";
$mysqli = new mysqli($servername, $username, $password, $dbname);
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$sql = "SELECT * FROM table ORDER BY Date ASC";
$result = mysqli_query($mysqli, $sql) or die(mysqli_error());
$datum = '';
while($set = mysqli_fetch_assoc($result)) {
?>
<tr>
<td>
<?php
if ($datum == $set["Date"]) {
echo ' ';
} else {
echo $set["Date"];
}
?>
</td>
<td><?php echo $set["ParticipantID"]; ?></td>
<td><?php echo $set["Gender"]; ?></td>
<td><?php echo $set["Hand"]; ?></td>
<td><?php echo $set["Under18"]; ?></td>
<td><?php echo $set["Adult"]; ?></td>
<td><?php echo $set["r"]; ?></td>
<td><?php echo $set["c"]; ?></td>
<td><?php echo $set["l"]; ?></td>
<td><?php echo $set["Cash"]; ?></td>
<td><?php echo $set["Card"]; ?></td>
<td><?php echo $set["Ammount"]; ?></td>
</tr>
<?php $datum = $set["Date"]; ?>
<?php
}
?>
</table> </body></html>
So for 04-01-2018 for the colum R the total summed up value should be 2. etc.
Thanks for the help.
Kind Regards
Mark
The first way of solving this problem is to use the second MySQL query to get total values for each date. After that create a function printDateTableRow to print each row of the table. Using the mysqli_fetch_assoc($totalMysqliResult) parameter of this function, you can pass values of a row with total values for each new date.
Another way is to use the the WITH ROLLUP group modifier (you can try to write such query by yourself).
Here is the working code for the first way:
<html>
<head>
<title>Your Home Page</title>
</head>
<body>
<table border="1">
<tr>
<th>Date</th>
<th>Participant ID</th>
<th>Gender</th>
<th>Hand</th>
<th>Udnder 18</th>
<th>Adult</th>
<th>R</th>
<th>C</th>
<th>L</th>
<th>Cash</th>
<th>Card</th>
<th>Ammount</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "***";
$mysqli = new mysqli($servername, $username, $password, $dbname);
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$sql = "SELECT * FROM `table` ORDER BY Date ASC";
$result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
$totalMysqliResult = mysqli_query($mysqli, '
SELECT
Date, "" AS ParticipantID, "" AS Gender, "" AS Hand,
SUM(Under18) AS date_total_Under18,
SUM(Adult) AS date_total_Adult, SUM(r) AS date_total_r,
SUM(c) AS date_total_с, SUM(l) AS date_total_l,
SUM(Cash) AS date_total_Cash, SUM(Card) AS date_total_Card,
SUM(Amount) AS date_total_Amount
FROM `table`
GROUP BY Date
ORDER BY Date ASC') or die(mysqli_error($mysqli));
/**
* #param $row
* #param null $dateFieldHeader
*/
function printDateTableRow($row, $dateFieldHeader = null) {
?><tr><?
foreach ($row as $key => $curVal) {
?><td><?
if ($key == 'Date') {
echo $dateFieldHeader == null ? $curVal : $dateFieldHeader;
} else {
echo $curVal;
}
?></td><?
}
?></tr><?
}
$previousDate = null;
$oneDateRowsCount = 0;
while($row = mysqli_fetch_assoc($result)) {
if ($row['Date'] == $previousDate) {
$oneDateRowsCount++;
printDateTableRow($row, ' ');
} else {
if ($previousDate !== null) {
printDateTableRow(mysqli_fetch_assoc($totalMysqliResult), 'Date total');
}
printDateTableRow($row);
$oneDateRowsCount = 1;
}
$previousDate = $row['Date'];
}
// add total for the last rows with the same date
if ($oneDateRowsCount > 1) {
printDateTableRow(mysqli_fetch_assoc($totalMysqliResult), 'Date total');
}
?>
</body>
</html>
You should refactor this code in OOP style and use PDO instead of mysqli.

Pass a value from one page to another and select values from database where a column is the first page value

I have two pages, first is a page with table and the second page shows details from the table, for ex. Table shows No, Subject, Location, Date, And Piority but when i click one each row it pass the value from No Column to the second page and write it in No place. what i want in second page is to get details from database where No Column is the value i pass it from first page.
Here is my first page how i get the value to GET:
<?php
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) { ?>
<tr>
<td><?= $row['No'] ?></td>
<td><?= $row['subject'] ?></td>
<td><?= $row['location'] ?></td>
<td><?= $row['geo'] ?></td>
<td><?= $row['date'] ?></td>
<td><?= $row['piority'] ?></td>
</tr>
<?php
}
}
?>
here is my second page that i want to get data from database with No variable from first page:
<?php
$varPage = $_GET['No'];
$servername = "localhost";
$username = "bayansh_r";
$password = "u)nHf,Amo)";
$dbname = "bayansh_c";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn,"SELECT `news` FROM `editor3` WHERE No = '".$varPage."'");
while($row = mysqli_fetch_array($result))
?>
and now i want to write the News here is my code:
<p style="font-family:B Zar; direction:rtl; font-size:165%;"> <?= $row['news'] ?> </p>
but i get NULL result in Paragraph. is there any thing wrong with my code????
I have tried. I'ts working...
<?php
while( $row=mysqli_fetch_array($result)){
?>
<p style="font-family:B Zar; direction:rtl; font-size:165%;"><?php echo $row['news'];?></p>
<?php
}
?>
Here is yourfirst page how i get the value to GET:
<?php
if($result->num_rows > 0) {
while($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><? echo $row['No']; ?></td>
<td><? echo $row['subject']; ?></td>
<td><? echo $row['location']; ?></td>
<td><? echo $row['geo']; ?></td>
<td><? echo $row['date']; ?></td>
<td><? echo $row['piority']; ?></td>
</tr>
<?php
}
}
?>
here is your second page that you want to get data from database with No variable from first page:
<?php
$varPage = $_GET['No'];
$servername = "localhost";
$username = "bayansh_r";
$password = "u)nHf,Amo)";
$dbname = "bayansh_c";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (mysqli_connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn,"SELECT news FROM editor3 WHERE No = '".$varPage."'");
while($row = mysqli_fetch_assoc($result)){?>
<p style="font-family:B Zar; direction:rtl; font-size:165%;"><?php echo $row['news'];?></p>
<?php } ?>
Hope it will works if any problem please comment.

PHP MySQL JOIN tables, while loop

I've been reading about MySQL Foreign Keys and using Parent/Child relationship tables.
Below is the table relationship (phpmyadmin). Table "dpuchanges" has a foreign key (PARENT) relationship with table "opendpu", column (ECRNUM).
For some reason I am not pulling any data (see php code below). I'm thinking its my SQL statement, but I cannot figure out where it went wrong. I'm not receiving any PHP errors. Can anyone help?
<!DOCTYPE html>
<html>
<head></head>
<body>
<table>
<?php
require ('config.php');
$db = null;
$limit = 10;
$counter = 0;
while (true) {
try {
$db = new PDO($dsn, $uname, $pword);
$db->exec( "SET CHARACTER SET utf8" );
$db->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC );
$db->setAttribute( PDO::ATTR_PERSISTENT, true );
break;
}
catch (Exception $e) {
$db = null;
$counter++;
if ($counter == $limit)
throw $e;
}
}
$stmt = $db->prepare("SELECT ACTION, PARTNO, ACTIONTXT, REV_WAS, REV_NOW, QTY_FROM, QTY_TO FROM dpuchanges JOIN opendpu ON opendpu.ECRNUM = dpuchanges.PARENT WHERE opendpu.ECRNUM = 82095");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$action=$row["ACTION"];
$partno=$row["PARTNO"];
$actiontxt=$row["ACTIONTXT"];
$rev_was=$row["REV_WAS"];
$rev_now=$row["REV_NOW"];
$qty_from=$row["QTY_FROM"];
$qty_to= $row["QTY_TO"];
?>
<tr>
<td>Action = <?php echo $action; ?></td>
<td>PartNo = <?php echo $partno; ?></td>
<td>Description = <?php echo $actiontxt; ?></td>
<td>REV WAS = <?php echo $rev_was; ?></td>
<td>REV NOW = <?php echo $rev_now; ?></td>
<td>QTY FROM = <?php echo $qty_from; ?></td>
<td>QTY TO = <?php echo $qty_to; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
Your datatypes and SIZES of the related columns "dpuchanges.parent" and "openpdu.ecrnum" MUST be the same.
Source: http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
I think you are missing ;
<td>Action = <?php echo $action; ?></td>
<td>PartNo = <?php echo $partno; ?></td>
<td>Description = <?php echo $actiontxt; ?></td>
<td>REV WAS = <?php echo $rev_was; ?></td>
<td>REV NOW = <?php echo $rev_now; ?></td>
<td>QTY FROM = <?php echo $qty_from; ?></td>
<td>QTY TO = <?php echo $qty_t; ?></td>

Categories