无法使用 PHP 和 MySQL 执行 MySQL 查询


Can not execute the MySQL query using PHP and MySQL

我在执行sql查询时遇到问题。我在下面解释我的代码。

投诉.php:

require_once("./include/dbconfig.php");
if($_REQUEST['typeOfApp']=="WEB"){
        $sent_form="Web";
        $ticket_id=generateTicketId('W1');
    }
function generateTicketId($code){
        $sql="select * from db_ticket order by id desc ";
        $qrylast=mysqli_query($con,$sql);
        echo mysqli_num_rows($qrylast);exit;
}

在这里,我正在尝试检查行数。但我什么也没得到。我在下面给出我dbconfig.php

数据库配置.php

$con = new mysqli("localhost", "root", "****", "************"); 
if ($con->connect_error)die("Connection failed: "); 

在这里,我需要检查表中存在的行数。请帮助我。

您需要全局定义连接变量,以便必须在函数内部访问,或者需要在函数参数中传递它

require_once("./include/dbconfig.php");
//global $con;
if($_REQUEST['typeOfApp']=="WEB"){
        $sent_form="Web";
        $ticket_id=generateTicketId('W1',$con);// pass connection variable
    }

$con变量作为参数和函数需要传递return value

function generateTicketId($code, $con) {
    $sql = "select * from db_ticket order by id desc ";
    $qrylast = mysqli_query($con, $sql);
    $rows = mysqli_num_rows($qrylast);
    if($rows>0){
        return $rows;
    }else{
        return FALSE
    }
}

http://php.net/manual/en/language.variables.scope.php