Jump to content

[SOLVED] MySQL row count (prepared statement)


malikah

Recommended Posts

Hi, I'm having trouble returning a full row count from my database. The following code returns "1" column instead of "24" columns.

 

$conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME)

$query = "SELECT * FROM table";	

	if($stmt = $this->conn->prepare($query))
	{
		$stmt->execute();

		if($stmt->fetch())
		{
			if ($stmt->field_count) {
				$result = $stmt->store_result();
			}
			$stmt->close();
			return $result;
		}
	}

You may want to use mysql_num_rows() to count your rows with.

 

http://us.php.net/manual/en/function.mysql-num-rows.php

 

I tried that, but unfortunately get the error: "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource".

You need to store results so you can count them PHP: mysqli::store_result.

 

Edit: actually look at this if you are using prepared statements: PHP: mysqli_stmt::num_rows - Manual

 

Read the Typer85 at gmail dot com's comment at the bottom of the page.

OK, I used "DESCRIBE table" to get it working:

 

$query = "DESCRIBE table";   
      
      if($stmt = $this->conn->prepare($query))
      {
         $stmt->execute();
         $stmt->store_result();
         echo $stmt->num_rows();
         $stmt->close();
      }

 

NOW.. All I need is to return the names of each field...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.