I've done this before, and it worked. I am trying to echo out specific rows on my database in a table. Here is my code:
<?php
$connect = mysql_connect("localhost", "xxx", "xxx") or
die ("Hey loser, check your server connection.");
mysql_select_db("xxx");
$quey1="select * from `Ad Requests`";
$result=mysql_query($quey1) or die(mysql_error());
?>
<table border=1 style="background-color:#F0F8FF;" >
<caption><EM>Student Record</EM></caption>
<tr>
<th>Student ID</th>
<th>Student Name</th>
<th>Class</th>
</tr>
<?php
while($row=mysql_fetch_array($result)){
echo "</td><td>";
echo $row['id'];
echo "</td><td>";
echo $row['twitter'];
echo "</td><td>";
echo $row['why'];
echo "</td></tr>";
}
echo "</table>";
?>
It gives me no errors, but It just shows a blank table with none of these rows.
My Question: How come this wont show any rows in the table, what am I doing wrong?
mysql_fetch_array() returns a numerically-keyed array, e.g. $row[1]. You want mysql_fetch_assoc() instead. Or use mysql_fetch_row(), which fetches a dual-array - numerical AND string keys.
<?php
while($row=mysql_fetch_array($result)){
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['twitter'];
echo "</td><td>";
echo $row['why'];
echo "</td></tr>";
}
echo "</table>";
?>
I think tr's and td's where not correct
You forgot to open a new table row for every row of your database.
while($row=mysql_fetch_array($result)){
echo "<tr>"; // Your code lacks this
echo "</td><td>";
Currently, your outputted HTML will have a lot of table row closures </tr> but no openers, hence the lack of table rows in your resulting visual output.
Related
After I printed those values by using PHP, I got confused how to get the value of a selected cell.
<div>
<php
$query = mysql_query(select * from items, $conn);
echo "<table border='1'>
<th width ='120'>itemID</th>
<th width ='120'>itemName</th>";
while(row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>".$row['itemID']."</td>";
echo "<td>".$row['itemName']."</td>";
}
echo "</table>";
?>
</div>
seems you doing fine, iv'e just edited some typos, added tags,and changed to: "mysql_fetch_assoc" function. try this:
<div>
<?php
$query = mysql_query("select * from items", $conn);
echo "<table border='1'>
<tr>
<th width ='120'>itemID</th>
<th width ='120'>itemName</th>
</tr>";
while($row = mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>".$row['itemID']."</td>";
echo "<td>".$row['itemName']."</td>";
echo "</tr>";
}
echo "</table>";
?>
</div>
With the code below I have to report to a table of rows from a database.
But in addition to the lines that I need, is displayed containing parts in php code (noName).
I try to keep only the echo tag
echo $row[0];
but i get the error
Notice: Undefined offset: 1 in test.php on line 16
MYSQL
ID | Team
------------------------------
1 test1
2 test2
3 test3
PHP
<?php
$connection = mysqli_connect("YourHost","user","password","dbName") or die('connection to DB failed');
$query = mysqli_query($connection,"SELECT team FROM s1");
while ($row = mysqli_fetch_array($query,MYSQLI_NUM)) {
?>
<table>
<tr>
<td><p> team name <?php if(isset($row[0])){
echo $row[0];
}else{
echo 'noName';
} ?></p></td>
</tr>
<tr>
<td><p> team name <?php if(isset($row[1])){
echo $row[1];
}else{
echo 'noName';
} ?></p></td>
</tr>
<?php } ?>
Output in php page:
test1
noName
test2
noName
You are selecting just 1 field that is team from the database.
So in your code PHP if you write $row[0] is the same if you write $row['team'].
$row[1] so is empty.
If you want print the name of all team you have to write this code:
...
echo "<table>";
while ($row = mysqli_fetch_array($query,MYSQLI_NUM)) {
?>
<tr>
<td><p> team name <?php echo $row[0]; ?></p></td>
</tr>
i would write your code like this:
<?php
$connection = mysqli_connect("YourHost","user","password","dbName") or die('connection to DB failed');
$query = mysqli_query($connection,"SELECT team FROM s1");
if($query) {
echo "<table>";
while ($row = mysqli_fetch_array($query,MYSQLI_NUM)) {
echo "<tr>";
echo "<td><p> team name " . $row['team'] . "</p></td>";
echo "</tr>";
}
echo "</table>";
}
?>
Look at your SQL query:
"SELECT team FROM s1"
You're only selecting one value from the table, the value called team. So each row returned by this query will have only one value. That value is in $row[0]. Hence, there is no value in $row[1]. In order to populate $row[1] with something, your SELECT will need at least a second value:
"SELECT team, someOtherValue FROM s1"
Of course, if your table has no other values, then there's nothing else to display...
It looks like you're trying to display the same conceptual value twice in each loop. Why? Generally each iteration of the loop would add a single row to the HTML table, not two rows. Something like this:
while ($row = mysqli_fetch_array($query,MYSQLI_NUM)) {
?>
<table>
<tr>
<td><p> team name <?php if(isset($row[0])){
echo $row[0];
}else{
echo 'noName';
} ?></p></td>
</tr>
<?php } ?>
I'm not sure about the title, I tried my best.
I have a table displayed with information from a database using this file
display.php
<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("tournaments") or die(mysql_error());
$result = mysql_query("SELECT * FROM tournies")
or die(mysql_error());
echo '<table id="bets" class="tablesorter" cellspacing="0" summary="Datapass">
<thead>
<tr>
<th>Tournament <br> Name</th>
<th>Pot</th>
<th>Maximum <br> Players</th>
<th>Minimum <br> Players</th>
<th>Host</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>';
while($row = mysql_fetch_array( $result )) {
$i=0; if( $i % 2 == 0 ) {
$class = "";
} else {
$class = "";
}
echo "<tr" . $class . "><td>";
echo $row['tour_name'];
$tour_id = $row['tour_name'];
echo "</td><td>";
echo $row['pot']," Tokens";
echo "</td><td class=\"BR\">";
echo $row['max_players']," Players";
echo "</td><td class=\"BR\">";
echo $row['min_players']," Players";
echo "</td><td class=\"BR\">";
echo $row['host'];
echo "</td><td>";
echo "<input id=\"delete_button\" type=\"button\" value=\"Delete Row\" onClick=\"SomeDeleteRowFunction(this)\">";
echo "</td><td>";
echo "<form action=\"join.php?name=$name\" method=\"POST\" >";
echo "<input id=\"join_button\" type=\"submit\" value=\"Join\">";
echo "</td></tr>";
}
echo "</tbody></table>";
?>
Basically I want the user to press a button from a row of the table and they go to a new page called join.php. I need the persons username and the name of the tournament from the row the clicked.
For example here's my page:
When they click the join button at the end of row one it should send them to
'join.php?name=thierusernamehere&tourname=dfgdds'
Any help much appreciated. Thanks.
echo '<td>Join</td>'
There are many way to approach.
The easiest way is just echo 'JOIN';
or you can use a form with hidden input and submit button.
BUT
Your code is really a mess, try to make your code more maintainable and readable. And do NOT use any mysql_* functions, they are deprecated.
Read more about PDO:
http://php.net/manual/en/book.pdo.php
http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/
I've created a PHP program for adding and viewing reminders. The add page works, but I'm having some trouble displaying them properly. How should I code this to only get the actual data? Also, how could I put this into an HTML table? (i.e. column for name, description, and date; rows are the retrieved data)
Thanks
When I open the file in my browser I get this as an output:
Array ( [reminderID] => 14 [reminderName] => Test [reminderDescript] => Test_Descript [reminderDate] => 2012 05 7 )
Code:
<?php include 'header.php'; ?>
<?php include 'database.php'; ?>
<div id="content">
<h1>Reminder List</h1>
<table align ="center">
<thead>
<tr>
<td>Name</td>
<td>Description</td>
<td>Date</td>
</tr>
</thead>
<?php
$query = 'SELECT * FROM reminder_event';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
print_r($row);
}
?>
</table>
<p><a href='reminder_add.php'>Add Reminder</a></p>
</div>
<?php include 'footer.php'; ?>
print_r() is a diagnostic tool for debugging, not to be used for real output. Instead, output HTML to a table using the array keys fetched from your row.
// Open a table
echo "<table>";
while($row = mysql_fetch_assoc($result)) {
// Output each row
echo "<tr>
<td>{$row['reminderName']}</td>
<td>{$row['reminderDescript']}</td>
<td>{$row['reminderDate']}</td>
</tr>";
}
// Close the table
echo "</table>";
Better still, escape each of the values for HTML output with [htmlspecialchars()] (http://us3.php.net/manual/en/function.htmlspecialchars.php) before output to prevent cross-site scripting attacks and broken HTML if characters like < > & are encountered in the values.
echo "<table>";
while($row = mysql_fetch_assoc($result)) {
// Encode all values for HTML output
$name = htmlspecialchars($row['reminderName']);
$desc = htmlspecialchars($row['reminderDescript']);
$date = htmlspecialchars($row['reminderDate']);
// Then output the encoded values
echo "<tr><td>$name</td><td>$desc</td><td>$date</td></tr>";
}
echo "</table>";
Change:
while($row = mysql_fetch_assoc($result)) {
print_r($row);
}
To:
while($row = mysql_fetch_assoc($result)) {
echo $row['reminderID'];
echo $row['reminderName'];
echo $row['reminderDescript'];
echo $row['reminderDate'];
}
You can echo those values out in whatever HTML you'd like. So, for example, if you want it in a table you would do something like this:
echo "<table><tr>";
while($row = mysql_fetch_assoc($result)) {
echo "<td>" . $row['reminderID'] . "</td>";
echo "<td>" . $row['reminderName'] . "</td>";
echo "<td>" . $row['reminderDescript'] . "</td>";
echo "<td>" . $row['reminderDate'] . "</td>";
}
echo "</tr></table>";
You can clean that up a bit to take some (or all) of the HTML out of the PHP.
<?php include 'header.php'; ?>
<?php include 'database.php'; ?>
<div id="content">
<h1>Reminder List</h1>
<table>
<thead><tr><td>id</td><td>name</td><td>description</td><td>date</td></tr></thead>
<tbody>
<?php
$query = 'SELECT * FROM reminder_event';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['reminderID']; ?></td>
<td><?php echo $row['reminderName']; ?></td>
<td><?php echo $row['reminderDescript']; ?></td>
<td><?php echo $row['reminderDate']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<p><a href='reminder_add.php'>Add Reminder</a></p>
</div>
<?php include 'footer.php'; ?>
The code below isn't working as intended. I was wanting 3 columns with the first column being a site name, the second column being the category, and the third being the URL for RSS (haven't gotten to that part yet). The data is in the database but when I view the result of the statements below, the site name results link to the current web page I am on. I would greatly appreciate any assistance with getting each column of data to display on the page correctly. Thank you
<?php
$query="SELECT * FROM SOMETABLE";
$result=mysql_query($query);
$num = mysql_numrows($result);
echo "
<table border='1'>
<th>Site Name:</th>
<th>Category:</th>
<th>RSS:</th>";
$i=0;
while ($i < $num) {
$siteName =mysql_result($result,$i,"siteName");
$category =mysql_result($result,$i,"category");
$category =mysql_result($result,$i,"url");
$rss =mysql_result($result,$i,"rss");
echo "
<tr>
<td><a href='$url'>$siteName</a></td>
<td>$category</td>
<td>$rss</td>
</tr>";
$i++;
}
?>
echo "<table>";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr>";
echo "<td>$line ["siteName"]</td>";
echo "<td>$line ["category"]</td>";
echo "<td>$line ["url"]</td>";
echo "<td>$line ["rss"]</td>";
echo "</tr>";
}
echo "</table>";
skip $num, mysql_resuls ( the way how you do it ) open row TR in headers ( I skipped it ) and close table
echo "
<table border='1'>
<th>Site Name:</th>
<th>Category:</th>
<th>RSS:</th>";
Should be:
echo "
<table border='1'>
<tr>
<th>Site Name:</th>
<th>Category:</th>
<th>RSS:</th>
</tr>";
To start with.
Or just use some tool like SDTable.com or jqGrid and let them do all job for you =)