I am trying to echo username.I have a table in which i only have USERID and then i take that USEDID and compare it in another table and ECHO USERNAME from there.But if there are 50 entries then it only echoes 25 .
here is the code
<?php while($row = mysqli_fetch_assoc($result)) {
$user_id = $row['user_id'];
$result2 = mysqli_query($conn, "select * from `users` WHERE `user_id` = '$user_id'");
$row2 = mysqli_fetch_assoc($result2);
while($row2 = mysqli_fetch_assoc($result2)){
$username = $row2['username'];
}
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['user_id']; ?></td>
<td><?php echo $username; ?></td>
<td><?php echo $row['points']; ?></td>
</tr>
This above code doesnt echo anything at all in username but echo all results
If i change
$row2 = mysqli_fetch_assoc($result2);
to
$row2 = mysqli_fetch_assoc($result);
It echo everything with username but only few/half results
Other code that is in connection with this
$result = mysqli_query($conn, "select * from `lottery`");
$row = mysqli_fetch_assoc($result);
Remove this line:
$row2 = mysqli_fetch_assoc($result2)
Related
I inserted the data in the table very well and it's showing in phpMyadmin but when i try to display data from the database only one single item is displayed. I need help.
Code and screenshoots below
<?php
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
$count = mysqli_num_rows($result);
if($count > 0){
while($row = mysqli_fetch_assoc($result)){
$incidentId = $row['id'];
$icTypeName = $row['inctype_name'];
$addedBy = $row['added_by'];
$addedOn = $row['added_on'];
}
?>
<tr class="tableData">
<td><?php echo $incidentId;?></td>
<td><?php echo $icTypeName;?></td>
<td><?php echo $addedBy;?></td>
<td><?php echo $addedOn;?></td>
<td><i class="uil uil-edit icon editIcon"></i></td>
</tr>
<?php
} else {
$_SESSION['noData'] = 'No incidents to show!';
}
} else {
die('Failed to Connect!');
}
?>
Images below
I expect to get all the data in the database tables displayed with the "SELECT * FROM nw"
Simply move the } that ends the while loop to after the code that is using the variables. You are currently consuming all the resultset before outputting anything, so you will only see the last rows data.
<?php
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
$count = mysqli_num_rows($result);
if($count > 0){
while($row = mysqli_fetch_assoc($result)){
$incidentId = $row['id'];
$icTypeName = $row['inctype_name'];
$addedBy = $row['added_by'];
$addedOn = $row['added_on'];
//} REMOVED
?>
<tr class="tableData">
<td><?php echo $incidentId;?></td>
<td><?php echo $icTypeName;?></td>
<td><?php echo $addedBy;?></td>
<td><?php echo $addedOn;?></td>
<td><i class="uil uil-edit icon editIcon"></i></td>
</tr>
<?php
} // END OF WHILE LOOP
} else {
$_SESSION['noData'] = 'No incidents to show!';
}
} else {
die('Failed to Connect!');
}
?>
I am creating a system in which I want to fetch data from a table complaints Now i Have two variables that on behalf of which i want to fetch data from table complaints. One is the district variable and second is pin code and there are a lot of pin codes attached to one center. For example a center signs up and he wants to work on multiple pin codes lets say 110007 and 110008 so his pin code data is stored in a table named center. And his details like his district is stored in users table. Now i have made a page in which i want to show data from a table complaints where district matches his center and pin code matches his pin code. And the code is working fine if one pin code matches but if there are multiple pin codes attached to a center and the complaint is of only 1 pin code then it shows no result but i want it to show results even if it matches only 1 or any of the pin code.
i have tried some code this is my code
session_start();
if(!isset($_SESSION['username']) || (trim($_SESSION['username']) == '')) {
header("location: login.php");
exit();
}
$username = $_SESSION['username'];
if ($username=="dhruv") {
header("location: complaint.php");
}
$sql = "SELECT * FROM users where username = '$username'";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
$district = $row['district'];
}
}
$sql = "SELECT * FROM center where username = '$username'";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
$pin = $row['id'];
echo $pin."<br>";
}
}
$sql = "SELECT *
FROM complaints
where city_pin = '$pin'
AND status = 'pending'
And district ='$district'";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$status = $row['status'];
?>
<tr>
<td><?php echo"<a href='otp.php'>".$row['center']."</a>"?></td>
<td><?php echo $row['time2']?></td>
<td><?php echo $row['product']?></td>
<td><?php echo $row['quantity']?></td>
<td><?php echo $row['warranty']?></td>
<td><?php echo $row['date_of_purchase']?></td>
<td><?php echo $row['customer_name']?></td>
<td><?php echo $row['contact']?></td>
<td><?php echo $row['customer_email']?></td>
<td><?php echo $row['customer_address']?></td>
<td><?php echo $row['city_pin']?></td>
<td><?php echo $row['issue_complaint']?></td>
<td class="red"><?php echo $row['status']?></td>
</tr>
<?php
}
} else {
echo "0 results".mysqli_error($con);
}
please feel free to ask if you didn't understand any part of my question
So i made you a mysqli procedural Code.
That you have to test,
Take all advices from the comments about sql injection and the rest.
as you can see, you have to bind the result,
So please check https://phpdelusions.net/pdo#prepared
For the future use.
So to the code.
I combined all SQL statement to one, you should still get all complaints to one user. Of course yoi shoult test ot in phpmyademin or mysql workbench if you get all the complaints.
To the mysqli i used still the procedural stile, feel free to change every thing to a more object oriented style and of course pdo
session_start();
if(!isset($_SESSION['username']) || (trim($_SESSION['username']) == '')) {
header("location: login.php");
exit();
}
$username = $_SESSION['username'];
if ($username=="dhruv") {
header("location: complaint.php");
}
$stmt = mysqli_prepare($con, "SELECT
c.id
,c.center
,c.time2
,c.product
,c.quantity
, c.warranty
, c.date_of_purchase
, c.customer_name
, c.contact
, c.customer_address
, c.city_pin
, c.issue_complaint
, c.status
FROM
complaints c
INNER JOIN (SELECT `district` FROM users where username = ?) u
ON c.`district` = u.`district`
INNER JOIN (SELECT id FROM center where username = ?) ce
ON c.city_pin = ce.id
where
c.status = 'pending';";
mysqli_stmt_bind_param($stmt, "ss", $username, $username);
mysqli_stmt_execute($stmt);
if (mysqli_stmt_affected_rows($stmt)) > 0) {
/* bind result variables */
mysqli_stmt_bind_result($stmt,$id, $center, $time2, $product, $quantity,
$warranty, $date_of_purchase, $customer_name, $contact,
$customer_address, $city_pin, $issue_complaint,$status);
/* fetch values */
while (mysqli_stmt_fetch($stmt)) {
?>
<tr>
<td><?php echo"<a href='otp.php'>".$center."</a>"?></td>
<td><?php echo $time2 ?></td>
<td><?php echo $product ?></td>
<td><?php echo $quantity ?></td>
<td><?php echo $warranty ?></td>
<td><?php echo $date_of_purchase ?></td>
<td><?php echo $customer_name ?></td>
<td><?php echo $contact ?></td>
<td><?php echo $customer_email ?></td>
<td><?php echo $customer_address ?></td>
<td><?php echo $city_pin ?></td>
<td><?php echo $issue_complaint ?></td>
<td class="red"><?php echo $status ?></td>
</tr>
<?php
}
} else {
echo "0 results".mysqli_error($con);
}
/* close connection */
mysqli_close($link);
I want to show multiple records from database upon some criteria in to html table, I only get 1 record, but I have 2 records in database which meet the criteria.
Please help to display all records from database which meets the criteria.
<?php
$count = 1;
$query = "SELECT * FROM career group by pno";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
$pno = $row['pno'];
$query = "SELECT sum(duration) as total_duration FROM career where pno = $pno";
$result = mysqli_query($connection, $query);
$Value = $row = mysqli_fetch_assoc($result);
$total_value = implode(",", $Value);
if ($total_value < 54) {
continue;
}
$rank_query = "SELECT name,pno,rank,medical_category,staff_course_isc FROM user_general_info where pno = $pno and rank in ('manager','staff') and medical_category = 'Aye' and staff_course_isc = 'Yes'";
echo $rank_query;
$result = mysqli_query($connection, $rank_query);
$row = mysqli_fetch_assoc($result);
$pno = $row['pno'];
$name = $row['name'];
$rank = $row['rank'];
// $total_value= implode(",", $Value['name']);
// echo $total_value; exit;
?>
<tr>
<td><?php echo $count; ?></td>
<td><?php echo strtoupper($pno); ?></td>
<td><?php echo strtoupper($name); ?></td>
<td><?php echo strtoupper($rank); ?></td>
</tr>
<?php $count++;
} ?>
Here is my database rows
Record 1 : Pno: 2222, name: test1, rank : manger , medical_category: Aye : duration : 59, staff_course_isc: Yes
Record 2 : Pno: 4301234, name:test2, rank : staff , medical_category: Aye : duration : 122, staff_course_isc: Yes
You should name your variables differently:
<?php
$count = 1;
$query = "SELECT * FROM career group by pno";
$result1 = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result1))
{
$pno = $row['pno'];
$query = "SELECT sum(duration) as total_duration FROM career where pno = $pno";
$result2 = mysqli_query($connection, $query);
$Value = $row = mysqli_fetch_assoc($result2);
$total_value = implode(",", $Value);
if ($total_value < 54) continue;
$rank_query = "SELECT name,pno,rank,medical_category,staff_course_isc FROM user_general_info where pno = $pno and rank in ('manager','staff') and medical_category = 'Aye' and staff_course_isc = 'Yes'";
echo $rank_query;
$result3 = mysqli_query($connection, $rank_query);
$row = mysqli_fetch_assoc($result3);
$pno = $row['pno'];
$name = $row['name'];
$rank = $row['rank'];
// $total_value= implode(",", $Value['name']);
// echo $total_value; exit;
?>
<tr>
<td><?php echo $count; ?></td>
<td><?php echo strtoupper($pno); ?></td>
<td><?php echo strtoupper($name); ?></td>
<td><?php echo strtoupper($rank); ?></td>
</tr>
<?php $count++;
} ?>
I'm making a page which needs to display all the rows stored in a table named 'events' in a database named 'school'
The problem is that even though I've multiple entries/rows in the table, for some reason only a single row is displayed when I run the page.
Here's my code-
<?php
require("includes/common.php");
$query = "SELECT * FROM school.events";
$result = mysqli_query($con, $query)or die(mysqli_error($con));
$row = mysqli_fetch_array($result);
$ename = $row['name'];
$place = $row['place'];
$date = $row['date'];
?>
.
.
.
<?php
while ($row) {
?>
<tr>
<td><?php echo $date; ?></td>
<td><?php echo $ename; ?></td>
<td><?php echo $place; ?></td>
</tr>
<?php
$row = mysqli_fetch_array($result);
$name = $row['name'];
$place = $row['place'];
$date = $row['date'];
}
?>
Try instead while($row = mysqli_fetch_assoc($result)){ ... } , you're just assigning the array to $row, you can't loop an array saying while($array)
Do it this way:
while($row = mysqli_fetch_assoc($result)){
echo $row['id'];
echo $row['name'];
}
That is because $row = mysqli_fetch_array($result); will only pull one row and then move the pointer. In order to get all the rows, use a while loop
while($row = mysqli_fetch_array($result)) {
//code to populate table one row at a time
}
Hello my first question here, I have a bad of trouble getting this code to work or better said how to proceed.
I have 2 tables, table 1 holds a string separated by commas, after exploding the array I want to compare the strings to another table that holds product prices related to the string.
<?php
function packages()
{
global $db;
$query = "SELECT * FROM product_package WHERE package_id > 1; ";
$result = mysqli_query($db, $query) or die('<h3>Query failed</h3>');
$rowcount = mysqli_num_rows($result);
if ($rowcount < 1)
{
return;
}
else
{
while ($row = mysqli_fetch_array($result))
{
$singles = explode(',', $row["product_ids"]);
$query2 = "SELECT product_price FROM products WHERE product_id = $single; ";
$result2 = mysqli_query($db, $query2);
?>
<tr>
<td><?php echo $row["package_id"] ?></td>
<td><?php echo $row["package_name"] ?></td>
<td><?php echo $row["product_ids"] ?></td>
<td>
EDIT
</td>
<td>
<?php
foreach($singles as $single)
{
echo $single . '<br />';
}
?>
</td>
</tr>
<?php
}
}
}
?>
How do I echo the prices that are overlapping with $single?