mysqli如果结果> 0做点什么

时间:2012-09-06 13:06:21

标签: php mysqli

我如何使用mysqli?

$SQL = mysql_query("SELECT username FROM users WHERE username = '$new_username'");
$result = mysql_num_rows($SQL);

If(!$result > 0) {
echo...

我试过了:

$SQL = $mysqli->query("SELECT username FROM users WHERE username = '$utilizator");
$rezultat->num_rows($SQL);`

但我没有得到任何结果。

3 个答案:

答案 0 :(得分:2)

您已使用$SQL启动查询..继续使用它,因为对象将位于该变量中:

$SQL = $mysqli->query("SELECT username FROM users WHERE username = '$utilizator'");
$num = $SQL->num_rows();

if($num){
    // run your code here
}

答案 1 :(得分:2)

你必须在执行后调用store_result()。

php.net有一个很棒的php文档:http://www.php.net/manual/de/mysqli-stmt.num-rows.php

例如:

$query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20";
if ($stmt = $mysqli->prepare($query)) {
  /* execute query */
  $stmt->execute();

  /* store result */
  $stmt->store_result();

  printf("Number of rows: %d.\n", $stmt->num_rows);

  /* close statement */
  $stmt->close();
}

答案 2 :(得分:1)

$number_of_rows = $SQL->num_rows;