So I have two tables, one storing the username and the other with their personal info. What I'm trying to get is a result where, I want to take the current active username and join it with the personal info table to get the ID, Username + their basic info to appear in a table. I came up with the query after googling, but it seems that I did something wrong. Can someone tell me what I did wrong?
users = the table that contain username
userinfo table with their basic info
$username is basically a variable that was stored in the $SESSION
<?php
$query = mysql_query("
SELECT users.UserID, users.Username, userinfo.FirstName, userinfo.LastName,
FROM users
WHERE users.Username = $username
INNER JOIN userinfo
ON users.UserID=userinfo.UserID");
?>
Assuming I have the parse data to table coding right below that specific code, what is wrong with the query?
Basically the error I keep getting is error #1064 at line #2 (when I run the query without the php code). Thanks in advance!
<?php
$query = mysql_query("
SELECT users.UserID, users.Username, userinfo.FirstName, userinfo.LastName
FROM users
INNER JOIN userinfo
ON users.UserID=userinfo.UserID
WHERE users.Username = $username
");
?>
Try this
<?php
$query = mysql_query("
SELECT users.UserID, users.Username, userinfo.FirstName, userinfo.LastName,
FROM users
INNER JOIN userinfo
ON users.UserID=userinfo.UserID
WHERE users.Username = $username");
?>
The syntax is -
SELECT col, ...
FROM <tbl_name> AS t1
INNER JOIN <join_tbl_name> AS t2
ON t1.col = t2.col
WHERE <cond>
See the below code. If you print it, you will find where error occurs.
echo $query = mysql_query("SELECT users.UserID, users.Username, userinfo.FirstName, userinfo.LastName FROM users WHERE users.Username = '$username' INNER JOIN userinfo ON users.UserID=userinfo.UserID");
or use
$sql="SELECT users.UserID, users.Username, userinfo.FirstName, userinfo.LastName FROM users WHERE users.Username = '$username' INNER JOIN userinfo ON users.UserID=userinfo.UserID";
if(!mysql_query($sql,$con))
{
echo "Unexpected Error <br/>";;
die (mysql_error());
}
where $con is mysql connection statement.
You have an extra comma at the end of field select. Try removing that part:
$query = "SELECT users.UserID, users.Username, userinfo.FirstName, userinfo.LastName
FROM users
INNER JOIN userinfo
ON users.UserID=userinfo.UserID
WHERE users.Username = $username";
Related
I have a simple blog where I'm practicing some php and mysql. I'm trying to display the username of the post author (you know where it says posted by Author).
I have two tables blog_members and blog_posts which are related 1 to many so I got a memberID field into the blog_posts. I'm trying to get the username of the member who's the author of the post.
I was thinking to do a join or something but I can't figure this out.
Here's what I was trying to do but it's not working because I'm sure I'm not using it properly.
$query1 = "SELECT username from blog_members JOIN blog_posts ON memberID = memberID ";
$result1 = mysqli_query($link, $query1);
$row1 = mysqli_fetch_array($result1);
PS: I got it working one way by using SESSION to get the userID but that works only if the user is logged is which is not the case, I want to display the name in any case.
Thanks!
Use inner join this way
And with a proper sanitize use $your_user_id for match
$query1 = "SELECT username
from blog_members
INNER JOIN blog_posts ON blog_members.memberID = blog_posts.memberID
WHERE blog_posts.memberID = '" .$your_user_id . "';";
JOIN syntax is wrong in your query.
Use following query:
$query1 = "SELECT username from blog_members JOIN blog_posts ON blog_members.memberID = blog_posts.memberID ";
$result1 = mysqli_query($link, $query1);
$row1 = mysqli_fetch_array($result1);
Try something like this, usng INNER JOIN :
$query1 = "SELECT blog_members.username FROM blog_members INNER JOIN blog_posts ON blog_members.memberID = blog_posts.memberID ";
reference : http://www.w3schools.com/sql/sql_join.asp
Here is a solution using a simple WHERE condition (same performance Explicit vs implicit SQL joins) :
SELECT a.username FROM blog_members a, blog_posts b WHERE a.memberID = b.memberID
But if you need more information about MySQL Join : https://www.sitepoint.com/understanding-sql-joins-mysql-database/
Hope this helps !
I wrote a simple program that has two tables, one user and the other update_acc where the users table contains the registration details like username, name, last name, bank, account number of user while the update_acc contains users username, perfect money name, perfect money no. I have able to create the join but I want to output it one into a table with each information to its field then it shouldn't bring out every information to the user but just his own information, thanks.
` <?php
require_once 'core/init.php';
error_reporting(0);
require 'db/connect.php';
$sql = "
SELECT
users.username, users.name, users.last_name, users.bank, update_acc.perfect_money, update_acc.pm_no as update_acc
FROM users
JOIN update_acc ON users.username = update_acc.username
";
$results = $db->query($sql);
if($results->num_rows) {
while($row = $results->fetch_object()) {
echo "{$row->username} {$row->name} {$row->last_name} {$row->bank} {$row->update_acc}<br>";
}
}else{
echo 'No results.';
}
`
I guess what you are looking for is a where clause.
$sql = "
SELECT
users.username, users.name, users.last_name, users.bank, update_acc.perfect_money, update_acc.pm_no as update_acc
FROM users
JOIN update_acc ON users.username = update_acc.username
where users.username='adabebi' ";
this will show records for only username adabebi
This is my sql query
$sql = "SELECT comments.comment, users.userid, comments.bpid
FROM comments, users ,blogpages
WHERE comments.user_id = users.user and comments.bpid ='".$blogid."'
ORDER BY comments.cid;"
$query = mysqli_query($con,$sql) or die (mysqli_error($con));
while($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
$comm =$row["comment"];
$commentsuser =$row["userid"];
$commentbody .= "".$commentsuser."".$comm." ";
}
This is my form and display
<form action="" method="POST">
<textarea name="commentarea"></textarea>
<input type="submit" value="COMMENT" >
<h2><?php echo $commentbody; ?></h2>
</form>
but after i enter comment each value is displaying multiple times ?
the comment doesn't show up until i re-enter the page
You are missing a join condition:
SELECT comments.comment, users.userid, comments.bpid
FROM comments, users ,blogpages
WHERE comments.user_id = users.user AND
comments.bpid = blogpages.id AND -- Missing in the OP
comments.bpid ='".$blogid."'
ORDER BY comments.cid;
Having said that, implicit joins are deprecated, and you should probably be using an explicit join:
SELECT comments.comment, users.userid, comments.bpid
FROM comments
JOIN users ON comments.user_id = users.user
JOIN blogpages ON comments.bpid = blogpages.id
WHERE comments.bpid ='".$blogid."'
ORDER BY comments.cid;
This will also help visualize the join condition you were missing, since in this form its an actual part of the syntax.
I think the sql you are looking for is something like..
SELECT
comments.comment,
users.userid,
comments.bpid
FROM
comments,
INNER JOIN users ON comments.userId = users.userid,
INNER JOIN blogpages ON comments.bpid = blogpages.bpid
WHERE
comments.bpid ='".$blogid."'
ORDER BY
comments.cid;
Im making a blog like system using HTML, CSS, PHP and MySQl.
The site is made up of three tables.
user (id, username, password, email)
posts (postid, title, post)
comments (postid, id, comment, commentid) postid coming from posts and id from user.
I am trying to display all of the comments and the users username who left them for a certain post.
When i use this query in phpmyadmin:
SELECT user.username, comments.comment FROM user INNER JOIN comments on user.id=comments.id where postid=1
It shows what i need.
When i add it into php i get a blank page.
<?php
//echo "1";
session_start();
$connect = mysql_connect('localhost', 'root', 'root') or die("Couldn't connect");
mysql_select_db("com541blog") or die("Couldn't connect to database");
//echo "2";
//$postid = $_GET['type'];
$_SESSION['postid'] = $postid;
//echo "3";
$query_comments = mysql_query("SELECT user.username as username, comments.comment as comment FROM user INNER JOIN comments on user.id=comments.id WHERE postid='1'");
$info = mysql_fetch_array($query_comments);
$username = $info['username'];
$comment = $info['comment'];
echo $username;
echo $comment;
?>
Thanks in advance for the help :)
You're not executing any query.
$rs = mysql_query($query_comments);
$info = mysql_fetch_array($rs);
Your first line has an error I suspect, ie missing 'c' near the end of 'connect'.
include("db_connet.php"); should be include("db_connect.php");
Also, missing a semi-colon ;. This:
$query_comments = ("SELECT user.username, comments.comment
FROM user INNER JOIN comments on user.id=comments.id
where postid=1")
Should read:
$query_comments = ("SELECT user.username, comments.comment
FROM user INNER JOIN comments on user.id=comments.id
where postid=1");
Also, not bad practice to qualify each of your column names with a table name eg user.username as you're doing. But you might prefer eg the following more concise syntax using table aliases:
$query_comments = ("SELECT u.username, c.comment
FROM user u INNER JOIN comments c on u.id = c.id
where c.postid = 1");
(Note the table aliases don't need to be a single letter, so can be handy reducing a table name such as "ManufacturerSuppliedPartsListData_Feb01", to eg "mpl", without losing their meaning. Or eg if you've got "Customers" and "Credit" instead of just "c" you might use eg "cust" and "cred")
You need to specify mysql_query in PHP ... Else your query will not be executed
Like :
$query_comments = mysql_query("SELECT user.username, comments.comment FROM user INNER JOIN comments on user.id=comments.id where where postid=1");
i have this piece of code that selects all users from the table and another sql statement that counts the number of records for every user. the problem I'm facing is that i have a sql in a foreach loop which is not good for performance but i wasn't able to combine both of them in one statement. any advise?
$query = $db->getAll("SELECT * FROM users");
foreach($query as $v){
$tpl->setCurrentBlock('useri');
$query2 = $db->numRows("SELECT * FROM signups AS s INNER JOIN users AS u ON s.userid=u.id WHERE u.id={$v['id']}");
$tpl->setVariable('total',$query2);
$tpl->setVariable($v);
$tpl->parseCurrentBlock();
}
Try this query against your DB:
SELECT u.id, COUNT(s.*)
FROM users u
LEFT JOIN signups s ON s.userid = u.id
GROUP BY u.id
I hope I got it right. I have no SQL DB to test it right here. Important: You have to group by each field you select that is no aggregate.
Edit:
If it is not fast enough yet, an index on signups.userid may help. This is hypothetic, however, so you should check the Execution Plan your query engine generates.
$query = $db->getAll("
SELECT u.id, u.name, COUNT(*) total
FROM signups AS s RIGHT JOIN users AS u ON s.userid=u.id
GROUP BY u.id, u.name
ORDER BY u.name
");
foreach($query as $v){
$tpl->setCurrentBlock('useri');
$tpl->setVariable('total', $query['total']);
// ...
$tpl->parseCurrentBlock();
}
If I understand good your problem, initialize variable and in loop increase it.
int i;
foreach($query as $v){
$tpl->setCurrentBlock('useri');
$query2 = $db->numRows("SELECT * FROM signups AS s INNER JOIN users AS u ON s.userid=u.id WHERE u.id={$v['id']}");
$tpl->setVariable('total',$query2);
$tpl->setVariable($v);
$tpl->parseCurrentBlock();
i++;
}