Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用变量选择列_Mysql_Sql_Database_Relational Database - Fatal编程技术网

Mysql 使用变量选择列

Mysql 使用变量选择列,mysql,sql,database,relational-database,Mysql,Sql,Database,Relational Database,也许这不是一个标题的最佳描述,但我真的想不出任何东西。 我想做的是,我可以创建一个查询,使用userID作为关系从列表中选择类型,然后选择城镇 TABLE listings saleID userID type description 1 23 clothing nice clothing 2 45 clothing More nice Clothing TABLE users userID country

也许这不是一个标题的最佳描述,但我真的想不出任何东西。 我想做的是,我可以创建一个查询,使用userID作为关系从列表中选择类型,然后选择城镇

TABLE listings
saleID userID   type          description
1      23     clothing       nice clothing
2      45     clothing     More nice Clothing

TABLE users
userID    country      county         town
23         uk          county       townname1
24         uk          county       townname2
这些变量在url中设置为get-eg)?type=clothing&town=townname2

if (!isset($type)){
$query = "SELECT * FROM listings";
}
if (isset($type)) {
$query = "SELECT * FROM listings WHERE type = '{$type}'";
}
这是我一直坚持的一点,我想获得所有带有变量$type“town”的列表,然后从用户中选择带有变量$town的town

if (isset($town)) {
   $query = "SELECT users.town listings.userID listings.type FROM listings 
   WHERE type = '{$type}' 
   INNER JOIN users 
   ON users.town = '{$town}'";       
}
希望我已经解释清楚了,能够理解。 请帮我回答最后一个问题

SELECT u.town l.userID, l.type 
FROM listings l
INNER JOIN users u on u.userID = l.userID   
WHERE l.type = '{$type}' 
AND u.town = '{$town}'
要进一步了解有关联接的更多信息,请访问以下链接:


作为旁注,如果变量的值来自外部,则查询易受攻击。请看下面的文章,了解如何预防它。通过使用
PreparedStatements
可以避免在值周围使用单引号

要进一步了解有关联接的更多信息,请访问以下链接:


作为旁注,如果变量的值来自外部,则查询易受攻击。请看下面的文章,了解如何预防它。通过使用
PreparedStatements
可以避免在值周围使用单引号


还要验证SQL注入的输入值,看看还要验证SQL注入的输入值,看看感谢您的回复。我假设“a”作为列表的变量?“来自清单a”感谢您的回复。我假设“a”作为列表的变量?“来自清单a”$results=mysql\u query($query);虽然($listing=mysql\u fetch\u assoc($results)){很抱歉,我一直按enter键,我如何访问表listings中的描述?我会使用$listings['l.description']因为它似乎找不到它?找到了。需要它是全部,而不是只指定特定的列。将l.userID、l.type更改为l.*再次感谢解决方案$results=mysql\u query($query);while($listing=mysql\u fetch\u assoc($results)){抱歉,我一直按enter键,如何访问表列表中的描述?我会使用$listings['l.description']吗,因为它似乎找不到它?找到了它。需要全部,而不仅仅是指定特定列。将l.userID、l.type更改为l.*再次感谢您的解决方案
SELECT  b.town, a.userID, a.type 
FROM    listings a
        INNER JOIN users b 
            ON  a.userID  = b.userID    
WHERE   a.type = '{$type}' AND
        b.town = '{$town}'