How do I echo an inner join column name - php

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.

Related

Using aliases in PHP script to join 1 table twice for 2 different outputs

In one table I have team name and team ID. In another table I have Winner and Runner up which are both linked to team ID (but I need to change the ID to team name using an alias). I need to link the winner and runner up with the team name from the teams table. Can anyone help me with it to display both winner and runner up instead of the team ID number. The images below should explain it better than me.
I can get it so the table works without using an Alias but can only get either the winner or runner up column to work. On image 4 it displays the team ID but I need it to display the Team name. Team name and Team ID are linked on the teams table in the database.
I've been told to use inner joins as such:
INNER JOIN teams t_winner ON tournament_info.winner = t_winner.team_id
INNER JOIN teams t_runner ON tournament_info.winner = t_runner.team_id
Then use:
<td align="center"><?php echo $row["t_runner.team_name"]; ?></td>
to display the output but doesn't seem to work.
Here is my code:
<!DOCTYPE html>
<html>
<head>
<table align="left" width="100%" border="1" style="border-collapse:collapse;">
<tr>
<th><strong>Game Name</strong></th>
<th><strong>Tournament Name</strong></th>
<th><strong>Winner</strong></th>
<th><strong>Runner Up</strong></th>
<th><strong>End Date</strong></th>
</tr>
</thead>
<tbody>
<?php
$count=1;
$sel_query="SELECT *
FROM tournament_info
INNER JOIN games ON tournament_info.game = games.id
INNER JOIN tournaments ON tournament_info.tournament = tournaments.tournament_id
INNER JOIN teams t_winner ON tournament_info.winner = t_winner.team_id
INNER JOIN teams t_runner ON tournament_info.runner_up = t_runner.team_id
GROUP BY tournament_name
ORDER BY finish_date DESC
LIMIT 10";
$result = mysqli_query($con,$sel_query);
while($row = mysqli_fetch_assoc($result)) { ?>
<td align="center"><?php
if(!empty($row["game_url"])){ ?>
<?= $row["game_name"]; ?>
<?php }else {
echo $row["game_name"];
}
?></td>
<td align="center"><?php
if(!empty($row["tournament_url"])){ ?>
<?= $row["tournament_name"]; ?>
<?php }else {
echo $row["tournament_name"];
}
?></td>
<td align="center"><?php echo $row["team_name.t_winner"]; ?></td>
<td align="center"><?php echo $row["team_name.t_runner"]; ?></td>
<td align="center"><?php echo date('F j, Y', strtotime($row["finish_date"])); ?></td>
</tr>
<?php $count++; } ?>
</tbody>
</table>
</html>

count vales in db

I'm looking to upload a cvs into a db and then find how many times an instance of data appears to create a pick list.
I upload a cvs in to the db leaving me with
SKU and QUANTITY
I use to get the data from the db but I cant seam to find a way to group that data so their is only 1 sku for each item and a number of ordered items.
<table border="1" width="100%" id="table1">
<?php
$query = mysql_query("select * from pickcount");
while($fetch = mysql_fetch_array($query))
{
?>
<tr>
<td><?php echo $fetch['sku']; ?></td>
<td><?php echo $fetch['quan']; ?></td>
</tr>
<?php
}
?>
</table>
<table border="1" width="100%" id="table1">
<?php
$query = mysql_query("SELECT sku, SUM(quan) AS Sum_Of_Quan FROM pickcount GROUP BY sku");
while($fetch = mysql_fetch_array($query))
{
?>
<tr>
<td><?php echo $fetch['sku']; ?></td>
<td><?php echo $fetch['Sum_Of_Quan']; ?></td>
</tr>
<?php
}
?>
</table>
Try to replace your table structure with above structure and check the result
Replace your sql query to:
select sku, sum(quan) as sum_quan
from pickcount
group by sku
PS - you'll have to change
php echo $fetch['quan'];
to
php echo $fetch['sum_quan'];

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>

How to fetch the values using inner join

I have two tables, from that i need to fetch each row value, and print it like below.
<table id="contact" class="display">
<thead>
<tr>
<td>Name</td>
<td>Business Email</td>
<td>Mobile No</td>
<td>Company Name</td>
<td>Message</td>
</tr>
</thead>
<tr>
<td><?php echo $result['0']; ?></td>
<td><?php echo $result['1']; ?></td>
<td><?php echo $result['2']; ?></td>
<td><?php echo $result['3']; ?></td>
<td><?php echo $result['4']; ?></td>
</tr><?php
}
?>
</table>
I have tried
SELECT GROUP_CONCAT(field_value)
FROM `table2` GROUP BY `submit_time`
But it will make problem in explode step.
If comma separator came in message field, explode function increase array value, i cant fetch the correct value and print it.
Kindly share the idea to fix this.
You should have a primary key or atleast an auto increment key on the first table in order to associate it with the second table. The plugin you are using might be using it by ordering it. So every table 2 entry has a table 1 entry ordered by timestamp. What you have to do is create a new table1 column called ID and make it auto increment, it wont effect the working of the plugin. Then you can use a custom query like this.
SELECT $wpdb->table1.*
FROM $wpdb->table1
INNER JOIN $wpdb->table2 ON ($wpdb->table1.ID = $wpdb->table2.ID)
WHERE $wpdb->table2.field_name = 'text-506'

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