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.
Related
Hey guys I have one mysql table tick where i have some info like user id now to display that data I need to retrieve from user table name of the user based on that id
?php
$mysql5 = mysqli_query($dbc, "SELECT * FROM tick where status='1' ORDER BY dt DESC LIMIT 6 ");
if($indtbl = mysqli_fetch_array($mysql5))
{
$title = $indtbl['tickettitle'];
$company = $indtbl['companyname'];
$companyid =$indtbl['compid'];
$trackid = $indtbl['trackid'];
$assignto = $indtbl['assignto'];
$priority = $indtbl['priority'];
}
?>
and user table query is based on first one
<?php
$findresults2323 = mysqli_query($dbc, "SELECT * FROM users WHERE id= '$assignto'");
if($rest = mysqli_fetch_array($findresults2323))
{
$userimg = $rest['img'];
$fname1 = $rest['fname'];
$lname1 = $rest['lname'];
}
?>
now the problem is when i am fetching the array
<thead>
<tr>
<th class="text-left">Title</th>
<th>Company</th>
<th>Ticket ID</th>
<th>Assign To</th>
<th>Priority</th>
</tr>
</thead>
<?php
while($rowten = mysqli_fetch_array($mysql5)) {
$retrive11 = mysqli_fetch_array($findresults23);
?>
<tr>
<td><a href="readit.php?id=<?php echo $rowten["id"];?>"><?php echo $rowten["tickettitle"]; ?></td>
<td><a href="readit.php?id=<?php echo $rowten["id"];?>"><?php echo $rowten["companyname"];?> <?php echo $rowten["compid"];?></td>
<td><a href="readit.php?id=<?php echo $rowten["id"];?>"><?php echo $rowten["trackid"]; ?></td>
<td><a href="readit.php?id=<?php echo $rowten["id"];?>"><?php echo $retrive11['fname'];?></td>
<td><a href="readit.php?id=<?php echo $rowten["id"];?>"><?php echo $rowten["priority"]; ?></td>
</tr>
<?php
}
?>
i am getting the error
Notice: Trying to access array offset on value of type null on line 459
line 459 is
<td><a href="readit.php?id=<?php echo $rowten["id"];?>"><?php echo $retrive11['fname'];?></td>
what I am doing wrong
In order to use 2 queries, you need to run the second query inside of the loop so it retrieves the specific user record for the given result from the tick query.
while($rowten = mysqli_fetch_array($mysql5)) {
$assignto = $rowten['assignto'];
$findresults2323 = mysqli_query($dbc, "SELECT * FROM users WHERE id= '$assignto'");
$retrive11 = mysqli_fetch_array($findresults23);
However, this is very inefficient as it requires a query on the user table for every result in the tick query. The proper way of doing this is to use a single query with a join.
SELECT tick.*, users.fname FROM tick LEFT JOIN users ON (tick.assignto=users.id) where tick.status='1' ORDER BY tick.dt DESC LIMIT 6
The result of this query will have all the data from both your queries in one result.
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.
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>";
}
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;