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 } ?>
Related
I only can list out all the data row by row but I want to list out those 2 products like this from top to bottom, not row by row within
index.php
while ($row = mysqli_fetch_array($results)) {
echo '<td><'.$row['name'].'></td>';
}
server.php
$db = mysqli_connect('localhost','root','','crud');
$results = mysqli_query($db, "SELECT * FROM info");
If I really understand, you want each product to be in a different line. If yes, you could create new rows directly inside your loop like this
while ($row = mysqli_fetch_array($results)) {
echo '<tr><td>'.$row['name'].'</td></tr>';
}
<?php
while($row = mysqli_fetch_array($results)){
$detail_row.="<td>" . $row['items'] . "</td>";
$category_row.="<td>" . $row['items'] . "</td>";
}
?>
<table>
<tr>
<td>Details</td>
<?php echo $detail_row; ?>
</tr>
<tr>
<td>Categories</td>
<?php echo $category_row; ?>
</tr>
</table>
I am outputting newsletter email registrations.
How can I format them into tables and what edits can I do so that I can drop that row also.
My PHP code is :
<?php include "navbar-datacheck.php" ?>
<?php
include "../backend/db.php";
$sql = "SELECT * from newsletter";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
print "id: " . $row["id"]. " - Email ID: " . $row["email"]."<br>";
}
} else {
echo "0 results";
}
$mysqli->close();
?>
Construct your table comprising of all the newsletter details in the following manner,
// your code
if ($result->num_rows > 0) {
// output data of each row
?>
<table>
<tr>
<td>ID</td>
<td>Email</td>
<td>Action</td>
</tr>
<?php
while($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["email"]; ?></td>
<td>Delete</td>
</tr>
<?php
}
?>
</table>
<?php
} else {
echo "0 results";
}
// your code
... what edits can I do so that I can drop that row also.
As you can see, I have added a third column Action in your table so that you could delete a row of your choice at any given time. So once the user clicks on Delete link, this is how you should process and delete the corresponding record in delete.php file.
if(isset($_GET['id']) && !empty($_GET['id'])){
$id = $_GET['id'];
// Now delete the corresponding record from the table
}
I have this table im trying to display users, being 2 users per 2 columns, then list down. Here is what i have so far:
<?php $result = mysql_query("SELECT * from users WHERE adminlevel='5'");
while($row = mysql_fetch_array($result)) { echo
" <table>
<tr>
<td width='85' align='left'><br><center>". $row['username'] . "</center>
</td>
<td align='right'><center></center>
</td>
</tr>
<td width='85' align='left'><center></center>
</td>
<td align='right'><center></center>
</td>
</table>";
} ?>
This just displays the members as rows going down, and missing out the other column completely. I was hoping that it would display the username in each of the table fields. I also did try putting ". $row['username'] ." in the other fields too, but that just duplicated it.
EDIT:
So iv'e changed it to this, I can't see going down as I only have 2 members, Would this work:
<?php $result = mysql_query("SELECT * from users WHERE adminlevel='5'"); ?>
<table>
<tr>
<?php while($row = mysql_fetch_array($result)) { echo
"<td width='85' align='left'><font size='1px'><center>". $row['username'] . "</font></center></td>
<td align='right'><center></center></td>";
} ?>
</tr>
</table>
example:
try something like this
<table>
<tr>
<th>name</th>
<th>name</th>
</tr>
<?php
$result = mysql_query("SELECT * from users WHERE adminlevel='5'");
$i = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($i == '0') echo "<tr>";
echo "<td>{$row['username']}</td>";
if ($i == '1') echo "</tr>";
$i++;
if($i =='2')$i='0';
}
?>
</table>
I think you're asking why the other fields in your "user" mysql table aren't showing up.
They aren't there because you only asked for the username:
$row['username']
If you have first/last name in your mysql table, you can retrieve it in the same way:
$row['firstname']
$row['lastname']
In your code you got the row as a key/value array like this:
$row = mysql_fetch_array($result)
The "key" is the name of the mysql column, like username, lastname, firstname. And the value is what is stored in the mysql table under that row/column, like joecool, smith, joe.
I'm trying to make a search form that return some data from an oracle database,it works fine except that it skip the first row of table .
I used the following code :
enter code here
if(isset($_POST['search']) && !empty($_POST["search_box"])){
$name=$_POST['search_box'];
$sql='SELECT deejays.name,available_dates.data, available_dates.venue,available_dates.location FROM deejays,available_dates';
$sql .=" WHERE deejays.name ='{$name}'";
$sql .=' AND pk_id=fk_id';
$verify="SELECT deejays.name FROM deejays WHERE deejays.name ='{$name}'";
$stid = oci_parse($conn,$sql );
oci_execute($stid);
if (oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)){
echo "<table width=\"100%\" align=\"center\" cellpadding=\"5\" cellspacing=\"5\">
<tr class=\"source\">
<td colspan=3 align=\"center\"><h1>The facebook tour dates for ".$name." are:</h1></td>
</tr>
<tr align=\"center\" class=\"source1\">
<td><strong>DATA</strong></td>
<td><strong>VENUE</strong></td>
<td><strong>LOCATION</strong></td>
</tr>";?>
<?php while($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)){ ?>
<tr align="center" class="rows">
<td ><?php echo $row['DATA'];?></td>
<td ><?php echo $row['VENUE'];?></td>
<td ><?php echo $row['LOCATION'];?></td>
</tr>
<?php } ?>
</table>
<?php } else {
$stid = oci_parse($conn,$verify );
oci_execute($stid);
if (oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS))
{ echo "This artist hasn't records on facebook/bandsintown<br>
enter code here
the expected result is:
and the real result is:
The second image shows that the first row is skipped,how could I solve this?Thanks in advance
As I commented, oci_fetch_array() will return an array containing the next result-set row of a query. Which means everytime you call it, the cursor will move to the next row. I have two suggestions.
First
Load all the result into an array after oci_execute(). And count the array if it has any row.
oci_execute($stid);
$result = array();
while($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS))
{
$array[] = $row;
}
//check if result is not empty
if (count($result)) {
//the table part here.
}
And when you want to display it in html table, use foreach loop to iterate the array and build the <tr>.
<?php foreach($result as $row) { ?>
<tr align="center" class="rows">
<t ><?php echo $row['DATA'];?></td>
<td><?php echo $row['VENUE'];?></td>
<td><?php echo $row['LOCATION'];?></td>
</tr>
<?php } ?>
SECOND
You can use oci_fetch_all() to load all the rows from the query into an array.
oci_execute($stid);
//$result will have all the result of the current query
$count = oci_fetch_all($stid, $result);
if ($count) {
//table part here
}
And building the <tr> will be the same as the first.
Another way is to use buffered query. But this will depend on many factors.
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.