简体   繁体   English

在一个mysql查询中查询两个表

[英]query two tables in one mysql query

im having trouble getting data from two seperate tables 我在从两个单独的表中获取数据时遇到麻烦

so far i have this 到目前为止,我有这个

<? 
include('config.php'); 
$xid = $_GET['xid'];

$result = mysql_query("SELECT * FROM `config`") or trigger_error(mysql_error()); 
while($row = mysql_fetch_array($result)){ 
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); } 

$result = mysql_query("SELECT * FROM `utinfo` WHERE `xid` = $xid") or trigger_error(mysql_error()); 
while($row2 = mysql_fetch_array($result)){ 
foreach($row2 AS $key => $value) { $row2[$key] = stripslashes($value); } 
$un = urldecode($row2['un']);
};

switch ($row['module'])
{
case 1:
  echo "Function 1 for user $uid on account $un";
  break;
case 2:
  echo "Function 2 for user $uid on account $un";
  break;
case 3:
  echo "Function 3 for user $uid on account $un";
  break;
default:
  echo "No module defined.";

};
};
?>

The config table config has the row named modules, and its populated by 2 entries, one of which is 1, the other 3. So i should be seeing case 1 and then case 3. But all im getting is the default echo. config表config有一个名为modules的行,它由2个条目填充,其中一个是1,另一个是3。因此,我应该看到情况1,然后是情况3。但是,所有即时消息都是默认回显。

(This is not an answer to the OP, but something you really should care about, so I think it's worth writting it) (这不是OP的答案,而是您真正应该关心的事情,因此我认为值得编写)

It seems there is a enormous SQL-injection in your code. 似乎您的代码中有大量的SQL注入。

The normal way of calling your page would be with something like " xid=5 " in the URL, to get informations of user #5. 调用页面的常规方法是在URL中使用“ xid=5 ”之类的内容,以获取用户#5的信息。

Now, suppose someone give " xid=5 or 1=1 ". 现在,假设有人给出“ xid=5 or 1=1 ”。 The resulting query would be : 结果查询为:

SELECT * FROM `utinfo` WHERE `xid` = 5 or 1=1

The condition is always true ; 条件总是正确的; you'd get informations of ALL users as an output, as you iterate through the resultset. 遍历结果集时,您将获得所有用户的信息作为输出。

Another possibility : " xid=5; delete from utinfo; " ; 另一种可能性:“ xid=5; delete from utinfo; which would give this query : 这将给这个查询:

SELECT * FROM `utinfo` WHERE `xid` = 5; delete from utinfo;

That would empty your table :-( 那将清空您的表:-(


You must always escape / check / sanitize / whatever you data before putting them in a SQL query, especially (but not only) if they come from a user of the application. 在将数据放入SQL查询之前,必须始终对其进行转义/检查/清理/任何数据,特别是(但不仅限于)如果它们来自应用程序用户。

For strings, see the mysql_real_escape_string function. 有关字符串,请参见mysql_real_escape_string函数。
For data that sould be integers, you could use intval (worst case, if data was not valid, you'll get 0, which might get no result from the DB, but, at least, won't break it ^^ ) 对于可以为整数的数据,可以使用intval (最坏的情况是,如果数据无效,则将得到0,这可能不会从数据库中得到结果,但至少不会破坏它^^)

Another solution would be to use prepared statements ; 另一种解决方案是使用准备好的语句; but those are not available with mysql_* function : you have to switch to either 但是那些不适用于mysql_*函数:您必须切换到任一

Anyway, for a new application, you shouldn't use mysql_* : it is old, and doesn't get new functionnalities / improvements that mysqli and PDO get... 无论如何,对于新的应用程序,您不应该使用mysql_* :它是旧的,并且不会获得mysqli和PDO所获得的新功能/改进...

stripslashes() is used on strings. stripslashes()用于字符串。 Your case values are integers. 您的案例值是整数。 It seems like you have a type mismatch here? 看来您这里的类型不匹配?

  1. Why are you not using PDO? 为什么不使用PDO? You should really standardis on PDO if you can. 如果可以的话,您应该真正在PDO上进行标准化。
  2. Table names in SQL select should not be quoted. SQL select中的表名不应加引号。
  3. You should consider using prepared statements in order to avoid SQL Injection and then you don't have to worry about having to quote your paramaters 您应该考虑使用准备好的语句以避免SQL注入,然后您不必担心必须引用参数

The first answer is probably correct regarding type mismatches, you should be able to fix the issue by using the following code: 对于类型不匹配,第一个答案可能是正确的,您应该可以使用以下代码解决问题:

switch ((integer) $row['module'])

See the following: http://us.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting 参见以下内容: http : //us.php.net/manual/zh/language.types.type-juggling.php#language.types.typecasting

Alternatively, you could try this: 或者,您可以尝试以下操作:

settype($row['module'], "integer");

switch ($row['module'])

See: http://us.php.net/manual/en/function.settype.php 请参阅: http//us.php.net/manual/en/function.settype.php

I would also suggest echo'ing the value of $row['module'] onto the page just to check that it is indeed an integer. 我还建议将$ row ['module']的值回显到页面上只是为了检查它是否确实是整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM