为什么我不能在mysqli中获得num_rows?

时间:2013-06-16 15:10:54

标签: php mysqli

我无法理解为什么我没有在mysqli中获得num_rows

代码:

$MysqlHost='localhost';
$MysqlUser='test';
$MysqlPassword='password';
$MysqlDatabase='test_data';


function mysqli($query){
  $mysqli = new mysqli($GLOBALS["MysqlHost"],$GLOBALS["MysqlUser"],$GLOBALS["MysqlPassword"],$GLOBALS["MysqlDatabase"]);
  if (mysqli_connect_errno()) {
    printf("Bad connect: %s\n", mysqli_connect_error());
    exit();
  }
  $result = $mysqli->query("SET NAMES utf8");
  $result = $mysqli->query("set character_set_client='utf8'");
  $result = $mysqli->query("set collation_connection='utf8_general_ci'");
  $result = $mysqli->query($query);
  $mysqli->error;
  $result->error;
  $mysqli->close();
  return $result;
};

$res_user=mysqli("SELECT * FROM table");
print $res_user->num_rows;

我得到空结果,而不是null(0),只有空......

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

$MysqlHost='localhost';
$MysqlUser='test';
$MysqlPassword='password';
$MysqlDatabase='test_data';


function mysqli($query){
  $mysqli = new mysqli($GLOBALS["MysqlHost"],$GLOBALS["MysqlUser"],$GLOBALS["MysqlPassword"],$GLOBALS["MysqlDatabase"]);
  if (mysqli_connect_errno()) {
    return mysqli_connect_error();
  }
  else {
    $result = $mysqli->query("SET NAMES utf8");
    $result = $mysqli->query("set character_set_client='utf8'");
    $result = $mysqli->query("set collation_connection='utf8_general_ci'");
    $result = $mysqli->query($query);

    $err = $mysqli->error;
    $rrr = $result->error;
    $num = $result->num_rows;
    $mysqli->close();

    return Array($result, $num, $err, $rrr);
  }
};

$res_user=mysqli("SELECT * FROM table");
print $res_user->num_rows;

这样的事情应该有效。

您应该对连接变量的方法有所了解。