mysql_num_rows equivalent in aura sql

531 Views Asked by At

Hi am using Aura sql to perform querying. What is the equivalent function for mysql_num_rows in aura sql.

I have to check:

if(mysql_num_rows($query)==1)
 // do something
else
 // do something

For this i need the equivalent function in Aura.Sql.

1

There are 1 best solutions below

2
Hari K T On

Aura.Sql uses PDO internally. The equivalent to mysql_num_rows http://www.php.net/manual/en/function.mysql-num-rows.php points to http://www.php.net/manual/en/pdostatement.rowcount.php .

If you are using v1 of aura insert, update, delete etc always returns the number of affected rows. See https://github.com/auraphp/Aura.Sql/blob/develop/src/Aura/Sql/Connection/AbstractConnection.php#L953 .

If you are using a select statement you could make use of the count() or you can use fetchOne https://github.com/auraphp/Aura.Sql/tree/develop#fetching-results .

So in this case I will say

// the text of the query
$text = 'SELECT * FROM foo WHERE id = :id AND bar IN(:bar_list)';

// values to bind to query placeholders
$bind = [
    'id' => 1,
    'bar_list' => ['a', 'b', 'c'],
];

// returns all rows; the query ends up being
// "SELECT * FROM foo WHERE id = 1 AND bar IN('a', 'b', 'c')"
$result = $connection->fetchOne($text, $bind);
if (! empty($result)) {
}

Let me know if that helps!