调用函数后,无法使用php读取mysql数据库中使用php编写的值

调用函数后,无法使用php读取mysql数据库中使用php编写的值,php,mysql,Php,Mysql,在php函数中,我尝试在数据库表中插入一行作为 function abnc() { $link = conn to db; $query = "insert into table( a,c ,v) values (1,2,3);" $result = mysqli_query($link, $query); if(mysqli_rows_affected($link) == 1){ close conn; return true;} else{ return false; close conn

在php函数中,我尝试在数据库表中插入一行作为

function abnc()
{

$link = conn to db;
$query = "insert into table( a,c ,v) values (1,2,3);"
$result = mysqli_query($link, $query);
if(mysqli_rows_affected($link) == 1){
close conn;
return true;}
else{
return false;
close conn;
}
现在,在另一个地方,我调用了这个函数,并试图读取我插入的值 作为

我得到的输出是真的0

我认为当函数执行时,脚本只是继续。 我希望它等到函数执行完成。
任何解决方案???

脚本不会继续。调用abnc()时,将执行该函数并返回一个值,该值存储在变量$done中。这个值可能是真的,因为您的输出是真的0

在abnc()中插入一行。这意味着一行受到影响,函数返回true。然后关闭数据库连接,这可能就是以后无法访问插入数据的原因。

试试这个

$done = abnc();
$link = conn to db;

if ($done) {
    $query = "select * from table where a=1 and c=2 and v=3";
    $result = mysqli_query($link, $query);
    if (mysqli_num_rows($result) > 0) {
        do {
            $resultSet = array();
            if (($row = mysqli_store_result($link))) {
                while ($row = mysqli_fetch_assoc($row)) {
                    $resultSet[] = $row;
                }
                $return[] = $resultSet;
                @mysqli_free_result($row);
            }
        } while (@mysqli_next_result($link));
        return $return;
    }
} else {
    return false;
}

您在函数中关闭了数据库连接,请尝试在函数外部打开它,然后在脚本末尾关闭。

但为什么它不能读取值,我刚刚插入了??如果我只是使用phpmyadmin执行相同的查询,我可以看到它返回一个row@AmitMittal如果在abnc()中关闭连接,我看不到您再次连接到数据库?实际上,该函数位于另一个文件functions.php中,我已将其包含在该文件中。在这个文件中,我打开了一个到数据库的连接
$done = abnc();
$link = conn to db;

if ($done) {
    $query = "select * from table where a=1 and c=2 and v=3";
    $result = mysqli_query($link, $query);
    if (mysqli_num_rows($result) > 0) {
        do {
            $resultSet = array();
            if (($row = mysqli_store_result($link))) {
                while ($row = mysqli_fetch_assoc($row)) {
                    $resultSet[] = $row;
                }
                $return[] = $resultSet;
                @mysqli_free_result($row);
            }
        } while (@mysqli_next_result($link));
        return $return;
    }
} else {
    return false;
}