jcslzr
jcslzr

Reputation: 435

How to get mysqli_num_row value

Im learning and doing some excercises from a book, and I have an arror after a query.

I want to know the value of mysqli_num_row to further understand my error but I do not get a value.

I already check and if I put echo 'r'; I get an r in the screen so I know that line is getting executed but I can not get anything for the value of mysqli_num_row.

What should I write in the final line to get this value on the screen?

$q = "SELECT user_id, first_name FROM users WHERE email='$e' AND pass=SHA1('$p')";
$r = @mysqli_query($dbc, $q); // Run the query.
echo mysqli_num_row($r);

Right now I am just getting a blank screen.

Thanks.

Upvotes: 1

Views: 2328

Answers (5)

bansi
bansi

Reputation: 57002

As you are learning start with prepared statements, it is much easier and less prone to injection attacks. here is a sample.

/* Create a new mysqli object with database connection parameters */
   $mysqli = new mysqli('localhost', 'username', 'password', 'db');

   if(mysqli_connect_errno()) {
      echo "Connection Failed: " . mysqli_connect_errno();
      exit();
   }

   /* Create a prepared statement */
   if($stmt = $mysqli -> prepare("SELECT user_id, first_name FROM users WHERE email=? AND pass=SHA1(?)")) {

      /* Bind parameters
         s - string, b - blob, i - int, etc */
      $stmt -> bind_param("ss", $e, $p);

      /* Execute it */
      $stmt -> execute();

      /* Get num rows */
      $mum_rows = $stmt -> num_rows();

      /* Bind results */
      $stmt -> bind_result($user_id, $first_name);

      /* Fetch the value. You may need to fetch in loop if has more than 1 rows. */
      $stmt -> fetch();

      /* echo the result. */
      echo $user_id, ' = ', $first_name;

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

   /* Close connection */
   $mysqli -> close();

Note: error handling implementation is left to you.
This is Object oriented style you can use Procedural style also if you are not comfortable with Object oriented style

Upvotes: 1

Krish R
Krish R

Reputation: 22711

Can you try this, You have missed to add s in mysqli_num_rows it should be mysqli_num_rows()

$e =  mysqli_real_escape_string($dbc, $e);
$p =  mysqli_real_escape_string($dbc, $p);
$q = "SELECT user_id, first_name FROM users WHERE email='$e' AND pass=SHA1('$p')";
$r = mysqli_query($dbc, $q) or die(mysqli_error());
echo mysqli_num_rows($r);

Upvotes: 1

i think you need to write your code like in below.

$q = "SELECT user_id, first_name FROM users WHERE email='$e' AND pass=SHA1('$p')";
$r =  mysqli_query($dbc, $q); // Run the query.
echo mysqli_num_rows($r);

You just remove @ sign from @mysqli_query.

Thanks

Upvotes: 1

zzlalani
zzlalani

Reputation: 24374

you are using wrong function it is actually mysqli_num_rows(); you are missing s in the end of row

echo mysqli_num_rows($r);

http://www.w3schools.com/php/func_mysqli_num_rows.asp

Upvotes: 3

MaGnetas
MaGnetas

Reputation: 5108

You might have skipped a letter. Try:

echo mysqli_num_rows($r);

Upvotes: 1

Related Questions