[英]Select * from table where colum = username not working
[英]sql select all from table where username in array
我试图从以前选择的SQL数据库中将一些CD名称打印到数组中,但是到处都可以得到相同的结果。 你能帮助我吗? 我试图在第二个php的代码中的不同位置添加[$ x],但得到相同的错误。
我只从用户名= loggein_user的数据库中获得第一个选择的CD名称。
require 'connectmysql.php';
//COUNT how many cart history has each user
mysql_select_db('shop');
$data = mysql_query("SELECT COUNT(Username) AS num FROM cart WHERE Username = '{$_SESSION['Username']}' ") or die(mysql_error());
$row2 = mysql_fetch_assoc($data);
$numUsers = $row2['num'];
$_SESSION['count_cart_history'] = $numUsers;
mysql_close($con);
问题在for循环下,但我找不到它。 我试图将其更改为$ result = ....或$ row3 = ....,而没有[$ x],但是什么也没有。
for($x=0;$x<=$_SESSION['count_cart_history']; $x++)
{
$selecthistory[$x] = "SELECT * FROM cart WHERE Username = '{$_SESSION['Username']}'";
$result[$x] = mysql_query($selecthistory[$x], $con);
$row3[$x] = mysql_fetch_assoc($result[$x]);
// $_SESSION['historymarket_id'][$x] = $row3[$x]["Market_ID"];
$_SESSION['historycd_name'][$x] = $row3[$x]["CD_NAME"];
// $_SESSION['historydate_sold'][$x] = $row3[$x]["Date_Sold"];
// $_SESSION['historyprice'][$x] = $row3[$x];["Price"];
// $_SESSION['historytotalprice'][$x] = $row3[$x]["TotalPrice"];
// $_SESSION['historyquantity'][$x] = $row3[$x]["Quantity"];
// $_SESSION['historycredit'][$x] = $row3[$x]["Credit"];
}
echo'edw';
echo $_SESSION['historycd_name'][0];
echo $_SESSION['historycd_name'][1];
echo $_SESSION['historycd_name'][2];
for($x=0;$x<=$_SESSION['count_cart_history']; $x++)
{
?>
<p>
Market_ID :
<input name="market_id" type="text" readonly value=" <?php //echo $_SESSION['historymarket_id'][$x]; ?>">
</p>
<p>
CD Name :
<input name="CD_NAME" type="text" readonly value="<?php echo $_SESSION['historycd_name'][$x]; ?>">
</p>
<p>
Date Sold :
<input name="Date_Sold" type="text" readonly value="<?php //echo $_SESSION['historydate_sold'][$x]; ?>">
</p>
<p>
Price:
<input name="price" type="text" readonly value="<?php //echo $_SESSION['historyprice'][$x]; ?>">
</p>
<p>
Total Price:
<input name="totalprice" type="text" readonly value="<?php //echo $_SESSION['historytotalprice'][$x]; ?>">
</p>
<p>Credit Card:
<input name="Credit" type="text" readonly value="<?php //echo $_SESSION['historycredit'][$x]; ?>">
</p>
<p>Quantity:
<input name="quantity" type="text" readonly value="<?php //echo $_SESSION['historyquantity'][$x]; ?>">
</p>
<p>PostAddress:
<input name="PostAddress" type="text" readonly value="">
</p>
<?php } mysql_close($con);?>
您在每个循环上都执行相同的SELECT
查询,因此您存储的所有项目都将相同。
您可以尝试这样的事情:
$x = 0;
$selecthistory = "SELECT * FROM cart WHERE Username = '{$_SESSION['Username']}'";
$result = mysql_query($selecthistory[$x], $con);
// loop through all the items from the cart
// this will loop $_SESSION['count_cart_history'] times as long as the count hasn't changed
while($result && $row3 = mysql_fetch_assoc($result)){
// $_SESSION['historymarket_id'][$x] = $row3["Market_ID"];
$_SESSION['historycd_name'][$x] = $row3["CD_NAME"];
// $_SESSION['historydate_sold'][$x] = $row3["Date_Sold"];
// $_SESSION['historyprice'][$x] = $row3["Price"];
// $_SESSION['historytotalprice'][$x] = $row3["TotalPrice"];
// $_SESSION['historyquantity'][$x] = $row3["Quantity"];
// $_SESSION['historycredit'][$x] = $row3["Credit"];
$x++;
}
建议您不要使用mysql_
函数。 而是升级到mysqli_
函数。 您还应该在查询中使用$_SESSION['Username']
之前将其转义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.