从使用功能mysqli_fetch_assoc返回多个值 [英] Return multiple values from a function using mysqli_fetch_assoc
本文介绍了从使用功能mysqli_fetch_assoc返回多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从mysqli_fetch_assoc得到具体的值,并从该函数在下面的code回报他们?
具体来说,我试图同时获得$ CID和$ FNAME从db_select(返回),当按钮被点击提交。
函数db_query($ SQL){
$ CON = db_connect();
$结果= mysqli_query($ CON,$ SQL);
返回$结果;
} 功能db_select($ SQL){
$行=阵列();
$结果= db_query($的SQL);
如果(!$结果){
返回false;
}
而($行= mysqli_fetch_assoc($结果)){
$ CID = $行['C_ID'];
$ FNAME = $行['C_Fname'];
$行= $ CID;
}
返回$行;
} 如果(使用isset($ _ POST ['提交'])){
回声$行= db_select(SELECT C_ID,C_Fname从客户WHERE C_Email ='$电子邮件)
//怎么也返回$ FNAME?
}
解决方案
该db_select函数返回一个全阵列它看起来像,所以很有可能你是想获得像这样这两个变量:
函数db_select($ SQL){
$行=阵列();
$结果= db_query($的SQL);
如果(!$结果){
返回false;
} //这个函数可能返回多行...
而($阵列= mysqli_fetch_assoc($结果)){
//这将返回多行
$行[] = $阵列;
} //这将返回只有一行
//(但可能还有更多您缺少)
// $行= mysqli_fetch_assoc($结果) //注意这里的变化
返回$行;
}如果(使用isset($ _ POST ['提交'])){
$行= db_select(SELECT C_ID,C_Fname从客户WHERE C_Email ='$电子邮件) //如果你有一排选项,你回音,像这样
//回声$行['C_ID'];
//回声$行['C_Fname']; //对于多行选项,做一个foreach或者如果你知道密钥就可以直接访问它
的foreach($行作为$数组){
回声$阵列['C_ID'];
回声$阵列['C_Fname'];
}}
How do I get specific values from mysqli_fetch_assoc and return them from the function in the code below?
Specifically, I am trying to get both $CID and $Fname to return from db_select() when the submit button is clicked.
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?
}
解决方案
The db_select function returns a full array it looks like, so likely you are wanting to access those two variables like so:
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'];
}
}
这篇关于从使用功能mysqli_fetch_assoc返回多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文