如何从mysqli_fetch_assoc中获取特定值,并从下面的代码中的函数中返回它们?
具体来说,当单击提交按钮时,我试图同时从db_select()返回$ CID和$ Fname。
function db_query($sql){
$con = db_connect();
$result = mysqli_query($con,$sql);
return $result;
}
function db_select($sql){
$rows = array();
$result = db_query($sql);
if(!$result){
return false;
}
while($row = mysqli_fetch_assoc($result)){
$CID = $row['C_ID'];
$Fname = $row['C_Fname'];
$rows = $CID;
}
return $rows;
}
if (isset($_POST['submit'])){
echo $rows=db_select("SELECT C_ID,C_Fname FROM Customer WHERE C_Email='$Email'")
//how to also return $Fname?
}
db_select函数返回一个看起来像完整的数组,因此您很可能想要访问这两个变量,如下所示:
function db_select($sql){
$rows = array();
$result = db_query($sql);
if(!$result){
return false;
}
// This function could potentially return multiple rows...
while($array = mysqli_fetch_assoc($result)){
// This will return multiple rows
$row[] = $array;
}
// This will return only one row
// (but there may be more that you are missing)
// $row = mysqli_fetch_assoc($result)
// Notice change here
return $row;
}
if (isset($_POST['submit'])){
$rows = db_select("SELECT C_ID,C_Fname FROM Customer WHERE C_Email='$Email'")
// If you have the one row option you echo like so
// echo $rows['C_ID'];
// echo $rows['C_Fname'];
// For multiple row option, do a foreach or if you know the key you can access it directly
foreach($rows as $arrays) {
echo $arrays['C_ID'];
echo $arrays['C_Fname'];
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句