Inner Join tables and display in php - php

I want to select data from more tables with Inner join.
These are my tables.
teams (id, team_name)
badges (id, badgename, badgeimage, badgedescription)
teambadges (id, team_id, badge_id)
I want to write a statement that shows the team name with all the badges they have. I also want to display this in a table
This is my statement.
$sql = mysqli_query($connection, 'SELECT teams.team_name,badges.badgename
FROM teambadges
INNER JOIN teams ON teams.id = teambadges.team_id
INNER JOIN badges ON badges.id = teambadges.badge_id;');
Php:
<table class="table table-condensed table-striped table-bordered table-hover">
<thead>
<tr>
<th width="5%"><center>No</center></th>
<th>team id</th>
<th>badge id</th>
</tr>
</thead>
<tbody id="data">
<?php $no=1; while ($row = mysqli_fetch_array($sql)) { ?>
<tr>
<td align="center"><?php echo $no; ?></td>
<td><?php echo $row['team_name']; ?></td>
<td><?php echo $row['badgename']; ?></td>
</tr>
<?php $no++; } ?>
</tbody>
</table>
This is executed inside the php page but i keep getting this error : Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result,

use
mysqli_query() or die(mysqli_error($connection))
to get your error
and check your query run successfully on mysql

i think you error is here
<td><?php echo $row['teams.name']; ?></td>
<td><?php echo $row['badges.name']; ?></td>
just chech if you have the right name column .
if you have there table with the same name
teams (id, team_name)
badges (id, badgename, badgeimage, badgedescription)
teambadges (id, team_id, badge_id)
you must do this :
<td><?php echo $row['team_name']; ?></td>
<td><?php echo $row['badgename']; ?></td>

Related

How do I echo an inner join column name

I have two tables that I have inner joined called milk and jan both has a column named id. trying to make a modal popup, but it wouldn't work bc of the ambigous id column name so i tried milk.id but still wouldn't work. is there a way around it?
this is my php query:
$sql="SELECT milk.pemohon, milk.nokp, milk.keterangan, jan.status
FROM milk
INNER JOIN jan ON milk.id = jan.users_id";
$result = mysqli_query($con,$sql);
?>
<table>
<thead>
<tr>
<th>Pemohon</th>
<th>No. KP</th>
<th>Keterangan</th>
<th>Status</th>
<th>Penghantaran</th>
</tr>
</thead>
<tbody>
<?php
while($row=mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["pemohon"]; ?></td>
<td><?php echo $row["nokp"]; ?></td>
<td><?php echo $row["keterangan"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td><button data-id='<?php echo $row['milk.id']; ?>' class="userinfo btn btn-success">Info</button></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
$sql="
SELECT
milk.id AS milk_id,
milk.pemohon,
milk.nokp,
milk.keterangan,
jan.id AS jan_id,
jan.status
FROM milk
INNER JOIN jan ON milk.id = jan.users_id
";
Now you can just select milk_id or jan_id in php using $row['milk_id'] or $row['jan_id']
To explain:
MySQL uses the column name as name, thus on milk.id it uses id.
If you have multiple select with the same column name, ex milk.id and jan.id it returns id and id(1) respectively.
Best thing is to use an alias with AS followed by the name. In this way, MySQL returns that as the name.

How to hide repeating part of the retrieved data values using PHP , MYSQL & HTML

I have obtained following table as a result of this MySQL statement
select entry.id as id,
entry.payorder,
entry.bank,
entry.PFMS_SCHEME,
entry.name,
entry.vendor,
tally_head.head,
ledgers.amount,
entry.po,
entry.Description,
bank_details.Name as name,
entry.sum as sum
from entry join bank_details on entry.name=bank_details.id
join ledgers on ledgers.id=entry.id
join tally_head on ledgers.ledger=tally_head.id
group by tally_head.head
result i got
These are the mysql tables from which i am retrieving data
entrybank tableledgertally
the final output i want
the view i want
The HTML and PHP Code I used`
<table class="table">
<thead>
<tr>
<th>Voucher No </th>
<th>Payorder </th>
<th>Bank </th>
<th>PFMS_SCHEME </th>
<th>Party Name </th>
<th> For</th>
<th> Po/agmt/inv N.o </th>
<th>Ledger</th>
<th>Ledger Amount</th>
<th>Description/ Narration</th>
<th>sum</th>
</tr>
</thead>
<tbody>
<?php
$sql=mysqli_query($con,"select entry.id as cid,entry.payorder,entry.bank,entry.PFMS_SCHEME,entry.name,entry.vendor,tally_head.head,ledgers.amount,entry.po,entry.Description,bank_details.Name as name,entry.sum as sum from entry join bank_details on entry.name=bank_details.id join ledgers on ledgers.id=entry.id join tally_head on ledgers.ledger=tally_head.id group by tally_head.head ");
$cnt=1;
while($row=mysqli_fetch_array($sql))
?>
<tr>
<td><?php echo htmlentities($row['cid']);?></td>
<td><?php echo htmlentities($row['payorder']);?></td>
<td><?php echo htmlentities($row['bank']);?></td>
<td><?php echo htmlentities($row['PFMS_SCHEME']);?></td>
<td><?php echo htmlentities($row['name']);?></td>
<td><?php echo htmlentities($row['vendor']);?></td>
<td><?php echo htmlentities($row['po']);?></td>
<td><?php echo htmlentities($row['head']);?></td>
<td><?php echo htmlentities($row['amount']);?></td>
<td><?php echo htmlentities($row['Description']);?></td>
<td><?php echo htmlentities($row['sum']);?></td>
<td>
</td>
</tr>
<?php
$cnt++;
?>
</tbody>
</table>

Fetching from two table using join clause based on id [duplicate]

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 5 years ago.
I am making a table on bootstrap fetching data in this table from two table in same database here is picture of my database
here is my code i am using to fetch value from databases
<table class="table table-hover table-striped">
<thead>
<tr>
<th>Id</th>
<th>File-name</th>
<th>Purpose</th>
<th>Recieved-By </th>
<th>Processed-By</th>
<th>Address</th>
<th>Contact-No</th>
<th>Recieved-Date</th>
<th>Update-date</th>
<th>status</th>
</tr>
</thead>
<tbody>
<?php
$p_query= "SELECT id, file_name, recieved_by, processed_by, purpose, address, contact_no, date,update_date,reason FROM files INNER JOIN update_table ON files.id=update_table.id";
$con=mysqli_connect("localhost","root","","fileprogramsysteeem");
$p_run=mysqli_query($con,$p_query);
if(mysqli_num_rows($p_run)){
while($row=mysqli_fetch_array($p_run))
{
$id=$row['id'];
$file=$row['file_name'];
$purpose=$row['purpose'];
$recieve=$row['recieved_by'];
$processed=$row['processed_by'];
$address=$row['address'];
$contact=$row['contact_no'];
$date=$row['date'];
$up_date=$row['update_date'];
$status=$row['reason'];
?>
<tr>
<td><?php echo $id;?></td>
<td><?php echo $file;?></td>
<td><?php echo $purpose;?></td>
<td><?php echo $recieve;?></td>
<td><?php echo $processed;?></td>
<td><?php echo $address;?></td>
<td><?php echo $contact;?></td>
<td><?php echo $date;?></td>
<td><?php echo $up_date;?></td>
<td style="color:#337AB7;"><?php echo $id?>"><?php echo $status;?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
Now after applying this code i am getting this error :
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result
I have to select file_name, received _by, processed_by, purpose, address, contact_no, date from table files and update_date , reason from table update_table
$p_query= "SELECT files.* , update_table.* FROM files INNER JOIN update_table ON files.id=update_table.id";
$con=mysqli_connect("localhost","root","","fileprogramsysteeem");
try this may be this would help

Wht the php query return a single row

i am developing a off-line chat application, i have two table 1. user details (cli_id,email, User name ) 2. chat table (c_from, c_to, subject, matter, image) now the problem is that i am taking the cli_id from the user table as from and to but when fetching the query it return a single row, my code looks like this
<table width="100%" border="0">
<tr>
<td width="16%"><strong>From</strong></td>
<td width="23%"><strong>Subject</strong></td>
<td width="40%"><strong>Matter</strong></td>
<td width="21%"><strong>To</strong></td>
</tr>
<?php
include('connect.php');
$sql=mysql_query("SELECT * FROM `chat` ORDER BY chat_id DESC")or die(mysql_error());
while($row=mysql_fetch_array($sql))
{
?>
<tr>
<td><?php echo $row['c_from']; ?></td>
<td><?php echo $row['subject']; ?></td>
<td><?php echo $row['matter']; ?></td>
<td><?php
$chat_to =$row['c_to'];
$sql=mysql_query("SELECT * FROM `client` WHERE cli_id = $chat_to")or die(mysql_error());
while($qry=mysql_fetch_array($sql))
{
echo $qry['email'];
}
?></td>
</tr>
<?php } ?>
</table>
You're overwriting $sql inside the loop, which replaces the result set in your outer loop with a result set which is already "emptied" by the time the code execution returns to the outer loop.
$sql variable changed inside the while loop. Use a different variable here:
$sql=mysql_query("SELECT * FROM `client` WHERE cli_id = $chat_to")or die(mysql_error());
try this :
<table width="100%" border="0">
<tr>
<td width="16%"><strong>From</strong></td>
<td width="23%"><strong>Subject</strong></td>
<td width="40%"><strong>Matter</strong></td>
<td width="21%"><strong>To</strong></td>
</tr>
<?php
include('connect.php');
$selectChat=mysql_query("SELECT * FROM `chat` ORDER BY chat_id DESC")or die(mysql_error());
while($row=mysql_fetch_array($selectChat))
{
?>
<tr>
<td><?php echo $row['c_from']; ?></td>
<td><?php echo $row['subject']; ?></td>
<td><?php echo $row['matter']; ?></td>
<td><?php
$chat_to =$row['c_to'];
$selectClient=mysql_query("SELECT * FROM `client` WHERE cli_id = $chat_to")or die(mysql_error());
while($qry=mysql_fetch_array($selectClient))
{
echo $qry['email'];
}
?></td>
</tr>
<?php } ?>
</table>
It is probably better for you to use a join in order to minimize the amount of database requests, this will also reduce the need for you to have a second query loop inside the first loop. Try the following code
<table width="100%" border="0">
<tr>
<td width="16%"><strong>From</strong></td>
<td width="23%"><strong>Subject</strong></td>
<td width="40%"><strong>Matter</strong></td>
<td width="21%"><strong>To</strong></td>
</tr>
<?php
include('connect.php');
$sql=mysql_query("SELECT * FROM `chat` LEFT JOIN 'client' on 'chat.c_to = client.cli_id' ORDER BY chat_id DESC")or die(mysql_error());
while($row=mysql_fetch_array($sql))
{
?>
<tr>
<td><?php echo $row['c_from']; ?></td>
<td><?php echo $row['subject']; ?> </td>
<td><?php echo $row['matter']; ?></td>
<td><?php echo $row['email'];?></td>
</tr>
<?php } ?>
</table>
You must need to rewrite the while statement that appears immediately after the main query
while($row=mysql_fetch_array($sql))
as
while($row=mysql_fetch_row($sql))
Hope this might help you.

mysql_fetch_assoc returns duplicated rows...

My fetch_assoc returns duplicated rows. It seems that it multiplies itself. I have 4 inputs in my table and it returns 16.
Here is my code.... Please help me. I think I got the looping wrong.
<?php
$tryshow =" SELECT c.customer_date, c.lastname, c.firstname,
s.room_number, s.date_in, s.date_out
FROM customers c
INNER JOIN services s
ON c.customer_date = s.date_in
WHERE c.customer_date = '$customer_date' ";
$result = #mysql_query($tryshow,$conn)
or die(mysql_error());
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print...";
}
?>
<form>
<table width="700" border="0">
<tr>
<td width="100">Customer Date:</td>
<td width="100">Last Name</td>
<td width="100">First Name</td>
<td width="100">Room Number</td>
<td width="100">Date In</td>
<td width="100">Date Out</td>
</tr>
<?php while($row=mysql_fetch_assoc($result)){ ?>
<tr>
<td><?php echo $row['customer_date']; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['room_number']; ?></td>
<td><?php echo $row['date_in']; ?></td>
<td><?php echo $row['date_out']; ?></td>
</tr>
<?php }?>
</table>
Thanks in advance.
-renz
You have two choices, if the query runs the same in phpMyAdmin. First, you can clean your data. If this is possible, its the best direction to go. Better data makes better applications possible. If you can't do much for the data integrity then you need to account for it. The simplest way would be to add a distinct to your query....
SELECT distinct c.customer_date, c.lastname, c.firstname,
s.room_number, s.date_in, s.date_out
FROM customers c
INNER JOIN services s
ON c.customer_date = s.date_in
WHERE c.customer_date = '$customer_date'
your on clause
ON c.customer_date = s.date_in
should be on a unique key
ON c.room_number = s.room_number

Categories