简体   繁体   English

html表在SQL查询后返回空

[英]Html table returns empty after SQL query

I'm having problems with SQL query that returns no results instead of the data from the tables. 我在使用SQL查询时遇到问题,该查询不返回任何结果,而不返回表中的数据。 I have two tables on my DB, one is for Products and the other is Basket. 我的数据库上有两个表,一个用于产品,另一个用于购物篮。 The idea is to take the product id's from the basket and retrieve all the rest of the data from the product table. 这个想法是从购物篮中取出产品ID,然后从产品表中检索所有其余数据。 This is what i did: 这就是我所做的:

$sql = sprintf("SELECT * FROM Basket");

$result = mysql_query($sql, $link);

while ($row = mysql_fetch_array($result)) {

$my_id = $row["Id"];

$prod_s=sprintf("SELECT * FROM Products WHERE Id='%s'",$my_id) ;
$prod= mysql_fetch_array($prod_s);

echo "<td>" . htmlentities($prod["Name"],ENT_QUOTES,"UTF-8") . "</td>";
echo "<td>" . htmlentities($prod["Size"]) . "</td>";

. . . The table is being created but all fields are empty. 正在创建表,但所有字段均为空。

Thank you! 谢谢!

First of all, your current code is vulnerable to second-level SQL injections: if one of the IDs in the database is a malicious string (eg the good old ; DROP DATABASE foo ), you're screwed. 首先,您当前的代码容易受到第二级SQL注入的攻击:如果数据库中的ID之一是恶意字符串(例如,旧的旧代码; DROP DATABASE foo ),那么您就被搞砸了。

Now, your actual problem is that you're not actually sending the second query to the SQL server. 现在,您的实际问题是您实际上没有将第二个查询发送到SQL Server。 You'll want to run mysql_query() on it and use the result handle with mysql_fetch_array . 您将要在其上运行mysql_query()并将结果句柄与mysql_fetch_array You're already doing it correctly with the initial query. 您已经使用初始查询正确执行了此操作。 Just do the same thing again. 再次做同样的事情。

Finally, you might want to know that all of this can be done in a single SQL query, using joins. 最后,您可能想知道所有这些操作都可以使用联接在单个SQL查询中完成。 You may want to ask your favourite search engine about those. 您可能需要询问您最喜欢的搜索引擎。 Good luck! 祝好运!

I think you still have to add a mysql_query for prod_s. 我认为您仍然必须为prod_s添加mysql_query。

    $my_id = $row["Id"];

    $prod_s=sprintf("SELECT * FROM Products WHERE Id='%s'",$my_id) ;
    $prod_q=mysql_query($prod_s);
    $prod= mysql_fetch_array($prod_q);

    echo "<td>" . htmlentities($prod["Name"],ENT_QUOTES,"UTF-8") . "</td>";
    echo "<td>" . htmlentities($prod["Size"]) . "</td>";

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

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