0

我不明白为什么我没有进入num_rowsmysqli。

代码:

$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),只有空的......

知道为什么会这样吗?

4

1 回答 1

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;

像这样的东西应该工作。

您应该更好地考虑您对连接变量的处理方法。

于 2013-06-16T15:18:16.620 回答