Netra
Netra

Reputation: 338

mysqli if result > 0 do something

How would i do this using mysqli?

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

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

I tried:

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

But I dont get any result.

Upvotes: 0

Views: 2647

Answers (3)

Del Pedro
Del Pedro

Reputation: 1213

you have to call store_result() after execution.

php.net has a wonderful php doc: http://www.php.net/manual/de/mysqli-stmt.num-rows.php

for example:

$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();
}

Upvotes: 2

Jurgo
Jurgo

Reputation: 905

$number_of_rows = $SQL->num_rows;

Upvotes: 1

Mihai Iorga
Mihai Iorga

Reputation: 39704

You have started the query with $SQL .. continue with it as the object will be in that variable:

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

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

Upvotes: 2

Related Questions