gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:34:07 25 4
gpt4 key购买 nike

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

complain.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

dbconfig.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变量作为参数传递,并且函数需要返回值

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

关于php - 无法使用 PHP 和 MySQL 执行 MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35033304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com