gpt4 book ai didi

php - 如何创建包含 MySQL 查询的 PHP 函数?

转载 作者:行者123 更新时间:2023-11-29 23:15:41 25 4
gpt4 key购买 nike

您好,目前我正在使用一个名为 select_Type.phpPHP 文件,其中包含一个 MySQL 查询来创建 <select></select> 框入 HTML。我正在使用include_once链接我的 admin.php./Includes/select_Type.php。我已经创建了一个名为 ./Includes/functions.php 的文件,其中所有查询都应在 PHP 函数 中设置。

我想了解有关函数的更多信息。

管理页面 admin.php 其中 <select></select>是:

<?php
session_start();
include_once './Includes/functions.php';

CheckLogIn('Superadmin');
SelectType();
//require_once './Includes/select_Type.php';
require_once './Includes/register.php';
?>

select_type.php:

    <?php
require_once 'functions.php';

$querySQL = "SELECT * FROM tbltype";
$queryResult = GetQuery($querySQL);

while ($row = mysqli_fetch_assoc($queryResult)){
$dataArrayType[] = $row;
}
?>

我尝试过的:

<?php
function SelectType() {
GLOBAL $_POST;
$querySQL = "SELECT * FROM tblType";
$queryResult=GetQuery($querySQL);

while ($row = mysqli_fetch_assoc($queryResult)) {
$dataArrayType[] = $row;
}
}
?>

我做错了什么?

提前致谢:)

最佳答案

谢谢大家,特别是似曾相识的感觉!!!!你们都非常有帮助。

问题是,你们大多数人是如何告诉我的:我没有返回该值。

之前:

<?php
function SelectType() {
GLOBAL $_POST;
$querySQL = "SELECT * FROM tblType";
$queryResult=GetQuery($querySQL);

while ($row = mysqli_fetch_assoc($queryResult)) {
$dataArrayType[] = $row;
}
}
?>

现在:

我删除了GLOBAL $_POST,因为$_POST已经是GLOBAL。(感谢ɴ ᴀ ᴛ ʜ)

<?php
function SelectType() {
$querySQL = "SELECT * FROM tblType";
$queryResult=GetQuery($querySQL);

while ($row = mysqli_fetch_assoc($queryResult)) {
$dataArrayType[] = $row;
}
return $dataArrayType;
}
?>

admin.php

我将函数 SelectType() 放在 foreach 中。瞧瞧!

    <select type="text" id="register_type" name="register_type" required>
<?php
foreach (SelectType() as $row) {
echo "<option value='" . $row['idType'] . "'>" . $row['dtType'] . '</option>';
}
?>
</select>

关于php - 如何创建包含 MySQL 查询的 PHP 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27838369/

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