MySQL row being outputted twice - php

I need to output a name from the last added row of my table, and it works sort of, it outputs the correct data twice. So instead of name1 it gives me name1name1. I'm still a novice with PHP but i think there is a double loop somewhere in this piece of code which is giving me a hard time, but i'm still unable to locate where that is..
If someone can give me a pointer that would be greatly appreciated.
<?php $query="SELECT question FROM poll ORDER BY id DESC LIMIT 1";
$results = mysql_query($query);
while ($row = mysql_fetch_array($results)) {
echo '<tr>';
foreach($row as $field) {
echo '<td>' . htmlspecialchars($field) . '</td>';
}
echo '</tr>';
} ?>

By default, mysql_fetch_array() returns values both as associative and as enumerated (MYSQL_BOTH); specify either one or the other
while ($row = mysql_fetch_array($results, MYSQL_ASSOC)) {
EDIT
But the mysql extension is old and deprecated, and you really should be switching to mysqli or pdo with prepared statements and bind variables. If you're just learning, then it's best to learn the right methods with the right extensions from the start

No need of the while. You are fetching a single row. And use mysql_fetch_assoc for associative array
$row = mysql_fetch_assoc($results);
echo '<tr>';
foreach($row as $field) {
echo '<td>' . htmlspecialchars($field) . '</td>';
}
echo '</tr>';

I would use mysql_fetch_assoc. Because this will only fetch the results as an associative array.
$query="SELECT question FROM poll ORDER BY id DESC LIMIT 1";
$results = mysql_query($query);
while ($row = mysql_fetch_assoc($results)) {
echo '<tr>';
foreach($row as $field) {
echo '<td>' . htmlspecialchars($field) . '</td>';
}
echo '</tr>';
}

Related

How to print the whole database table from phpmyadmin using PDO in php file

I am trying to print the whole database in tabular format in a php file using PDO. i have this database stored in phpmyadmin. But there are a lot of rows in there like name,id, etc etc... I have this following php code. i already made a connection to the database and added require_once() in the page. But i dont know how to print all these values in a tabular method. like showing it in a way a normal database will look like.
$q="SELECT * FROM `employee`";
$sth = $odb->prepare($q);
$sth->execute();
while ($r = $sth->fetch(PDO::FETCH_ASSOC)){
// code here
}
Can someone help me to display the table properly. That is if i run it in browser, i should see a table instead of that ugly array format
Ideally each table should have his own format, but if you just want to pop all the data from the base inside a HTML table, you could do something like that :
$sql = 'SELECT * from page';
$result = $pdo->query($sql);
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
if(count($result)) {
echo '<table><tr>';
foreach ($rows[0] as $columnName => $value) {
echo '<th>' . $columnName . '</th>';
}
echo '</tr>';
foreach ($rows as $row) {
echo '<tr>';
foreach ($row as $value) {
echo '<td>' . $value . '</td>';
}
echo '<tr>';
}
echo '</table>';
}
You could use this on each table you want to show.

Can I display query results without naming columns?

Is there a php or html code to dynamically generate a table.
query :
"SELECT * FROM table1;
Can I display this grid of info without any specifics.
IF there are 4 rows and 4 columns I want the table to be that size; if 5x5 than that.
It seems like this should be possible, but all code I can find wants me to specify names or columns.
Yes. There are a couple different ways to do this, but for illustration I will assume that the result of your query is stored in a variable called $results, which is simply a multidimensional array that you can loop through using a double foreach to dynamically produce your table.
echo '<table>';
foreach ($results as $row) {
echo '<tr>';
foreach ($row as $col) {
echo '<td>' . $col . '</td>';
}
echo '</tr>';
}
echo '</table>';
OR if you don't have a $results array and are instead getting query results and building the table at the same time, something like this might be more appropriate for your needs:
echo '<table>';
while ($row = mysqli_fetch_array($query)) {
echo '<tr>';
foreach ($row as $col) {
echo '<td>' . $col . '</td>';
}
echo '</tr>';
}
echo '</table>';
echo'<table>';
while($row = mysql_fetch_array($result))
{
echo'<tr>';
echo '<td>'.$row[colname].'</td>';
echo '</tr>';
}
echo'</table>';

Pull rows with only certain data in them PHP & Mysql

I'm trying to pull information and display it in a table form on a php page i have a mysql DB i have something like below which list everything in the table but i would like to have it only show certain information if a certain data is present
Columns i would like to display on the page
object_id, Name and, wish.
but i only want rows from the DB to be displayed if a column
wishState is ("pending")
this is what i have scoured around to find so far wich brings everything in.
$query="SELECT * FROM MY_TABLE";
$results = mysql_query($query);
while ($row = mysql_fetch_array($results)) {
echo '<tr>';
foreach($row as $field) {
echo '<td>' . htmlspecialchars($field) . '</td>';
}
echo '</tr>';
}
Thanks Ryan
The entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_, is officially deprecated as of PHP v5.5.0 and will be removed in the future. So please don't use mysql_* anymore.
//selects data where wish is pending
$query="SELECT * FROM `MY_TABLE` WHERE `wish`='pending'";
$results = mysqli_query($your_db_connection, $query) or exit(mysqli_error());
while ($row = mysqli_fetch_array($results)) {
//show only object_id, name and wish
echo '<tr>';
echo '<td>' . $row['object_id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['wish']. '</td>';
echo '</tr>';
}
$query="SELECT * FROM MY_TABLE WHERE `wish`='pending'";
That should work based on the information you gave us. WHERE tells the database to search for rows WHERE the column WISH is equal to "pending".

Using PHP foreach to get mysql table data

ive got 1 database table contains row:
TABLE FROM reservations:
attandee01
attandee02
attandee03
attandee04
attandee05
attandee06
PHP CODE:
$q = $db->query("SELECT * FROM bs_events
LEFT JOIN bs_reservations ON bs_reservations.id_event = bs_events.id");
while($r = $q->fetch_array(MYSQLI_ASSOC)):
echo '<td>' . $r['attandee1'] . '</td>';
echo '<td>' . $r['attandee2'] . '</td>'
echo '<td>' . $r['attandee3'] . '</td>'
endwhile;
or is there any simple way using foreach to echo attandee1 - attandee10?
$q = $db->query("SELECT * FROM bs_events
LEFT JOIN bs_reservations ON bs_reservations.id_event = bs_events.id");
while($r = $q->fetch_array(MYSQLI_ASSOC)):
foreach($r as $value) {
echo '<td>' . $value . '</td>';
}
endwhile;
Should echo each column value per table row.
EDIT: If you only want the attendees:
$q = $db->query("SELECT * FROM bs_events
LEFT JOIN bs_reservations ON bs_reservations.id_event = bs_events.id");
while($r = $q->fetch_array(MYSQLI_ASSOC)):
for($i = 1; $i < 11 $i++) {
echo '<td>' . $r['attendee'.$i] . '</td>';
}
endwhile;
you can use foreach, of course, as $r is a regular array and can be iterated using foreach() operator. Did you try it?
However, looking at the field names, I suspect serious design flaw in your data structure.
It seems attandees should be stored in another table.
You may also consider using templates. Printing data directly from the database loop is very bad practice. You have to get all your data first and only then start printing it out.
Well yes:
for($i=1; $i<11; ++$i) {
echo "<td>".$r["attandee".$i]. "</td>";
}
But that is columns, what you want is the row based solution for that I'd use something like:
for($r=$q->fetch_assoc(); !is_null($r); $r= $q->fetch_assoc()) {
echo "<td>".array_pop($r)."</td>"; // output the only element in the array?
}
$q->free(); // don't forget to free the memory of the result set!
don't know if i understood your question... is this what you're looking for:
while($row = $q->fetch_array(MYSQLI_ASSOC)):
foreach($row as $field){
echo '<td>' . $field . '</td>';
}
endwhile;
Not really sure if this is what you are looking for, but give it a shot:
while($r = $q->fetch_array(MYSQLI_ASSOC)) {
foreach($r as $value) {
echo '<td>' . $value . '</td>';
}
}
As example you can use alternate syntax.
<table>
<?php foreach ($mysqli->query($sql) as $row ): ?>
<tr><td><?php echo $row['row_name1']; ?></td><td><?php echo $row['row_name2']; ?></td></tr>
<?php endforeach; ?>
</table>

php, mysql how can I produce a dynamic html with fields in the first column and data in the next colums?

I found this great example which takes an sql statement and dynamically adds the field names into a header row and the data into the following rows?
Heres the example http://www.weberdev.com/get_example-4249.html
Since HTML tables must be written out by row, you'll have to read all the data before you're able to write out your table.
$rows = array();
while ($row = mysql_fetch_assoc($result)) $rows[] = $row;
// We'll use the first row to get the field names:
foreach($rows[0] as $field => $throwaway) {
echo '<tr>';
echo '<td>' . htmlspecialchars($field) . '</td>';
// And for each field iterate through ALL rows:
foreach($rows as $row) {
echo '<td>' . htmlspecialchars($row[$field]) . '</td>';
}
echo '</tr>';
}

Categories