简体   繁体   English

在新的mysql查询中嵌入现有的php记录集

[英]embedding existing php recordset within new mysql query

I have an existing recordset that retrieves all the information from a table in mysql called $rrows. 我有一个现有的记录集,该记录集从mysql中名为$ rrows的表中检索所有信息。 What I am hoping to do is to use this existing recordset within a new mysql query. 我希望做的是在新的mysql查询中使用此现有记录集。

For example I have the following line that retrieves the "product code" from one table: 例如,我有以下一行从一个表中检索“产品代码”:

<?php echo $rrows['productcode']; ?>

I am trying to then gather the respective images from a new table using this productcode by something similar to: 我正在尝试使用类似于以下内容的产品代码从新表中收集各个图像:

<img src="<?php

mysql_select_db("dbname", $con);
mysql_set_charset('utf8');

$result = mysql_query("SELECT * FROM furnimages WHERE productcode='$rrows['productcode']'");

while($row = mysql_fetch_array($result))
{
    echo '' . $row['photo'] . '';   
}
mysql_close($con);
?>">

Can this be done? 能做到吗? Originally I was going to LINK tables together to get all the information, but this doesnt work as some of the product codes in the main do not have corresponding data in the 'furnimages' table..... 最初,我将一起链接到LINK表以获取所有信息,但是由于主要产品中的某些产品代码在'furnimages'表中没有相应的数据,因此无法正常工作.....

Thanks in advance! 提前致谢! JD 京东

sprintf() is your best friend here. sprintf()是您最好的朋友。

$sql = <<<sql
SELECT * FROM furnimages 
WHERE productcode=%d
sql;

$result = mysql_query(sprintf($sql, $rrows['productcode']));

So, %d is the placeholder in the string to swap in the second argument in the call to sprintf(); 因此,%d是字符串中的占位符,可在调用sprintf()的第二个参数中交换;

%d denotes an integer placeholder; %d表示整数占位符; if $rrows['productcode'] is a string, use %s. 如果$ rrows ['productcode']是字符串,请使用%s。

This is better than simply quoting value of the variable as it adds a type constraint which reduces the risk of nasty sql injection. 这比直接引用变量的值要好,因为它增加了类型约束,从而减少了讨厌的sql注入风险。

It also makes it eminently more readable. 它还使它更具可读性。

Check out the PHP Data Objects extension , though, because that really is the only way forward for this type of thing. 不过,请查看PHP Data Objects扩展 ,因为这确实是解决这类问题的唯一方法。

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

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