$rowcount = 'SELECT COUNT(1) FROM (select * from isk.edi_site where postal_code = 123456)';
$stmt= oci_parse($conn, $rowcount);
oci_execute($stmt);
$num_row = oci_fetch_assoc($stmt);
$num = count($num_row, COUNT_RECURSIVE);
echo $num;
It counts and return the number "1". When I use the same SQL query in Oracle SQL developer, it echos out 4000+ count. Where would my mistake be? The column count works well..
A COUNT() query only returns a single row so you need to grab it from the oci_fetch_assoc() call. What you're doing is counting the rows in the result set which will always be 1.
$row = oci_fetch_assoc($stmt);
echo $row['COUNT(1)'];
Or give the count an alias:
SELECT COUNT(1) mycount FROM ...
$row = oci_fetch_assoc($stmt);
echo $row['MYCOUNT'];
Related
I'm trying to count the number of rows in a table and thought that this was the correct way to do that:
$result = $db->query("SELECT COUNT(*) FROM `table`;");
$count = $result->num_rows;
But counts always returns (int)1. If I use the same query in phpMyAdmin I get the right result. It sits in a table so I tried testing $count[0] as well, but that returns NULL.
What is the right way to do this?
You have to fetch that one record, it will contain the result of Count()
$result = $db->query("SELECT COUNT(*) FROM `table`");
$row = $result->fetch_row();
echo '#: ', $row[0];
Always try to do an associative fetch, that way you can easy get what you want in multiple case result
Here's an example
$result = $mysqli->query("SELECT COUNT(*) AS cityCount FROM myCity")
$row = $result->fetch_assoc();
echo $row['cityCount']." rows in table myCity.";
I find this way more readable:
$result = $mysqli->query('select count(*) as `c` from `table`');
$count = $result->fetch_object()->c;
echo "there are {$count} rows in the table";
Not that I have anything against arrays...
$result->num_rows; only returns the number of row(s) affected by a query. When you are performing a count(*) on a table it only returns one row so you can not have an other result than 1.
I am trying to write some PHP that will run an SQL query and then count the number of rows that query returned and store it so i can use it in an if statement. Currently i am outputting the number onto the screen for debug reasons and it always outputs as 0 whereas when i run the query directly in the database it returns over 600 rows. Why is this?
$result = $db->query('SELECT * FROM `bets` WHERE `user` = 76561198223084096');
$row_cnt = $result->mysql_num_rows;
printf("Result set has %d rows.\n",$row_cnt);
Any help is appreciated
... it always outputs as 0 whereas when i run the query directly in the database it returns over 600 rows.
The problem is because of this statement,
$row_cnt = $result->mysql_num_rows;
It should be,
$row_cnt = $result->num_rows;
Here's the reference:
mysqli_result::$num_rows
You can use the following solution:
$counter = mysql_query("SELECT COUNT(*) AS cnt FROM `bets` WHERE `user` = '76561198223084096'") ;
$num = mysql_fetch_array($counter);
$row_cnt= $num["cnt"];
printf("Result set has %d rows.\n",$row_cnt);
$result = $db->query("select count(*) as c from `bets` WHERE `user` = '76561198223084096'");
$count = $result->fetch_object()->c;
echo "Result set has {$count} rows ";
I searched in google and I found the following command:
SELECT COUNT(*) FROM table_name
And I used the above code as follows to count all rows that there are in a table.
$number = mysqli_query($connection,"SELECT COUNT(*) FROM table_name");
$rows = mysqli_num_rows($number);
echo $rows;
But when I see the result using the echo command, I see the result as the 1 value. While number of rows in my table is 12.
The query just returns 1 row. That row contains the count of rows in it. You need to fetch the row to get the answer, just like you do to get the results from any other query.
$result = mysqli_query($connection,"SELECT COUNT(*) AS count FROM table_name");
$row = mysqli_fetch_assoc($result);
$rows = $row['count'];
echo $rows;
here is my mysql statement:
$result = mysql_query("select count(*) from usersecurity where
email='".$_SESSION['username']."'");
echo mysql_num_rows($result);
Problem is, from my understanding, its always returning the value 1, even if the username doesn't exist in the table...why is that?
Because SELECT COUNT(*) ... with return a row with a single value containing zero.
COUNT(*) will always return a row, even if the result is zero. You need to examine the result of that column:
SELECT COUNT(*) AS count FROM t1 WHERE email = "SOME NON EXISTENT EMAIL";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo $row['count']; // 0
An alternative would be to select the count as the number of rows:
SELECT TRUE FROM t1 WHERE email = "SOME NON EXISTENT EMAIL";
$result = mysql_query($query);
echo mysql_num_rows($result); // 0
A COUNT(*) query always returns a single row. If you want the $ of rows then do one of the following.
$result = mysql_query("select count(*) as ROWS from usersecurity where
email='".$_SESSION['username']."'");
$rows = mysql_fetch_assoc($result);
echo $rows['ROWS'];
or
$result = mysql_query("select * as ROWS from usersecurity where
email='".$_SESSION['username']."'");
echo mysql_num_rows($result);
You will always get one row from the result query. That value will some value greater than or equal to zero. You need to inspect the actual count returned not the number of rows returned.
Your SELECT query is returning the following:
select count(*)...
----------------------
0
Thus, mysql_num_rows($result) is appropriately returning 1 as a result. If you're looking to get the value of your count, then you need to take an approach like the following:
$result = mysql_query("select count(*) from usersecurity where
email='".$_SESSION['username']."'");
$row = mysql_fetch_array($result);
echo $row[0];
I'm trying to count the number of rows in a table and thought that this was the correct way to do that:
$result = $db->query("SELECT COUNT(*) FROM `table`;");
$count = $result->num_rows;
But counts always returns (int)1. If I use the same query in phpMyAdmin I get the right result. It sits in a table so I tried testing $count[0] as well, but that returns NULL.
What is the right way to do this?
You have to fetch that one record, it will contain the result of Count()
$result = $db->query("SELECT COUNT(*) FROM `table`");
$row = $result->fetch_row();
echo '#: ', $row[0];
Always try to do an associative fetch, that way you can easy get what you want in multiple case result
Here's an example
$result = $mysqli->query("SELECT COUNT(*) AS cityCount FROM myCity")
$row = $result->fetch_assoc();
echo $row['cityCount']." rows in table myCity.";
I find this way more readable:
$result = $mysqli->query('select count(*) as `c` from `table`');
$count = $result->fetch_object()->c;
echo "there are {$count} rows in the table";
Not that I have anything against arrays...
$result->num_rows; only returns the number of row(s) affected by a query. When you are performing a count(*) on a table it only returns one row so you can not have an other result than 1.