我忘记了从哪里复制它(我认为用户在php.net上发布了它)但是当用于包含简单INNER JOIN的语句时,这个用于获取准备好的和正常的MySQLi语句的结果的函数没有返回任何结果,至少我试过的那个:
function fetch($result){
$array = array();
if($result instanceof mysqli_stmt){
$result->store_result();
$variables = array();
$data = array();
$meta = $result->result_metadata();
while($field = $meta->fetch_field()){
$variables[] = &$data[$field->name];
}
call_user_func_array(array($result, 'bind_result'), $variables);
$i=0;
while($result->fetch()){
$array[$i] = array();
foreach($data as $k=>$v)
$array[$i][$k] = $v;
$i++;
}
}elseif($result instanceof mysqli_result){
while($row = $result->fetch_assoc())
$array[] = $row;
}
return $array;
}
我一直在尝试使用它(所有这些函数都是类$ database的方法):
function query($str, $values){
if($stmt=$this->sql->prepare($str)){
$types=$this->castArrayAsString($values); //returns a string of $values's types for bind_params
array_unshift($values, $types);
call_user_func_array(array(&$stmt, 'bind_param'), $this->makeValuesReferenced($values));
$stmt->execute();
$result=$this->fetch($stmt); //the function I'm having problems with
if(empty($result)){
$return=true;
}else{
$return=$result;
}
$stmt->close();
}else{
echo $this->sql->error;
$return=false;
}
return $return;
}
这不会返回任何内容:
$list=$database->query("SELECT a.field1, b.field2 FROM table1 AS a INNER JOIN table2 AS b ON a.id_table2=b.id WHERE a.someid=?", array($someid));
这很好用:
$list=$database->query("SELECT field1, field2 FROM table1 WHERE someid=?", array($someid));
如果我从提取结果中打印出元数据,它会显示选择的字段等,但仍然没有在fetch()函数中给出最终结果。
感谢任何帮助,
由于
编辑这里是整个课程
<?php
class database{
var $HOST="xx.xx.xx.xxx";
var $USER="xxxxxxxxxxx";
var $PASS='xxxxxxxxxx';
var $DATABASE="my_database";
var $sql;
function database(){
$this->sql=new mysqli($this->HOST, $this->USER, $this->PASS, $this->DATABASE);
if($this->sql->connect_errno){
echo "ERROR - No MySQL connection: ".$mysqli->connect_error;
exit;
}
}
function query($str, $values){
if($stmt=$this->sql->prepare($str)){
$types=$this->castArrayAsString($values);
array_unshift($values, $types);
call_user_func_array(array(&$stmt, 'bind_param'), $this->makeValuesReferenced($values));
$stmt->execute();
$result=$this->fetch($stmt);
if(empty($result)){
$return=true;
}else{
$return=$result;
}
$stmt->close();
}else{
echo $this->sql->error;
$return=false;
}
return $return;
}
function fetch($result){
$array = array();
if($result instanceof mysqli_stmt){
$result->store_result();
$variables = array();
$data = array();
$meta = $result->result_metadata();
while($field = $meta->fetch_field()){
$variables[] = &$data[$field->name];
}
call_user_func_array(array($result, 'bind_result'), $variables);
$i=0;
while($result->fetch()){
$array[$i] = array();
foreach($data as $k=>$v)
$array[$i][$k] = $v;
$i++;
}
}elseif($result instanceof mysqli_result){
while($row = $result->fetch_assoc())
$array[] = $row;
}
return $array;
}
function close(){
$this->sql->close();
}
function castArrayAsString($array){
$types="";
foreach($array as $key => $value) {
if(is_numeric($value)){
if(is_float($value)){
$types.="d";
}else{
$types.="i";
}
}else{
$types.="s";
}
}
return $types;
}
function makeValuesReferenced($arr){
$refs=array();
foreach($arr as $key => $value){
$refs[$key] = &$arr[$key];
}
return $refs;
}
}
$database=new database;
&GT;
答案 0 :(得分:0)
返回0结果集的查询正在执行“内部联接”,这意味着源表和目标表必须为查询中链接的列共享相同的值(在本例中为a.id_table2)和b.id)。您可能没有任何匹配值,因此您获得0结果集。尝试左外连接。
尝试以下操作,看看是否返回了结果:
"SELECT a.field1, b.field2 FROM table1 AS a INNER JOIN table2 AS b ON a.id_table2=b.id WHERE a.someid = :someId"
$dbo = new Database();
$stmt = $dbo->sql->prepare($sql);
$stmt->bindValue(':someId', $someId, PDO::PARAM_INT);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
var_dump($row);
}