[英]How to create a PHP function that contain a MySQL query?
Hello at the moment I'm using a PHP file called select_Type.php that contain a MySQL query to create a <select></select>
Box into HTML. 您好,目前,我正在使用一个名为select_Type.php的PHP文件,该文件包含一个MySQL查询,以在HTML中创建一个<select></select>
Box。 I'm using include_once
to link my admin.php and ./Includes/select_Type.php . 我正在使用include_once
链接我的admin.php和./Includes/select_Type.php 。 I already created a file called ./Includes/functions.php where all my querys should be set in PHP functions . 我已经创建了一个名为./Includes/functions.php的文件,所有查询都应在PHP函数中进行设置。
I would like to learn more about functions. 我想了解更多有关功能的信息。
Admin page admin.php where the <select></select>
is : 管理员页面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 : select_type.php:
<?php
require_once 'functions.php';
$querySQL = "SELECT * FROM tbltype";
$queryResult = GetQuery($querySQL);
while ($row = mysqli_fetch_assoc($queryResult)){
$dataArrayType[] = $row;
}
?>
What I tried: 我试过的
<?php
function SelectType() {
GLOBAL $_POST;
$querySQL = "SELECT * FROM tblType";
$queryResult=GetQuery($querySQL);
while ($row = mysqli_fetch_assoc($queryResult)) {
$dataArrayType[] = $row;
}
}
?>
What am I doing wrong ? 我究竟做错了什么 ?
Thanks in advance :) 提前致谢 :)
You can use this: 您可以使用此:
function sel($table,$field="*", $condition="1",$sort="" ){
if($sort!='') $sort="order by $sort ";
//echo "select $field from $table where $condition $sort ";
$sel_query=mysql_query("select $field from $table where $condition $sort ");
//$sel_result=array();
while($temp_res=@mysql_fetch_array($sel_query))
{
$sel_result[]=$temp_res;
}
return isset($sel_result)?$sel_result: 0;
}
And get result: 并得到结果:
$temp_res=sel("post","*"," userid ='".$frnd['friend']."' ORDER BY id DESC");
if($temp_res)foreach($temp_res as $row){
echo $row['content'];
}
Thank you all especially Déjà vu !!!!! 谢谢大家,尤其是Déjàvu !!!!! You were all very helpfull. 你们都很有帮助。
The problem was, how most of you told me: I didn't returned the value. 问题是,你们大多数人如何告诉我:我没有退还这笔钱。
<?php
function SelectType() {
GLOBAL $_POST;
$querySQL = "SELECT * FROM tblType";
$queryResult=GetQuery($querySQL);
while ($row = mysqli_fetch_assoc($queryResult)) {
$dataArrayType[] = $row;
}
}
?>
I deleted the GLOBAL $_POST
becauce $_POST
is already GLOBAL
.(Thanks to ɴ ᴀ ᴛ ʜ) 我删除了GLOBAL $_POST
因为$_POST
已经是GLOBAL
。(感谢ɴ)
<?php
function SelectType() {
$querySQL = "SELECT * FROM tblType";
$queryResult=GetQuery($querySQL);
while ($row = mysqli_fetch_assoc($queryResult)) {
$dataArrayType[] = $row;
}
return $dataArrayType;
}
?>
I put my function SelectType()
in my foreach
. 我把我的函数SelectType()
放在了我的foreach
。 Et voila! 瞧!
<select type="text" id="register_type" name="register_type" required>
<?php
foreach (SelectType() as $row) {
echo "<option value='" . $row['idType'] . "'>" . $row['dtType'] . '</option>';
}
?>
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.