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);
Related
for the past few hours, I have been trying to find a simple method of while loop echoing information from multiple tables at once. I'd like to say I've not pulled all my hair out looking, but I have.
Here is one mysqli query to get the following fields from CUSTOMER
$tid = $_SESSION['user_id']; // "id" is 1 for example
$query = "SELECT * FROM `CUSTOMER` WHERE user_id = {$tid}";
$results = mysqli_query($dbconnection, $query);
while ($row = mysqli_fetch_array($results)) {
echo $row['user_id'] . "<br><br>";
echo $row['c_fname'] . "<br>";
echo $row['c_sname'] . "<br>";
};
Here is another mysqli query to get the following fields from SALE
$query = "SELECT * FROM `SALE` WHERE user_id = {$tid}";
$results = mysqli_query($dbconnection, $query);
while ($row = mysqli_fetch_array($results)) {
echo $row['s_date'] . "<br>";
echo $row['s_total'] . "<br>";
};
Could someone possibly show me how I can get both of these tables in one query so that echoing both tables information is possible at the same time instead of separately. I am not fussed how it is done, As long as it gets all from both tables for echoing purpose, that is good.
You can do it by using LEFT JOIN like this.
SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
And this is your code.
$query = "SELECT * FROM `CUSTOMER` LEFT JOIN `SALE` ON `SALE`.user_id=`CUSTOMER`.user_id WHERE `SALE`.user_id={$tid}";
$results = mysqli_query($dbconnection, $query);
while ($row = mysqli_fetch_array($results)) {
echo $row['user_id'] . "<br><br>";
echo $row['c_fname'] . "<br>";
echo $row['c_sname'] . "<br>";
echo $row['s_date'] . "<br>";
echo $row['s_total'] . "<br>";
}
For more info read this,
https://www.w3schools.com/sql/sql_join_left.asp
I hope this helps.
EDITED
This is for joining 3 tables,
SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
LEFT JOIN table3 ON table1.column_name = table3.column_name;
In your code.
SELECT * FROM `CUSTOMER`
LEFT JOIN `SALE` ON `CUSTOMER`.user_id = `SALE`.user_id
LEFT JOIN `PRODUCTS` ON `CUSTOMER`.user_id = `PRODUCTS`.user_id
WHERE `SALE`.user_id={$tid};
As variable.
$query = "SELECT * FROM `CUSTOMER` LEFT JOIN `SALE` ON `CUSTOMER`.user_id = `SALE`.user_id LEFT JOIN `PRODUCTS` ON `CUSTOMER`.user_id = `PRODUCTS`.user_id WHERE `SALE`.user_id={$tid}";
You can use the following code and will help u solve Ur problem
$query = "SELECT C.*,S.* FROM CUSTOMER C,SALES S
WHERE C.user_id={$tid}
and C.user_id=S.user_id;
while ($row = mysqli_fetch_array($results)) {
echo $row['C.user_id'] . "<br><br>";
echo $row['C.c_fname'] . "<br>";
echo $row['C.c_sname'] . "<br>";
echo $row['S.s_date'] . "<br>";
echo $row['S.s_total'] . "<br>";
};
You can simply join the tables to get your expected result as shown below.
$query = "SELECT c.user_id, c.c_fname, c.c_sname, s.s_date, s.s_total FROM `CUSTOMER` AS c INNER JOIN `SALE` AS s ON c.user_id = s.user_id WHERE c.user_id = {$tid}";
Joining 3 tables example
$query = "SELECT *
FROM `CUSTOMER` AS c
INNER JOIN `SALE` AS s ON c.user_id = s.user_id
INNER JOIN `PRODUCTS` AS p ON p.product_id = s.product_id
WHERE c.user_id = {$tid}";
I have been trying to fix my issue on this since morning :)
I have two tables. table1= post table2=users. now I need to extract data from both tables. searched how to use INNER JOIN, found some codes. I run the query inside phpmyadmin and it works fine here is my sql query
SELECT post.topic_id, post.category_id, post.topic_id, post.post_creator, post.post_date, post.post_content, users.username, users.gender, users.id
FROM post INNER JOIN users
ON post.post_creator=users.id
WHERE post.post_creator=users.id and post.topic_id=19
ORDER BY post.post_date DESC
but when I use this sql query inside my php it gives me error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN users ON post.post_creator=users.id ORDER BY post.post_date ASC' at line 1
below is my php code
<?php
include_once './forum_Scripts/connect_to_MySql.php';
$cid = $_GET['cid'];
if(isset($_SESSION['uid'])){
$logged = " | <a href ='create_topic.php?cid=".$cid."'>Click here to create a new topic</a> ";
}else{
$logged = " | Please log in to create topics in this forum.";
}
$tid = $_GET['tid'];
$sql = "SELECT * FROM topics WHERE category_id='".$cid."' AND id='".$tid."' LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 1){
echo "<table width='100%'> ";
if(isset($_SESSION['uid'])){
echo "<tr><td colspan='2'><input type='submit' value='Add Reply' onClick=\"window.location = 'post_reply.php?cid=".$cid."&tid=".$tid."'\" /> | <a href = 'http://amaforum.net63.net/'>Return to Forum Index</a><hr />";
}else{
echo "<tr><td colspan='2'><p>Please log in to add your reply</p><hr /></td>";}
while ($row = mysql_fetch_assoc($res)){
$sql2 = "SELECT post.topic_id, post.category_id, post.topic_id, post.post_creator, post.post_date, post.post_content, users.username, users.gender, users.id"
. "FROM post INNER JOIN users ON post.post_creator=users.id ORDER BY post.post_date ASC" ;
$res2 = mysql_query($sql2) or die(mysql_error());
while ($row2 = mysql_fetch_assoc($res2))
{
echo "<tr><td valign='top' style='border:2px solid #000000'><div style='min-height: 125px;'>".$row['topic_title']."<br />
by ".$row2['post_creator']." - ".$row2['post_date']."<hr />".$row2['post_content']."</div></td>"
. "<td width='200' valign='top' style='border: 1px solid #000000;'>"
. "<input id='".d_text."' type='".text."' name='".the_creator."' value='".$row2['post_creator']."' >"
. "echo '$user_info' "
. "</td></tr>"
. "<tr><td colspan='2'><hr /></td></tr> ";
}
$old_views = $row['topic_views'];
$new_views = $old_views + 1;
$sql3 = "UPDATE topics SET topic_views='".$new_views."' WHERE category_id='".$cid."' AND id='".$tid."' LIMIT 1 ";
$res3 = mysql_query($sql3) or die (mysql_error());
}
echo "</table>";
}else{
echo "<p>This topic does not exist</p>";
}
mysql_close();
?>
I can get it to work and I really need help from you guys..
Thanks in advance
This should solve your problem (I've just added the space that was missing when you concatenated the two lines of your sql query):
<?php
include_once './forum_Scripts/connect_to_MySql.php';
$cid = $_GET['cid'];
if(isset($_SESSION['uid'])){
$logged = " | <a href ='create_topic.php?cid=".$cid."'>Click here to create a new topic</a> ";
}else{
$logged = " | Please log in to create topics in this forum.";
}
$tid = $_GET['tid'];
$sql = "SELECT * FROM topics WHERE category_id='".$cid."' AND id='".$tid."' LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 1){
echo "<table width='100%'> ";
if(isset($_SESSION['uid'])){
echo "<tr><td colspan='2'><input type='submit' value='Add Reply' onClick=\"window.location = 'post_reply.php?cid=".$cid."&tid=".$tid."'\" /> | <a href = 'http://amaforum.net63.net/'>Return to Forum Index</a><hr />";
}else{
echo "<tr><td colspan='2'><p>Please log in to add your reply</p><hr /></td>";}
while ($row = mysql_fetch_assoc($res)){
$sql2 = "SELECT post.topic_id, post.category_id, post.topic_id, post.post_creator, post.post_date, post.post_content, users.username, users.gender, users.id "
. "FROM post INNER JOIN users ON post.post_creator=users.id ORDER BY post.post_date ASC" ;
$res2 = mysql_query($sql2) or die(mysql_error());
while ($row2 = mysql_fetch_assoc($res2))
{
echo "<tr><td valign='top' style='border:2px solid #000000'><div style='min-height: 125px;'>".$row['topic_title']."<br />
by ".$row2['post_creator']." - ".$row2['post_date']."<hr />".$row2['post_content']."</div></td>"
. "<td width='200' valign='top' style='border: 1px solid #000000;'>"
. "<input id='".d_text."' type='".text."' name='".the_creator."' value='".$row2['post_creator']."' >"
. "echo '$user_info' "
. "</td></tr>"
. "<tr><td colspan='2'><hr /></td></tr> ";
}
$old_views = $row['topic_views'];
$new_views = $old_views + 1;
$sql3 = "UPDATE topics SET topic_views='".$new_views."' WHERE category_id='".$cid."' AND id='".$tid."' LIMIT 1 ";
$res3 = mysql_query($sql3) or die (mysql_error());
}
echo "</table>";
}else{
echo "<p>This topic does not exist</p>";
}
mysql_close();
?>
To avoid issues like this in the future, it's always good practice to log the sql that's being executed in your code somewhere, or even just echo it onto the web page while testing. That way you'll very quickly debug issues like this.
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>' ;
}
}
I've asked this question before, but got no answers, so I'm asking it again but this time, I will be more specific.
I have an online forum which I created from scratch with php and mysql, I've implemented the image uploading part naming the image by the topic id, from the posts table, Now I'm having problems displaying the images by pulling the name and the extension from the image table and attaching it to the topic id to be displayed. Now this is the code snippet for displaying topics (viewtopic.php)
$sql = "
SELECT SQL_CALC_FOUND_ROWS p.id
, p.subject
, p.body
, p.date_posted
, p.date_updated
, u.name as author
, u.id as author_id
, u.signature as sig
, c.count as postcount
, p.forum_id as forum_id
, f.forum_moderator as 'mod'
, p.update_id
, u2.name as updated_by
FROM forum_forum f
JOIN forum_posts p
ON f.id = p.forum_id
JOIN forum_users u
ON u.id = p.author_id
LEFT
JOIN forum_users u2
ON u2.id = p.update_id
LEFT
JOIN forum_postcount c
ON u.id = c.user_id
WHERE $topicid IN (p.topic_id,p.id)
ORDER
BY p.topic_id
, p.date_posted
LIMIT $start,$limit;
";
$result = mysqli_query($sql, $conn)
or die(mysql_error() . "<br>" . $sql);
while ($row = mysqli_fecth_array($result) )
{
echo "<p>($body) . "</p>";
echo $sig;
}
Now after echo ($body) if I run this query;
$sql = "SELECT * FROM images WHERE name = '$name'";
$result = mysqli_query($sql) or die('Could not SELECT image data ' . mysql_error());
while ($therow = mysql_fetch_array($result))
{
$image_name = $therow["name"] ;
$ext = $therow["extension"];
}
?>
<img src="images/<?php echo $image_name.$ext; ?>" >
Help me, how do i get images to be displayed?
Try replace this:
while ($row = mysqli_fecth_array($result) )
{
echo "<p>($body) . "</p>";
echo $sig;
}
to
while ($row = mysqli_fetch_array($result) )
{
echo "<p>($body)" . "</p>";
echo $sig;
}
and this
$sql = "SELECT * FROM images WHERE name = '$name'";
$result = mysqli_query($sql) or die('Could not SELECT image data ' . mysql_error());
while ($therow = mysql_fetch_array($result))
{
$image_name = $therow["name"] ;
$ext = $therow["extension"];
}
?>
to
$sql = "SELECT * FROM images WHERE name = '$name'";
$result = mysqli_query($sql) or die('Could not SELECT image data ' . mysql_error());
while ($therow = mysqli_fetch_array($result))
{
$image_name = $therow["name"];
$ext = $therow["extension"];
}
?>
I'm trying to get the averages from the rating column of my table, then display a row in a HTML table for each distinct div_id and its average rating, in descending order. I know this should probably be easy, but I'm having a hard time figuring it out. Any help would be greatly appreciated.
<?php
mysql_connect($db_server, $db_username, $db_password) or die(mysql_error());
mysql_select_db($db_database) or die(mysql_error());
$result = mysql_query("SELECT * FROM ratings") or die(mysql_error());
while($row = mysql_fetch_object( $result )){
$ad = $row->div_id;
$result2 = mysql_query("SELECT div_id, avg(rating) AS avg, COUNT(*) FROM ratings WHERE div_id = '" . $ad . "' ORDER BY div_id DESC ") or die(mysql_error());
while($row2 = mysql_fetch_array( $result2 )){
$adid = $row2[0];
$count = $row2[2];
$avg = round($row2[1],2);
echo "<tr><td>";
echo $adid;
echo "</td><td>";
echo $avg;
echo "</td></tr>";
}
}
?>
Instead do tow queries, you can use the GROUP BY term in only one query.
SELECT div_id, avg(rating) AS avg, COUNT(*) FROM ratings GROUP BY div_id ORDER BY div_id DESC
php code
<?php
mysql_connect($db_server, $db_username, $db_password) or die(mysql_error());
mysql_select_db($db_database) or die(mysql_error());
$result = mysql_query("SELECT div_id, avg(rating) AS avg, COUNT(*) FROM ratings GROUP BY div_id ORDER BY avg DESC") or die(mysql_error());
while($row = mysql_fetch_array( $result )){
$adid = $row[0];
$count = $row[2];
$avg = round($row[1],2);
echo "<tr><td>" . $adid . "</td><td>" . $avg . "</td></tr>";
}
?>
Perhaps you should try doing this all in one query:
SELECT div_id, avg(rating) AS avgrating, COUNT(*) as cnt
FROM ratings
GROUP BY div_id
ORDER BY avgrating desc;