I'm trying to loop a mysql query and display every result on new row. I know i'm missing some pices, do i need to SET variable in mysql instead?
$value = array(B1,B2,B3,B4);
$query = "SELECT SUM($value[0]) AS ".$value[0]."_SUM, "
."SUM(answer_value) AS ".$value[0]."_ANSWER_SUM "
."FROM questions q JOIN answers a ON q.question_id = a.question_id "
."WHERE $value[0]=1";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result)) {
echo "<tr><td>".$value[0]."</td>";
echo "<td>".$row['B1_ANSWER_SUM']."</td>";
echo "<td>".$row['B1_SUM'] ."</td>";
echo '</tr>' ;
}
I would like to get this result:
B1 B1_ANSWER_SUM B1_SUM
B2 B2_ANSWER_SUM B2_SUM
B3 B2_ANSWER_SUM B2_SUM
You need to loop through your array and query each item in the array. See below:
$value = array('B1', 'B2', 'B3', 'B4');
foreach ($value as $v) {
$query = "SELECT SUM($v) AS ".$v."_SUM, SUM(answer_value) AS ".$v."_ANSWER_SUM FROM questions q JOIN answers a ON q.question_id = a.question_id WHERE $v=1";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result)) {
echo "<tr><td>".$v."</td>";
echo "<td>".$row[$v.'_ANSWER_SUM']."</td>";
echo "<td>".$row[$v.'_SUM'] ."</td>";
echo '</tr>' ;
}
}
Related
I have used join and was able to get output table in phpmyadmin using this query :
$query = "SELECT members.usn, members.name, events.ename FROM members JOIN participant ON members.usn = participant.usn JOIN events ON events.eid = participant.eid WHERE members.usn='.$usn.'";
$result = $conn->query($query);
The Output was
usn
name
ename
7DC18CS005
John
Robo Wars
I used PHP to display this in my homepage for specific user by maintaining usn in their SESSION.
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<tr>';
echo '<td>';
echo $row[0];
echo '</td>';
echo '<td>';
echo $row[1];
echo '</td>';
echo '<td>';
echo $row[2];
echo '</td>';
echo '</tr>';
}
} else {
echo "No Members[Connected DB]";
}
But the output gives me
"No Members[Connected DB]"
instead of expected output which is
usn
name
ename
7DC18CS005
John
Robo Wars
Thank you!
You have a typo in the query. Looks like you are trying to concatenate $usn to the query but you got it wrong.
Change
$query = "SELECT members.usn, members.name, events.ename FROM members JOIN participant ON members.usn = participant.usn JOIN events ON events.eid = participant.eid WHERE members.usn='.$usn.'";
To
$query = "SELECT members.usn, members.name, events.ename FROM members JOIN participant ON members.usn = participant.usn JOIN events ON events.eid = participant.eid WHERE members.usn='$usn'";
I'm facing a problem to display a name on a table, only the id is displayed I have tried some inner join queries without luck i got 2 tables
Table 1 = empresas
Here is where all the data lives:
the number 9 in u_tip corresponds to the ID of the data that is in tipoempresas table
Table 2 = tipoempresas
I want to display the name of the type not the ID
I'm using this code to display the data in a html table
$result = mysqli_query($conn,"SELECT * FROM empresas");
$i = 0;
while($row = $result->fetch_assoc())
{
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
You are already there you just need to use join statement and specify the column you want to display
$result = mysqli_query($conn,"SELECT * FROM empresas AS e INNER JOIN tipoempresas AS t ON t.id = e.u_tip");
$i = 0;
while($row = $result->fetch_assoc())
{
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value['Nombre'] . "</td>"; //SPECIFY THE COLUMN YOU WANT TO DISPLAY
}
echo "</tr>";
Hi am retreiving the distinct dates then am trying to retreive the rows with that dates but am getting them as zero..
Here is code
$sql = "SELECT DISTINCT date FROM video_data ORDER BY id DESC";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$date = $row['date'];
echo "<tr>";
echo "<td>$date</td>";
$query_order = "SELECT * FROM video_data WHERE date=$date";
$query_order_total = mysqli_query($connection,$query_order);
var_dump($query_order);
$total = mysqli_num_rows($query_order_total);
echo "<td>$total</td>";
echo"<tr>";
But when am selecting "SELECT * FROM video_data" its showing correct count
you are missing ' ' around $date
$query_order = "SELECT * FROM video_data WHERE date='$date'";
That said: when you have, let's say, 60 dates found in your first query, you will than fire 60 new queries to find the actual data.
how about:
$sql = "SELECT COUNT(*) total, date
FROM video_data
GROUP BY date
ORDER BY date DESC";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>". $row['date'] ."</td>";
echo "<td>". $row['total']. "</td>";
echo"<tr>";
}
i'm trying to get foreign key object id but it's not working.
$sql2 = "SELECT a.*, q.* FROM answer a inner join question q on a.question_id = q.id WHERE a.question_id = 1";
$result2 = $conn ->query($sql2);
while($row2 = $result2->fetch_assoc()) {
echo "<input name='group1' type='radio' id='". $row2['id'] ."' />" . "<label for='". $row2['id'] ."'>".$row2['answer_text']."</label>";
}
$result2 = $conn ->query($sql2);
There have no space after $conn
Try it without space:
$result2 = $conn->query($sql2);
Hello i'm a (beginning) php backend dev and i'm working on a dj panel but it doesnt work the right way i tried as many things as i could but i cant get it to work..
$active_ids = '1, 3, 4';
$query = "SELECT * FROM users WHERE id IN ({$active_ids})";
$result = $mysqli->query($query);
$query2 = "SELECT dj, count(*) AS n FROM timetable WHERE dj IN ({$active_ids}) GROUP BY dj";
$result2 = $mysqli->query($query2);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
echo "<tr>";
echo "<td>", $row['username'] ,"</td>";
}
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()){
echo "<td>", $row2['n'] ,"</td>";
echo "</tr>";
}
}
}
this is what it shows
ZOMBOY
Hater
ZOMBOY2 3
1
1
and this is how it needs to become but i cant find a way to do it
ZOMBOY 3
Hater 1
ZOMBOY2 1
You can use join instead to querying two table
$active_ids = '1, 3, 4';
$query = "SELECT u.username, count(*) AS n FROM users u, timetable tt WHERE u.id=tt.dj and u.id IN ({$active_ids}) GROUP BY tt.dj";
$result = $mysqli->query($query);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
echo "<tr>";
echo "<td>", $row['username'] ,"</td>";
echo "<td>", $row['n'] ,"</td>";
echo "</tr>";
}
}
You can do it like this, but must be look Joins
$active_ids = '1, 3, 4';
$query = "SELECT * FROM users WHERE id IN ({$active_ids})";
$result = $mysqli->query($query);
$query2 = "SELECT dj, count(*) AS n FROM timetable WHERE dj IN ({$active_ids}) GROUP BY dj";
$result2 = $mysqli->query($query2);
$columnOne = Array();
$columnTwo = Array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
$columnOne[]= $row['username'];
}
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()){
$columnTwo[] = row2['n'];
}
}
}
echo '<table>';
for($i=0;$i<count($columnOne);$i++){
echo '<tr><td>' . $columnOne[$i] . '</td><td>' . $columnTwo[$i] . '</td></tr>';
}
echo '</table>';
You could try something such as this (Not quite as elegant as others):
# Escape your characters
$active_ids = "'1', '3', '4'";
# Tidy up the querys to reduce the change of reserved words being used
$query = "SELECT * FROM `users` WHERE `id` IN ({$active_ids});";
$result = $mysqli->query($query);
$query2 = "SELECT `dj`, COUNT(*) AS n FROM `timetable` WHERE `dj` IN ({$active_ids}) GROUP BY `dj`";
$result2 = $mysqli->query($query2);
# Count your results
$c1 = count($result);
$c2 = count($result2);
#Set the counter to be the larger of the 2
$counter = (($c1 > $c2) ? $c1 : $c2);
if ($result->num_rows > 0 && $result2->num_rows > 0)
{
# Print the table opener
print '<table class="your_class">';
# Loop through your results
for ($i = 0; $i < $counter; $i++)
{
# Print the data needed
print '<tr><td>' . $result[$i]['username'] . '</td><td>' . $result2[$i]['n'] . '</td></tr>';
}
# End the table
print '</table>';
}