当我尝试从我的SQL查询中检索结果时,' php它没有告诉我它。
我的结果:
Array ( [0] => 0533 [SUBSTRING(cardnumber, 5, 4)] => 0533 )
虽然我只是想获得
的结果0533
显示。
我的php文件:
<?php
$con=mysqli_connect("localhost","root","wickedphat", "bluecard");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT SUBSTRING(cardnumber, 5, 4) FROM users WHERE username='".($_SESSION['username'])."'");
while($row = mysqli_fetch_array($result))
{
print_r($row) . " " ;
}
mysqli_close($con);
?>
在我的html文件中,我把它放在这里,它假设显示结果。
<?php include_once '../../includes/number1.php' ?>
答案 0 :(得分:1)
这些结果是正确的,并且与您提供的代码一起预期。有两点需要注意:
这应该证明这一点:
// Gave the result of SUBSTRING(cardnumber, 5, 4) the alias
// of "cardnum"
$result = mysqli_query($con,"SELECT SUBSTRING(cardnumber, 5, 4) as cardnum FROM users WHERE username='".($_SESSION['username'])."'");
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
// accessing the value of cardnum by refering to its key in the
// the array returned by mysqli_fetch_array()
echo $row['cardnum'] . ' ';
}
答案 1 :(得分:0)
如果你想要(奇怪的)关联数组键,我相信你想mysqli_fetch_assoc()
。