![](/img/trans.png)
[英]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.