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

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

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>

First result from DB not showing when using while loop

I am creating a php script to generate reports based on data stored in SQL, The query is here:
$sql = $adb->query("SELECT firstname, lastname, policynumber, isatype, isaname, startdate, unitvalue, numberofunits, currentamount, date, newunitvalue, newnumberofunits, newcurrentamount
FROM vtiger_isa, vtiger_addisa, vtiger_contactdetails
WHERE vtiger_isa.relatedclient = vtiger_addisa.addrelatedclient
AND vtiger_addisa.addrelatedclient = vtiger_contactdetails.contactid
AND vtiger_isa.relatedclient = $clientid
AND vtiger_isa.policynumber = $polnum
AND vtiger_addisa.addrelatedclient = $clientid
AND vtiger_addisa.newpolicynumber = $polnum
ORDER BY date ASC"
);
This performs fine as I have tested by using print_r($sql); and the results I want are there. Although when I am looping through the results they do not show. I have tested with different clientid's and the first result seems to missed out.
<b> New Figures:</b>
<table style="width:100%">
<tr>
<th>Date</th>
<th>Unit Value (P)</th>
<th>Number of Units</th>
<th>Total Value (£)</th>
</tr>
<?php
while ($sql->fetchInto($row)) {
?>
<tr>
<td><?php echo $row['date'];?></td>
<td><?php echo $row['newunitvalue'];?></td>
<td><?php echo $row['newnumberofunits'];?></td>
<td><?php echo $row['newcurrentamount'];?></td>
</tr>
<?php
}?>
</table>
</body>
</html>
I am not using the fetchInto() method. I using the following example:
<?php
$sql = $adb->query("...");
foreach($sql as $row){
?>
<tr>
<td><?php echo $row['date'];?></td>
<td><?php echo $row['newunitvalue'];?></td>
<td><?php echo $row['newnumberofunits'];?></td>
<td><?php echo $row['newcurrentamount'];?></td>
</tr>
<?php
}
?>
This is working every time. Try it out.

How to search an HTML table that collects data from Mysqli [duplicate]

This question already has answers here:
How can I get an unknown username given an ID?
(2 answers)
Closed 1 year ago.
trying to do a search on an html table want to be able to type the users nickname and it gives me all the users details on the same table.
<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Nickname</th>
<th>State</th>
<th>city</th>
<th>address</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM houses";
$q=$conn->query($sql);
while ($row = mysqli_fetch_array($q)) {
?>
<tr>
<td><?php echo $row['nickname']; ?></td>
<td><?php echo $row['state']; ?></td>
<td><?php echo $row['city']; ?></td>
<td><?php echo $row['address']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
You need to use WHERE in your SQL query:
$sql = "SELECT * FROM houses WHERE nickname = '{$nickname}'";
you also need to use an isset in order to have the word from your HTML input.

Inner Join tables and display in 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>

Categories