我开始学习如何使用准备好的查询,所以我正在更改一个门户以使用这些,由于某种原因我无法让它工作我运行了一个更简单的版本并且它工作了,然后我把它变成了一个函数和它停止工作,告诉我没有选择数据库...这是代码:
配置文件
//connection to the database
$mysqli = new mysqli($host, $user, $password, $database);
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
索引.php:
<?php
include ("system/mainFunctions.php");
$page = 'home';
$sub = NULL;
if (isset($_GET["page"]))
$page = $_GET["page"];
if (isset($_GET["sub"]))
$sub = $_GET["sub"];
?>
MainFunctions.php
function getContent($page , $sub, &$connection){
if($page == 'home' || ($page == 'home' && $sub ==NULL))
$qry = "SELECT * FROM public WHERE section = ?";
else
$qry = "SELECT * FROM public WHERE section = ? AND subsection = ?";
$stmt = $connection->stmt_init();
if ($stmt->prepare($qry)) {
if($page == 'home' || ($page == 'home' && $sub ==NULL))
$stmt->bind_param("s" , $page);
else
$stmt->bind_param("ss" , $page, $sub);
$stmt->execute();
$metaResults = $stmt->result_metadata();
$fields = $metaResults->fetch_fields();
$statementParams='';
//build the bind_results statement dynamically so I can get the results in an array
foreach($fields as $field){
if(empty($statementParams)){
$statementParams.="\$column['".$field->name."']";
}else{
$statementParams.=", \$column['".$field->name."']";
}
}
$statment="\$stmt->bind_result($statementParams);";
eval($statment);
while($stmt->fetch()){
if($page == 'home' || ($page == 'home' && $sub ==NULL))
formatNews($column);
else
formatStatic($column);
}
$stmt->close();
}
$connection->close();
}