[英]sql select all from table where username in array
I am trying to print some cd names in to an array from a SQL database which previously I had selected but everywhere I get the same results. 我试图从以前选择的SQL数据库中将一些CD名称打印到数组中,但是到处都可以得到相同的结果。 Can you help me? 你能帮助我吗? I was trying to add [$x] in different locations in code on the second php but get the same error. 我试图在第二个php的代码中的不同位置添加[$ x],但得到相同的错误。
I only get the first selected cd name from database where username= loggein_user. 我只从用户名= 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);
The Problem is somewhere under the for loop but I can't find it. 问题在for循环下,但我找不到它。 I was trying to change it like $result = .... or $row3 = .... without [$x] but nothing. 我试图将其更改为$ 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);?>
You are doing the same SELECT
query on every loop, so all the items you store will be the same. 您在每个循环上都执行相同的SELECT
查询,因此您存储的所有项目都将相同。
You could try something like this: 您可以尝试这样的事情:
$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++;
}
It is recommended that you do not use mysql_
functions; 建议您不要使用mysql_
函数。 instead upgrade to mysqli_
functions. 而是升级到mysqli_
函数。 You should also escape your $_SESSION['Username']
before using it in the query. 您还应该在查询中使用$_SESSION['Username']
之前将其转义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.