在下面的脚本中,我想使用explode函数从mysql中获取数据,还需要在explode函数中获取变量。 这就是我想要的方式
<?php
include ('config.php');
$track = "1,2,3";
$i = 1
$trackcount = explode(",",$track);
$sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";
$retval = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "{$row['track_name']}";
}
mysql_free_result($retval);
?>
这是代码
$sql = "SELECT * FROM tracks WHERE id='$trackcount[$i]'";
我希望sql从其中id = $trackcount[$i]
的曲目表中获取数据
无论$trackcount[$i]
mysql应该获取什么值,但它显示一个空白屏幕。
如果我把这个
$sql = "SELECT * FROM tracks WHERE id='$trackcount[1]'";
完美无缺
答案 0 :(得分:1)
将$ trackcount [$ i]保存在一个变量中,然后将其传递给查询,如下所示
<?php
include ('config.php');
$track = "1,2,3";
$i = 1;
$trackcount = explode(",",$track);
$id=$trackcount[$i];
$sql = "SELECT * FROM tracks WHERE id='$id'";
$retval = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "{$row['track_name']}";
}
mysql_free_result($retval);
?>
还有一件事是用你的查询回显检查你以前的代码,看看传递的是什么。
echo $sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";//like this
问题在于您的查询
$sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";//change
到
$sql = "SELECT * FROM tracks WHERE id='$trackcount[$i]'";
答案 1 :(得分:0)
一般情况下,您希望IN
运算符使用此类查询,因此对您而言,这将是: -
$sql="SELECT * FROM `tracks` WHERE `id` in (".$track.");";
或者,如果$ ids在一个数组中,
$sql="SELECT * FROM `tracks` WHERE `id` in (".implode( ',', $array ) .");";