<?php
$host="localhost";
$user="root";
$pwd="";
$db="assigment";
$conn=mysqli_connect($host,$user,$pwd,$db);
$query="SELECT * FROM 'tdata'";
$result=mysqli_query($conn,$query);
while ($row=mysqli_fetch($result)) {
?>
<table class="table">
<thead>
<tr>
<th>Full Name</th>
<th>Email</th>
<th>Birthday</th>
<th>Gender</th>
<th>Intrests</th>
<th>Address</th>
<th></th>
</thead>
</tr>
<tbody>
<?php echo "<tr><td>".$row[full_name]."</td></tr>";
}
?>
</tbody>
</table>
Error# Fatal error: Uncaught Error: Call to undefined function mysqli_fetch() in C:\xampp\htdocs\gsoft\assigment\tabledb.php:9 Stack trace: #0 {main} thrown in C:\xampp\htdocs\gsoft\assigment\tabledb.php on line 9
if you want fetch without index use mysqli_fetch_assoc().
Or
if you want to fetch with index use mysqli_fetch_array()
And i think you are doing wrong here because your table tag is inside while loop and your <thead> too.
i think it should be outside of while loop. and you have syntax error in your query and you have one extra <th>
your full code.
<?php
$host="localhost";
$user="root";
$pwd="";
$db="assigment";
$conn=mysqli_connect($host,$user,$pwd,$db);
$query="SELECT * FROM tdata ";
$result=mysqli_query($conn,$query); ?>
<table class="table">
<thead>
<tr>
<th>Full Name</th>
<th>Email</th>
<th>Birthday</th>
<th>Gender</th>
<th>Intrests</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php while ($row=mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['full_name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
</tr>
<?php }
?>
</tbody>
</table>
use mysqli_fetch_array function instead of mysqli_fetch
Use mysqli_fetch_array instead of mysqli_fetch
Also your table is inside the loop
Related
I want to print invoice data. but some error only one image print. how to print all data in an invoice I am using PHP 7.2 and loop foreach please help me...
error->Warning: Invalid argument supplied for foreach() in D:\xammp\htdocs\tam\admin\invoice.php on line 133
PHP GET id wise invoice data script
<?php
require 'setting/config.php';
$id=$_GET['id'];
$query="select * from orders where id='$id'";
$galrun=mysqli_query($conn, $query);
$result=mysqli_fetch_assoc($galrun);
$total=$result['total'];
$array[0]=$result['image'];
$array[1]=$result['productName'];
$array[2]=$result['product_qty'];
$array[3]=$result['salseprice'];
$resultdata= implode(",", $array);
?>
HTML invoice
<table class="table">
<thead>
<tr>
<th>Sr. No.</th>
<th>image</th>
<th>Description</th>
<th>Qty</th>
<th>Amount</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php
foreach ($resultdata as $item){
?>
<tr>
<td> ? </td>
<td><img src="image/product/<?php echo $item[0] ; ?>" style="width:100px;height:50px;"></td>
<td><?php echo $item[1] ; ?></td>
<td><?php echo $item[2] ; ?></td>
<td><?php echo $item[3] ; ?></td>
<td><?php echo $total ; ?</td>
</tr>
<?php }?>
</tbody>
</table>
You try to loop through $resultdata, but it is a string, because of this part:
$resultdata= implode(",", $array);
PHP implode() returns a string.
I am creating a simple database that can do basic CRUD operations (create, read, update, delete) using php. I am able to complete the create, and able to see the results if I directly query the mySQL DB in the back end. But, I am having trouble getting the table to display on the webpage. It is instead displaying a "Bad Gateway" error if I attempt to display the database entries.
I tried removing the reference to the table, specifically
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>...
and the web page on front end works fine. Albeit can only see the data if I query the backend.
<?php include('php_code.php'); ?>
...
...
...
<?php $results = mysqli_query($db, "SELECT * FROM info"); ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['city']; ?></td>
<td>
<a href="test.php?edit=<?php echo $row['id']; ?>"
class="edit_btn" >Edit</a>
</td>
<td>
<a href="php_code.php?del=<?php echo $row['id']; ?>"
class="del_btn">Delete</a>
</td>
</tr>
<?php } ?>
</table>
<!--in php_code.php-->
//to retrieve records
$select_query = "SELECT * FROM info";
$result = mysqli_query($db, $select_query);
I should be able to see the table with data containing name, address and city. But I am getting a 502 error instead.
Try this
<?php
include('php_code.php');
$results = mysqli_query($db, "SELECT * FROM `info` ");
$return = <<<HTML
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
HTML;
while ($row = mysqli_fetch_array($results)) {
$return .= <<<HTML
<tr>
<td>{$row['name']}</td>
<td>{$row['address']}</td>
<td>{$row['city']}</td>
<td><a href="test.php?edit={$row['id']}" class="edit_btn" >Edit</a></td>
<td>Delete</td>
</tr>
HTML;
}
$return .= <<<HTML
</tbody>
</table>
HTML;
echo $return;
?>
your php_code.php should really only have the database config...
you are closing the php statements so you cannot retrieve the result of your query. Don't split php parts and just echo html like this
<?php
echo " <table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan='2'>Action</th>
</tr>
</thead> ";
while ($row = mysqli_fetch_array($results)) {
echo "<tr>
<td>$row['name']</td>
<td>$row['address']</td>
<td> $row['city']</td>
<td>
<a href='test.php?edit=$row['id']'
class='edit_btn' >Edit</a>
</td>
<td>
<a href='php_code.php?del=$row['id']'
class='del_btn'>Delete</a>
</td>
</tr>";
}
echo "</table>";
?>
I want to list all users data from the database in PHP. there will be a user is logged-in at a single time. I want un-fetch or hide a single row who has active(logged-in). so, I'm asking you how can I do this...
$query=$conn->pdo->exec("SELECT * FROM usertable");
$row=$query->fetch();
<thead>
<tr>
<th>User Name</th>
<th>Name</th>
<th>Email</th>
<th>Role</th>
<th>Post</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php while($row=$query->fetch()){ ?>
<tr>
<td><?php echo $row['username']; ?>
</td>
<td><?php echo ucfirst($row['name']); ?>
</td>
<td><?php echo $row['username']; ?></td>
<td><?php echo ucfirst($row['user_type']); ?></td>
<td><?php echo $row['id']; ?></td>
</tr>
</tbody>
You can add new field in your usertable like logged with default value 0.when user login you should change 0 to 1.Now you change your sql query **select * from usertable where logged='0';**
I am creating a table that fetches data from an SQL database using a PDO method. The data loads fine but the issue I'm having is that the 'for loop' I'm using is multiplying a (table header) after every (table row).
I am wondering what a possible solution the the issue could be.
Any help is appreciated, thanks!
Here is the code:
<?php
for($i=0; $row = $result->fetch(); $i++){
?>
<table id="eventstable">
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
</table>
<?php }
?>
Up at the very top is the connection file that creates a connection to my local database and a statement that brings in the information I want to display from the database like so:
$result = $conn->prepare("SELECT * FROM events");
$result->execute();
Few possible solutions are
i) Put the header part and the table opening and closing tag outside the for loop. This will give you an empty table with headers if there is no data.
ii) Put an if condition and print headers only when i = 0, and put table tags outside the loop. This will give you an empty table with nothing if there is no data.
Edit: Method II (since you are learning)
<table id="eventstable">
<?php
for($i=0; $row = $result->fetch(); $i++){
if($i == 0){
?>
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<?php }//if statment ends here ?>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
<?php }
?>
I would also suggest since you are learning, use better ways than for loop. Look at the php PDO manuals and see the use of while or foreach. It will help more.
Put in loop only, what should be looped, everything else should be outside of loop.
For example, your code could look like this
<table id="eventstable">
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<?php
for($i=0; $row = $result->fetch(); $i++){
?>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
<?php
}
?>
</table>
try this :
<table id="eventstable">
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<?php
foreach($result->fetch() as $row){
?>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
<?php }
?>
</table>
I have this table element with the following code:
<?php
if(isset($_POST["submit"])){
if (strlen($cIdMsg = 0) && strlen($cFirstNameMsg = 0) && strlen($cLastNameMsg = 0) && strlen($pCodeMsg = 0)) {
require_once("conn.php");
$sql2 = "SELECT * FROM customer;";
$results = mysqli_query($conn, $sql2)
or die ('Problem with query' . mysqli_error($conn));
echo "no errors found";
}
}
?>
<table>
<tr>
<th>Customer ID</th>
<th>FIrst Name</th>
<th>Last Name </th>
</tr>
<?php
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row["customerID"]?></td>
<td><?php echo $row["firstName"]?></td>
<td><?php echo $row["lastName"]?></td>
</tr>
<?php } ?>
</table>
Above this table I have the php code that makes the sql queries inside an if isset condition so that it only loads after pressing submit on the form. I would like to do the same to the table. That is to only make it load after pressing submit. because on page load it is trying to do the mysqli_fetch_array on a non existent $result yet
Wrap the whole table inside:
<?php if (isset($result)) { ?>
<table>
<tr>
<th>Customer ID</th>
<th>FIrst Name</th>
<th>Last Name </th>
</tr>
<?php
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row["customerID"]?></td>
<td><?php echo $row["firstName"]?></td>
<td><?php echo $row["lastName"]?></td>
</tr>
<?php } ?>
</table>
<?php } ?>
I have used isset($result) based on what you have said. You can check for the POST values by checking for count($_POST), or something similar (not a good idea to check for isset($_POST["submit"])). If you are fetching for AJAX Response, it is always better to use a different separate file for this.
<?php if(mysqli_num_rows($result)!=0) { ?>
<table>
<tr>
<th>Customer ID</th>
<th>FIrst Name</th>
<th>Last Name </th>
</tr>
<?php
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row["customerID"]?></td>
<td><?php echo $row["firstName"]?></td>
<td><?php echo $row["lastName"]?></td>
</tr>
<?php } ?>
</table>
<?php } ?>