I am working with PHP and MySQL. I need to retrieve all the rows from the database table and display on the browser in tabular form, but for some reason only the First field in the table gets displayed and others fields don't show up or are not pulled from the database.
Here is my PHP code.
<?php
include("DBConn.php");
$result = mysql_query("SELECT * FROM new_table");
if(!$result) {
die("Database query failed: " . mysql_error());
}
echo "<table>
<caption>Signal Data:</caption>
<thead>
<tr>
<th scope=\"col\">TagName:</th>
<th scope=\"col\">Enabled</th>
<th scope=\"col\">EU Value</th>
</tr>
</thead>
<tbody>";
while ($row = mysql_fetch_array($result)) {
$tag = $row["TagName"];
$status = $row["Enabled"];
$Ev = $row["EUValue"];
echo "<tr>
<th scope=\"row\">$tag</th>
<td>$status</td>
<td>$EV</td>
</tr>";
}
echo "</tbody></table>";
?>
What is wrong with this PHP code? The code itself executes fine without any error.
The third row can be corrected by matching the case of the variable names. You are assigning the value to a variable named $Ev (lowercase 'v'):
$Ev = $row["EUValue"];
but attempting to use a variable named $EV (uppercase 'V'):
<td>$EV</td>
For both the second and third fields, ensure that Enabled and EUValue exactly match the names of the fields in your database, including case and spelling.
Finally, check the data itself to ensure that the fields have data in your database, and that the data is displayable in your HTML output.
Related
I have implemented a table from data tables
Link [https://datatables.net/]
i would like to use two tables in one site with different columns and datas in the columns after the mysqli connection i insert the data sets with a while mysqli fetch array function the first table works properly
"urlaubstage" -> is correct
but table 2
no matter what i do even var_dump i dint not get any reaction but the table is display correctly on the page but with empty columns
This is the html code
<table id="table_id" class="display">
<thead>
<tr>
<th>Urlaubstage Jahr</th>
<th>Urlaubstage Anspruch</th>
<th>Urlaubstage Beansprucht</th>
<th>Urlaubstage Rest</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($sql)){
$urlaubsTage = $row[4];
echo "<tr>";
echo "<td>{$urlaubsTage}</td>";
echo "<td>Anspruch</td>";
echo "<td>Beansprucht</td>";
echo "<td>Rest</td>";
echo "halllo";
}
echo "</tr>";
?>
</tbody>
</table>
!!!!!!!!!!!!<p>HERE STARTS THE SECOND TABLE</p>!!!!!!!!!!!!!!!!!!!!!!!!
<table id="table_id2" class="display">
<thead>
<tr>
<th>Urlaub Antragsdatum</th>
<th>Urlaub Startdatum</th>
<th>Urlaub Enddatum</th>
<th>Urlaubs Status</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($sql)){
var_dump($row); -> EVEn VAR_DUMP IS NOT SHOWN
echo "<tr>";
echo "<td>Antragsdatum</td>";
echo "<td>Startdatum</td>";
echo "<td>Enddatum</td>";
echo "<td>Status</td>";
echo "halllo";
}
echo "</tr>";
?>
</tbody>
</table>
JQUERY CODE
....
$('#table_id').DataTable();
//FUNKTION FÜR ZWEITE TABELLE
$('#table_id2').DataTable();
....
Picture of Code and Table
ok i got the answer by myself, after the first loop runs the query $sql with mysqli_fetch_array($sql) you cant use it anymore on the second loop why? cause it ran on the first loop and its over solution
rename;
$sql = mysqli_query(same query);
$sql2 = mysqli_query(same query);
first loop while($row = mysqli_fetch_array($sql);
second loop while($row = mysqli_fetch_array($sql2);
You don't need to execute the same query twice. You don't need the while loop either. Try to get the results into an array and then loop the array multiple times.
$result = $conn->query($sql)->fetch_all();
//...
foreach ($result as $row) {
//...
}
// Repeat the same loop again without calling query again
//foreach...
I want to put same value in one column in whole table.
The table looks like this
Table Image
For suppose, I want to put status as Community owner for all Lvl7's.
Here's the table code,
<table class="table" id="myTable">
<thead>
<tr class="header">
<th width="60%"></th>
<th width="20%"></th>
<th width="20%"></th>
</tr>
</thead>
<?php
$query = $config->prepare("SELECT playerName, playerLevel FROM `playerdata` WHERE playerLevel = 7 ORDER BY playerName DESC");
$query->execute();
while($data = $query->fetch())
{
echo "<tr><td>".$data['playerName']."</td>";
echo "<td>".$data['playerLevel']."</td></tr>";
}
?>
</table>
How do I put the value as community owner for this table.
I have same tables like these for all levels.
I dont know how to echo the third column using and or print the same value for all rows in Status column.
When I tried to code, it only printed the value Community Owner to the last row of table.
P.S:- I am rookie in all this stuff, any type of help is appreciated.
while($data = $query->fetch())
{
echo "<tr><td>".$data['playerName']."</td>";
echo "<td>".$data['playerLevel']."</td>";
switch ($data['playerLevel'])
{
case 7:
echo "<td>Community owner</td></tr>";
break;
default:
echo "<td>Not defined</td></tr>";
break;
}
}
//Changed data['playerLevel'] to $data['playerLevel'] from the original post in switch statement, it worked :D
sorry, I'm still very novice with server-side (back-end) development, thank you for helping in advance.
Recently, I've been developing a web page which you can choose which table you want to display from the mySQL server, then it will create an editable table in HTML using the mySQL table data.
So far, I'm only able to fetch which table to get from mySQL and display it using in the form of an array, which doesn't really look that great.
I wonder if you can display the array in a form of an editable table without knowing the how many columns, column names as it is different for every different table.
Later on, I have to upload the cell that is updated back to mySQL database and I've no idea how to do that as well.
Sorry for the trouble, this website had been a great help for me and the community is great. Thanks a lot!
Image of what I have so far:
Code I have so far:
SelectTable:
<div class="table-responsive">
<b>List of Tables</b>
<table class="table table-condensed selection_table">
<tbody>
<tr>
<?php
if ($tableResult = mysqli_query($conn,"show tables")){
while($table = mysqli_fetch_array($tableResult)) {
echo("<tr> <td>". "<a class = 'list_tables' href = ?clickedTable=$table[0]>". $table[0] . "</a>" ."</td> </tr>");
}
}else{
die("<b>"."No Table in Database!"."</b>");
}
if (isset($_GET['clickedTable'])){
$selectedTable = $_GET["clickedTable"];
}
?>
</tr>
</tbody>
</table>
</div>
Fetch array from selected table:
<?php
if (isset($_GET['clickedTable'])){
echo("<b> Current Table is: </b> ".$selectedTable. "<br/>");
$query = "SELECT * FROM $selectedTable";
if ($result = mysqli_query($conn , $query)) {
while ($row = mysqli_fetch_array($result)){
print_r($row);
}
}
}else{
echo("Please select a table");
}
?>
I'm building an exam management website and one of the pages I'm working on is for adding students to a course. I have a dropdown menu for the student number (which fetches values from a table), however I'd like to make it so that when the teacher selects the student number from the dropdown menu, that student's name and major appear on a table below. I have pretty much all the code for it however I can't seem to make it work. The way it is right now it shows the head of the table but it doesn't show any lines.
The errors are always in the lines where I declare $sql1 and $sql2 and vary according to how I define the condition in the statement.
Code for my dropdown menu : (works fine)
<label class="control-label" for="number">Student Number</label>
<?php
$sql = "SELECT number FROM students";
$result = $conn->query($sql);
echo "<select class=".'"form-control"'.' id="number" name="number" for="number">';
while ($row = $result->fetch_assoc()) {
echo '<option value="' . $row['number'] . '">' . $row['number'] . "</option>";
}
echo "</select>";
?>
Code for my table : (shows only head of table, which is the best I got after moving around the code and getting conversion errors and such)
The errors are always in the lines where I declare $sql1 and $sql2 and vary according to how I define the condition in the statement.
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Major</th>
</tr>
</thead>
<tbody>
<?php
$sql1 = "SELECT name FROM students WHERE number='$row'";
$result1 = $conn->query($sql1);
$value = $result1->fetch_object();
$sql2 = "SELECT major FROM students where number='$row'";
$result2 = $conn->query($sql2);
$value1 = $result2->fetch_object();
echo "<tr>
<td>".$value."</td>
<td>".$value1."</td>
</tr>";
?>
</tbody>
</table>
Thank you for all your help!!
Before I can formulate a complete answer, I must advise you that there are a few logical errors in your code.
How does your page "know" that a user selected an option from the select? You should perhaps intercept the event and respond to that using an asynchronoys mechanism, e.g. via AJAX.
Anyhow, there's no need to run two queries when you can make it with just one:
SELECT name, major FROM students WHERE number = ...
Once you have described how you mean to address issue #1 we can continue discussing the complete solution.
Well, I think there will be no $row in the the second snippet.
It seems that you didn't pass your $row from 1st snippet to 2nd snippet.
You can read this:
PHP Pass variable to next page
You can use session, cookie, get and post.
Or can just simply use "include", then the variables you defined can be used in the second page.
<?php
include "page1.php";
?>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Major</th>
</tr>
</thead>
<tbody>
<?php
$number = $row['number'];
$sql1 = "SELECT name, major FROM students WHERE number='$number'";
$result1 = $conn->query($sql1);
$value = $result1->fetch_object();
echo "<tr>
<td>".$value['name']."</td>
<td>".$value['major']."</td>
</tr>";
?>
</tbody>
</table>
According to godzillante's answer below, the mysql query should be like this:
Anyhow, there's no need to run two queries when you can make it with just one:
SELECT name, major FROM students WHERE number = ...
I notice that you use $row as the key of your second query.
But in the first snippet, the data you fetch is "$row" (it is an array, see PHP - fetch_assoc)
You should use $row['number'] instead.
So I'm trying to make a HTML table that gets data from a MySQL database and outputs it to the user. I'm doing so with PHP, which I'm extremely new to, so please excuse my messy code!
The code that I'm using is: braces for storm of "your code is awful!"
<table class="table table-striped table-hover ">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Description</th>
<th>Reward</th>
<th>Column heading</th>
</tr>
</thead>
<tbody>
<?php
$con = mysql_connect("localhost", "notarealuser", 'notmypassword');
for ($i = 1; $i <= 20; $i++) {
$items = ($mysqli->query("SELECT id FROM `items` WHERE id = $i"));
echo ("<tr>");
echo ("
<td>
while ($db_field = mysqli_fetch_assoc($items)) {
print $db_field['id'];
}</td>");
$items = ($mysqli->query("SELECT name FROM `items` WHERE id = $i"));
echo ("
<td>
while ($db_field = mysqli_fetch_assoc($items)) {
print $db_field['name'];
}</td>");
$items = ($mysqli->query("SELECT descrip FROM `items` WHERE id = $i"));
echo ("
<td>
while ($db_field = mysqli_fetch_assoc($items)) {
print $db_field['descrip'];
}</td>");
$items = ($mysqli->query("SELECT reward FROM `items` WHERE id = $i"));
echo ("
<td>
while ($db_field = mysqli_fetch_assoc($items)) {
print $db_field['reward'];
}</td>");
$items = ($mysqli->query("SELECT img FROM `items` WHERE id = $i"));
echo ("
<td>
while ($db_field = mysqli_fetch_assoc($items)) {
print $db_field['img'];
}</td>");
echo ("</tr>");
}
?>
</tbody>
</table>
However, this code is not working - it simply causes the page to output an immediate 500 Internal Server Error. IIS logs show it as a 500:0 - generic ISE. Any ideas?
You are mixing mysql and mysqli, not closing php code block and you are not selecting a database. Plus you don't have to run a query for each field
Try this:
<table class="table table-striped table-hover ">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Description</th>
<th>Reward</th>
<th>Column heading</th>
</tr>
</thead>
<tbody>
<?php
$con = new mysqli("host","user", "password", "database");
$execItems = $con->query("SELECT id, name, descrip, reward, img FROM `items` WHERE id BETWEEN 1 AND 20 ");
while($infoItems = $execItems->fetch_array()){
echo "
<tr>
<td>".$infoItems['id']."</td>
<td>".$infoItems['name']."</td>
<td>".$infoItems['descrip']."</td>
<td>".$infoItems['reward']."</td>
<td>".$infoItems['img']."</td>
</tr>
";
}
?>
</tbody>
</table>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Description</th>
<th>Reward</th>
<th>Column heading</th>
</tr>
</thead>
<tbody>
<?php
$con = mysqli_connect("hostname","username",'password');
$sql= "SELECT * FROM `items` WHERE id <20 ";
$items = (mysqli_query($sql));
while ( $db_field = mysqli_fetch_assoc($items) ) {?>
<tr><td><?php echo $db_field['id'];?></td></tr>
<tr><td><?php echo $db_field['name'];?></td></tr>
<tr><td><?php echo $db_field['descrip'];?></td></tr>
<tr><td><?php echo $db_field['reward'];?></td></tr>
<tr><td><?php echo $db_field['img'];?></td></tr>
<?php}
</tbody>
</table>
Try these, not tested
Where is the question?
There's many problems with this code.
First, you are confused between PHP and HTML.
Code between is PHP. It's executed on the server, you can have loops and variables and assignments there. And if you want some HTML there you use "echo".
Code outside is HTML - it's sent to the browser as is.
Second - what you seem to be doing is querying each field separately. This is not how you work with SQL.
Here's more or less what you need to do:
//Query all rows from 1 to 20:
$items = $mysqli->query("SELECT id,name,descrip,reward,img FROM `items` WHERE id between 1 and 20");
//Go through rows
while ( $row = mysqli_fetch_assoc($items) )
{
echo "<tr><td>{$db_field['id']}</td>";
//echo the rest of the fields the same way
});
I'm going to go ahead and assume that the code isn't working and that's because there's several basic errors. I'd strongly suggest doing some hard reading around the topic of PHP, especially since you're using databases, which, if accessed with insecure code can pose major security risks.
Firstly, you've set-up your connection using the procedural mysql_connect function but then just a few lines down you've switched to object-orientation by trying to call the method mysqli::query on a non object as it was never instantiated during your connection.
http://php.net/manual/en/mysqli.construct.php
Secondly, PHP echo() doesn't require the parentheses. PHP sometimes describes it as a function but it's a language construct and the parentheses will cause problems if you try to parse multiple parameters.
http://php.net/manual/en/function.echo.php
Thirdly, you can't simply switch from HTML and PHP and vice-versa with informing the server/browser. If you wish to do this, you need to either concatenate...
echo "<td>".while($db_filed = mysqli_fetch_assoc($item)) {
print $db_field['id'];
}."</td>;
Or preferably (in my opinion it looks cleaner)
<td>
<?php
while($db_filed = mysqli_fetch_assoc($item)) {
print $db_field['id'];
}
?>
</td>
However, those examples are based on your code which is outputting each ID into the same cell which I don't think is your goal so you should be inserting the cells into the loop as well so that each ID belongs to its own cell. Furthermore, I'd recommend using echo over print (it's faster).
Something else that may not be a problem now but could evolve into one is that you've used a constant for you FOR loop. If you need to ever pull more than 20 rows from your table then you will have to manually increase this figure and if you're table has less than 20 rows you will receive an error because the loop will be trying to access table rows that don't exist.
I'm no PHP expert so some of my terminology might be incorrect but hopefully what knowledge I do have will be of use. Again, I'd strongly recommend getting a good knowledge of the language before using it.