我正在为mysql语句构建我的第一个类,以便让我自己更容易查询。基本上我们会这样做。
Query::set_config($host,$username,$password);
if( Query::is_database("database_name") ) {
Query::get_results("SELECT * FROM REGISTRY WHERE `name`=? AND `last`=?",[
"s"=>"firstname",
"s"=>"lastname"
]);
}
我的问题是准备与否之间的细微差别,以及程序和OO。
以下是get_results
/*Load Results from SQL Query*/
public static function results($sql,$prepared=null) {
if($is_configurated) {
self::connect();
$rows = array();
if($prepared === null) {
$query = mysqli_query(self::$conn,$sql);
if(!$query)
echo mysqli_error(self::$conn);
while($r = mysqli_fetch_array($query)) {
$rows[] = $r;
}
}
else
{
$stmt = mysqli_prepare(self::$conn,$sql);
/*
i corresponding variable has type integer
d corresponding variable has type double
s corresponding variable has type string
b corresponding variable is a blob and will be sent in packets
*/
foreach($prepared as $type=>$param) {
if(strlen($type) > 1) {
self::$errors[]="Only one type is allowed";
continue;
} else {
mysqli_stmt_bind_param($stmt,$type,$param);
}
}
/*STUCK HERE NOW*/
mysqli_stmt_execute($stmt);
while (mysqli_stmt_fetch($stmt)) {
$rows[] = $r;
}
}
self::disconnect();
return $rows;
} else return "NO CONFIGURATED CONNECTION";
}
我写了我被卡住的地方,这就像普通mysqli
while
mysqli_fetch_row
或者什么。 PHP.net文档不是很流畅也不是很好。我只是需要帮助这样做,看看我的代码中是否有任何小缺陷。我需要从结果中返回行。
这是正确的吗?
/*Load Results from SQL Query*/
public static function results($sql,$prepared=null) {
if($is_configurated) {
self::connect();
$rows = array();
if($prepared === null) {
$query = mysqli_query(self::$conn,$sql);
if(!$query)
echo mysqli_error(self::$conn);
while($r = mysqli_fetch_array($query)) {
$rows[] = $r;
}
}
else
{
$stmt = mysqli_prepare(self::$conn,$sql);
/*
i corresponding variable has type integer
d corresponding variable has type double
s corresponding variable has type string
b corresponding variable is a blob and will be sent in packets
*/
foreach($prepared as $type=>$param) {
if(strlen($type) > 1) {
self::$errors[]="Only one type is allowed";
continue;
} else {
mysqli_stmt_bind_param($stmt,$type,$param);
}
}
/*STUCK HERE NOW*/
mysqli_stmt_execute($stmt);
$stored = mysqli_store_result($stmt);
if( count($stored) >= 1) {
while ($data = mysqli_stmt_fetch($stmt)) {
$rows[] = $data;
}
}
}
self::disconnect();
return $rows;
} else return "NO CONFIGURATED CONNECTION";
}
答案 0 :(得分:0)
如果要在预准备语句中使用mysqli函数,请查看mysqli_stmt_get_result
函数。但是,此函数仅适用于mysqlnd驱动程序。