I use this SQL query to get results from different tables
<?
$sql = "SELECT gtem.gname AS itmnme, gvendor.gname AS vendor, gtem.col AS qty
FROM gpopackageline
LEFT JOIN gpo ON gpo.gpoid = gpopackageline.gpoid
LEFT JOIN gtem ON gpopackageline.gtemid = gtem.gtemid
LEFT JOIN gvendor ON gitem.gvendorid = gvendor.gvendorid
WHERE gpopackageline.gpoid='".$sdo['swelid']."' ";
$row = dblib_get_row_list($sql);
?>
<td class="contents51" width="100%"><?=$row['itmnme']?> </td>
<td class="contents51" width="100%"><?=$row['vendor']?> </td>
<td class="contents51" width="100%"><?=$row['qty']?> </td>
Issue is I get only 1 and first result printed.
When i test query in phpmyadmin I get all the results
Does anyboby can help with this
Thank You
You need to loop through your results, either by a while, or foreach - depending on your logic.
For example
<?
$sql = "SELECT gtem.gname AS itmnme, gvendor.gname AS vendor, gtem.col AS qty
FROM gpopackageline
LEFT JOIN gpo ON gpo.gpoid = gpopackageline.gpoid
LEFT JOIN gtem ON gpopackageline.gtemid = gtem.gtemid
LEFT JOIN gvendor ON gitem.gvendorid = gvendor.gvendorid
WHERE gpopackageline.gpoid='".$sdo['swelid']."' ";
$getRows = dblib_get_row_list($sql); //Assuming this returns everything in an assoc array
foreach($getRows as $row) {
?>
<td class="contents51" width="100%"><?=$row['itmnme']?> </td>
<td class="contents51" width="100%"><?=$row['vendor']?> </td>
<td class="contents51" width="100%"><?=$row['qty']?> </td>
<?php
}
?>
I don't know what is your dblib_get_row_list is return. If it returns array then it should work as you expected.
<?
$sql = "SELECT gtem.gname AS itmnme, gvendor.gname AS vendor, gtem.col AS qty
FROM gpopackageline
LEFT JOIN gpo ON gpo.gpoid = gpopackageline.gpoid
LEFT JOIN gtem ON gpopackageline.gtemid = gtem.gtemid
LEFT JOIN gvendor ON gitem.gvendorid = gvendor.gvendorid
WHERE gpopackageline.gpoid='".$sdo['swelid']."' ";
$result = dblib_get_row_list($sql);
foreach($result as $row ){
echo "<td class='contents51' width='100%'>".$row['itmnme']."</td>";
echo "<td class='contents51' width='100%'>".$row['vendor']."</td>";
echo "<td class='contents51' width='100%'>".$row['qty']."</td>";
}
Related
I need to run two statements to return the data I need. The below code will return all needed except the count column which can be retrieved from a different table.
How can I run these two statements in the same code to retrieve the count as well?
Here is the code I have:
<?php
$query = "SELECT * from $CLIENTS_DATABASE order by id DESC";
if ($result = mysqli_query($conn, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row["name"]; ?> </td>
<td><?php echo $row["url"]; ?> </td>
<td><?php echo $row["secret"]; ?> </td>
<td><?php echo $row["count"]; ?> </td>
</tr>
<?php
}
mysqli_free_result($result);
}
?>
The other statement that the count data I need is this:
$query = "SELECT COUNT(*) as count from visits where id_client='$result[id]'"
Thanks for your time and any help you can provide.
Join the two queries.
$query = "SELECT order.*, IFNULL(c.count, 0) AS count
FROM $CLIENTS_DATABASE AS order
LEFT JOIN (SELECT id_client, COUNT(*) AS count
FROM visits
GROUP BY id_client) AS c
ON c.id_client = order.id
ORDER BY order.id DESC";
I have a database with the tables program and class. The class table has a foreign key from program, the id_program. In the PHP code below, I fetch the data from the class table and works great.
<?php
$A_apotelesma = NULL;
$query = "SELECT * FROM class";
$result = mysqli_query($con,$query) or die('Error, query failed');
$i=-1;
if ($result) {
while ($row = mysqli_fetch_array($result)) {
$i++;
$A_apotelesma[$i]['id_program'] = $row['id_program'];
$A_apotelesma[$i]['id_class'] = $row['id_class'];
$A_apotelesma[$i]['onoma'] = $row['onoma'];
$A_apotelesma[$i]['imera'] = $row['imera'];
$A_apotelesma[$i]['time_start'] = $row['time_start'];
$A_apotelesma[$i]['time_end'] = $row['time_end'];
}
}
echo "<table border=\"1\" bordercolor=\"#ff8533\">";
if(is_array($A_apotelesma)){
echo " <tr>
<td colspan='7' align='center'><strong><br>Στοιχεία υπάρχοντων τμημάτων:</strong><br><br></td>
</tr>
<tr>
<td align='left'>ID</td>
<td align='left'>Πρόγραμμα</td>
<td align='left'>Όνομα</td>
<td align='left'>Ημέρα</td>
<td align='left'>Ώρα έναρξης</td>
<td align='left'>Ώρα λήξης</td>
</tr>";
foreach($A_apotelesma as $in => $value){
echo "<tr>
<th width='16%'>".$value['id_class']."</th>
<th width='16%'>".$value['id_program']."</th>
<th width='16%'>".$value['onoma']."</th>
<th width='16%'>".$value['imera']."</th>
<th width='16%'>".$value['time_start']."</th>
<th width='16%'>".$value['time_end']."</th>
<td>
Διαγραφή τμήματος<br>
<img src=\"images/del.jpg\" onclick='delete_class(".$value['id_class'].")' onmouseover=\"this.style.cursor='pointer'\" />
</td>
</tr>";
}
}
else
echo "<tr><th>Δεν βρέθηκαν τμήματα.</th></tr>";
echo "</table>";
?>
I just would like in the foreach command the ".$value['id_program']." not to be the id_program value, but another value, called onoma from the "program" table. I tried:
SELECT onoma FROM program LEFT JOIN class ON program.id_program=class.id_program
but no luck. Any help would be really appreciated.
Something like this should work. You cannot overwite the id_program, you have to call it something else in the select statement
SELECT Program.Onoma as Onoma
FROM Program
INNER JOIN Class
ON Class.id_program = Program.id;
Need to join two tables rather than selecting values from single table.Replace your query:
$query = "SELECT c.id_class,c.imera,c.time_start,c.time_end,p.program_name,p.onoma FROM class c INNER JOIN program p ON c.id_program=p.id_program";
See program_name name is used instead of program_id to display program name from program table.
Trying to access CDID to set in the query and then use GET later to retrieve it on the next page.
I was always under the impression that INNER JOIN cdreview ON cdreview.CDID=cd.CDID would combine CDID as they are the same value, and then I could just access the value by setting it in the query as $cdid = $row['CDID']; but I keep getting Undefined index: CDID error message.
I'm a noob so any help would be appreciated.
<?php
require_once 'database_conn.php';
$userid = $_SESSION['userSession'];
$sql = "SELECT cdreview.reviewDate, cdreview.reviewText, cd.CDTitle FROM cd
INNER JOIN cdreview ON cdreview.CDID=cd.CDID AND cdreview.userID='$userid' ORDER BY cdreview.reviewDate;" or die;
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)){
$date = $row['reviewDate'];
$album = $row['CDTitle'];
$review = $row['reviewText'];
$cdid = $row['CDID'];
echo"<table align='center'>
<tr align='center'>
<td colspan='5'>
<h2>View All Reviews</h2>
</td>
</tr>
<tr align='center'>
<th>Date</th>
<th>Album</th>
<th>Reviews</th>
<th>Edit Review</th>
<th>Delete Review</th>
</tr>
<tr align='center'>
<td>$date</td>
<td>$album</td>
<td>$review</td>
<td></td>
<td></td>
</tr>
</table>";
}
} else {
echo "<script>alert('You have not left any reviews!')</script>";
echo "<script>window.open('home.php', '_self')</script>";
}
mysqli_close($conn);
?>
You are trying to get the value that you don't select
$date = $row['reviewDate'];
$album = $row['CDTitle'];
$review = $row['reviewText'];
$cdid = $row['CDID'];
but you select only
SELECT cdreview.reviewDate, cdreview.reviewText, cd.CDTitle from
you don't have a CDID column in your select
You need to add the CDID column to the select statement either using:
$sql = "SELECT cd.CDID, cdreview.reviewDate, cdreview.reviewText, cd.CDTitle FROM cd [...];"
or using:
$sql = "SELECT * FROM cd [...];"
You idea is correct that when you join on the CDID-column, that column is available for SELECT and has the same value from both tables. But it is not automatically selected - if you want to retriev it, you still need to select it.
There is nothing like an implicit SELECT where attributes that are used in JOIN or other statements are added to the SELECT. The SELECT-statement always retrievs exactly what you specify and nothing more.
I am trying create a order history page. An SQL statement that finds all the applications based on a order_ID in an orderdetails_tbl. Here is the SQL.
SELECT * FROM applications_tbl INNER JOIN orderdetails_tbl ON orderdetails_tbl.app_ID = applications_tbl.app_ID WHERE orderdetails_tbl.order_ID = $orderID
The results are displayed using this PHP.
$result = mysql_query("SELECT *
FROM applications_tbl
INNER JOIN orderdetails_tbl ON orderdetails_tbl.app_ID = applications_tbl.app_ID WHERE orderdetails_tbl.order_ID = ".$order_ID."");
$row = mysql_fetch_assoc($result);
echo "<table>";
do { ?>
<tr style="background-color:#fff">
<td> <img src="getimage.php?ID=<?php echo $row['app_ID']; ?>" width="100" height="100" alt="IMAGE" /></td>
<td width="20%"><?php echo $row['app_name']; ?></td>
<td><?php echo $row['app_desc']; ?></td>
<td width="15%"><?php echo $row['app_cost']; ?></td>
</tr>
<?php
} while ($row = mysql_fetch_assoc($result));
The SQL returns the correct values and the PHP displays them correctly. My issue is that it on the webpage it returns many duplicate results. It appears to be random, but I noticed the more apps/items in the order the more duplicate results. In phpMyAdmin when I run the SQL it doesn't display duplicates.
Cheers for your time. Any advice will be greatly received!
Use GROUP BY
$result = mysql_query("SELECT * FROM applications_tbl
INNER JOIN orderdetails_tbl ON orderdetails_tbl.app_ID = applications_tbl.app_ID
WHERE orderdetails_tbl.order_ID = ".$order_ID." GROUP BY app_ID");
A tip is to use alias to get sql-statements more readable like this:
$result = mysql_query("SELECT * FROM applications_tbl at INNER JOIN orderdetails_tbl od ON od.app_ID = at.app_ID WHERE od.order_ID = ".$order_ID);
But to answer your question you could use DISTINCT on one field and list the fields you want to have included in your recordset or add a GROUP BY -statement at the end of the SQL-statement.
DISTINCT: (example)
$result = mysql_query("SELECT DISTINCT at.id, od.app_ID FROM applications_tbl at INNER JOIN orderdetails_tbl od ON od.app_ID = at.app_ID WHERE od.order_ID = ".$order_ID);
OR
GROUP BY (example)
$result = mysql_query("SELECT * FROM applications_tbl at INNER JOIN orderdetails_tbl od ON od.app_ID = at.app_ID WHERE od.order_ID = ".$order_ID . " GROUP BY at.id");
What is the actual value of $order_ID ? (I ask because you say you have the same query and you only get duplicates when viewing in browser. It seems very odd)
I have a following problem. I have three tables: message, comment and user. I would like PHP to print the data of following fields from MySQL database as a table on my web-page: message.subject, user.username and then number of comments. Everything else works fine but I haven't managed to get PHP to print the number of comments.
This is what I have tried to do so far:
<?php
include("info.php");
$connect;
$sql="SELECT * FROM message, user
WHERE message.userID = user.userID AND
ORDER BY message.messageID DESC";
$result=mysql_query($sql) or die(mysql_error());
$sql2="SELECT message.messageID, COUNT(*) as comments FROM comment
INNER JOIN
message ON comment.messageID = message.messageID
GROUP BY comment.messageID";
$result2=mysql_query($sql2) or die(mysql_error());
<table>
<thead>
<tr>
<th>Subject</th>
<th>Sender</th>
<th>Number of comments</th>
</tr>
</thead>
<tbody>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td>
<a href="php/message.php?id=<?php echo $rows['messageID']; ?>">
<?php echo $rows['subject']; ?>
</td>
<td>
<?php echo $rows['username']; ?>
</td>
<td>
<?php while($rows2=mysql_fetch_array($result2)){ echo $rows2['comments'];}?>
</td>
</tr>
<?php
} mysql_close(); ?>
</tbody>
</table>
Couldn't you just get all this info with one query?
SELECT
m.messageID,
m.subject,
u.username,
c.numOfComments
FROM
message m
INNER JOIN user u ON m.userID = u.userID
LEFT JOIN (SELECT COUNT(1) AS numOfComments, messageID FROM comments GROUP BY messageID) c ON m.messageID = c.messageID
ORDER BY
m.messageID DESC
Try this script:
<?php
include("info.php");
connect();
$sql = "
SELECT
m.messageID,
m.subject,
u.username,
c.numOfComments
FROM
message m
INNER JOIN user u ON m.userID = u.userID
LEFT JOIN (SELECT COUNT(1) AS numOfComments, messageID FROM comments GROUP BY messageID) c ON m.messageID = c.messageID
ORDER BY
m.messageID DESC
";
$result = mysql_query($sql) or die(mysql_error());
echo "<table>
<thead>
<tr>
<th>Subject</th>
<th>Sender</th>
<th>Number of comments</th>
</tr>
</thead>
<tbody>\n";
while ($row = mysql_fetch_assoc($result))
{
$row = array_map("htmlspecialchars", $row); // sanitize to prevent XSS
echo " <tr>
<td>{$row["subject"]}</td>
<td>{$row["username"]}</td>
<td>{$row["numOfComments"]}</td>
</tr>\n";
}
echo " </tbody>
</table>";
?>
There are a few other corrections as well. For instance, your link doesn't have a closing </a> tag, and your script may be vulnerable to XSS attacks. And then there's the nested while-loop, which was causing unnecessary complication and bugs.
The problem i see is that you are nesting your while loops. Please see: http://www.php.net/manual/en/control-structures.while.php#52733
Use one sql statement.
This is the best way to do it (i think):
$sql = "
SELECT message.*, user.*, commentsCounter.comments FROM message
INNER JOIN user
ON user.userID = message.userID
INNER JOIN ( SELECT COUNT( 1 ) as comments, comment.messageID FROM comment GROUP BY comment.messageID ) commentsCounter
ON commentsCounter.messageID = message.messageID
ORDER BY message.messageID DESC
";
Why not just do something like the following?
$query = "SELECT comment FROM message where userId = $userId";
$result = mysql_query($query);
$numberOfComments = mysql_num_rows($result);
echo "$numberOfComments";
At least I believe that is what you're looking for?
Manual says this about mysql_num_rows():
mysql_num_rows — Get number of rows in result
http://www.w3schools.com/sql/sql_func_count.asp
$result3=mysql_query("COUNT(*) FROM comment") or die(mysql_error());
$row=mysql_fetch_array($result3);
$numcomments = $row[0];
echo $numcomments;
I use this (with a different table name) on a few sites.
Answer 2
Assuming you want the comments on a specifc post, try something like this:
$result3=mysql_query("SELECT DISTINCT message.messageID FROM comment ") or die(mysql_error());
$row=mysql_fetch_array($result3);
$numcomments = count($row);
echo $numcomments;