当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Using Where and Join together

问题描述:

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>
网友答案:

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";
网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: