PHP Count Rows In Oracle Database - php

I am trying to count the total number of rows in a single table. Just need to know how many rows total and nothing else. I am using Oracle database.
I have tried
$sql = "SELECT COUNT(*) AS homes FROM homes";
But I am unsure on how to display the result.

$sql = "SELECT COUNT(*) AS count FROM homes";

To count the rows from
$sql = "SELECT * FROM homes"; // fetch the rows
$sql1=mysql_query($sql);
$count=mysql_num_rows($sql1) // count number of rows in table
echo $count; //$count has the value of rows in table..

Use this code:
// Code goes here
<?php
$sql = "SELECT COUNT(*) AS homes FROM homes";
$result = mysqli_query($sql);
$row = mysqli_fetch_array($result);
print($row['homes']); // prints count
?>

<?php
$link = mysqli_connect("host", "username", "password");
mysqli_select_db("database", $link);
$result = mysqli_query("SELECT * FROM homes", $link);
$num_rows = mysqli_num_rows($result);
echo "$num_rows Rows\n";
?>

Related

How to display the five last rows in a database

I want to write HTML code that displays the last five rows from the database,
I have code which shows the last row, but I want to have the last five rows.
My code:
<?php
$conn = mysqli_connect("localhost", "root", "", "refd-2");
$sql = mysqli_query($conn, "SELECT * FROM last ORDER BY no DESC LIMIT 1");
$print_data = mysqli_fetch_row($sql);
echo $print_data[1];
echo "\n";
You can get the last 5 results from DB if you specify LIMIT 5.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$con = new mysqli("localhost","root","","refd-2");
$con->set_charset('utf8mb4');
$result = $con->query("SELECT * FROM last ORDER BY no DESC LIMIT 5");
// Get all rows at once as an array of rows
$rows = $result->fetch_all();
print_r($rows);
// or use foreach loop
foreach($rows as $row) {
print_r($row);
}
The fetch_all() method can be skipped if not needed and you can loop directly on the results.
foreach($result as $row) {
print_r($row);
}
try this :
<?php
$conn = mysqli_connect("localhost", "root", "", "refd-2");
$sql = mysqli_query($conn, "SELECT * FROM last ORDER BY no DESC LIMIT 5");
while($print_data = mysqli_fetch_array($sql)){
echo $print_data["row"];
// here you will change the name "row" to your database row name
echo "\n";
}
?>
Change LIMIT 1 with LIMIT 5 and fetch your resultset.
<?php
//database connectivity
$con=mysqli_connect("localhost","root","","refd-2") or die(mysqli_error());
//select values from empInfo table
$sql = "SELECT * FROM last ORDER BY no DESC LIMIT 5'";
$result = mysqli_query($con,$sql);
print_r(mysqli_fetch_array($result));
mysqli_close($con);
?>

Total Results versus Returned Results in PHP

I am using the following php to display the number of records returned in a db search.
$sql = "SELECT COUNT(id) FROM authorsbooks WHERE author LIKE '%$searchquery%'";
$query = mysqli_query($dbc, $sql);
$row = mysqli_fetch_row($query);
$rows = $row[0];
$textline1 = "Your Search Returned (<b>$rows</b>) Records";
<?php echo $textline1; ?>
This seems to work fine.
However, I cannot get the total number of records in the actual db to display.
Can anyone explain a way of getting the total number of records in the database. Btw, I have tried using $total = mysqli_num_rows($query) but it keeps returning 1 as an answer. Thanks for any help.
For that you've to fire another SQL query. Like this,
$sql = "SELECT COUNT(id) FROM authorsbooks";
$query = mysqli_query($dbc, $sql);
$row = mysqli_fetch_row($query);
$rows = $row[0];
echo $rows; // will return total rows in database.
SELECT COUNT(*) FROM authorsbooks
It's true that $total = mysqli_num_rows($query) should return one row. When you do a SELECT COUNT(*) then the query returns 1 row telling you how many matches there were in the table.

Count function not counting all rows in db table

I am trying to count the number of rows in a table, using the count function. I currently have 5 rows in the table in question. However the count function is only counting 1 row. Why is this? Any suggestions.
$count_pcode = mysqli_query($dbc, "SELECT COUNT(*)FROM Delivery_Pcode");
$count_row =mysqli_num_rows($count_pcode);
printf("%d results.\n",$count_row);
mysqli_free_result($count_pcode );
mysqli_close($dbc);
Just change the first line to:
$count_pcode = mysqli_query($dbc, "SELECT * FROM Delivery_Pcode");
The next line of code will then tell you how many rows your query return
$count_row =mysqli_num_rows($count_pcode);
This code should count the number of rows, and do so efficiently.
$result = mysql_query($dbc, "SELECT COUNT(*)FROM Delivery_Pcode");
// Verify it worked
if (!$result) echo mysql_error();
$row = mysql_fetch_row($result);
// Should show you an integer result.
print_r($row);
mysqli_free_result($result);
mysqli_close($dbc);
Try the following:
$count_pcode = mysqli_query($dbc, "SELECT * FROM Delivery_Pcode");
$count_row = mysqli_num_rows($count_pcode);

Count number of rows in SQL database

I am trying to view how many rows there are based on a SQL query using PHP.
I seem to be able query the database and return fields from a row but can't seem to find out how many rows there are based on the same query.
<?php
$host = 'localhost';
$user = 'MyUsername';
$pass = 'MyPassword';
$database = 'MyDatabase';
$con=mysqli_connect($host,$user,$pass ,$database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM MyTable WHERE test='123' AND test2='456'");
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
mysqli_close($con);
?>
All it returns is the text Rows on the screen, without the number of rows at the start.
Like I said, this same query works and returns a value if I try and select a row using:
while($row = mysqli_fetch_array($result))
{
echo $row["test"];
}
Anyone know why it won't return the number of rows?
You are using MySQLi. Because of you don't have a mysql query, mysql_num_rows doesn't return desired value.
You have to replace your mysql function with mysqli equal:
$num_rows = mysqli_num_rows($result);
You are using Mysqli to you should use mysqli_num_rows
$result = mysqli_query($con,"SELECT * FROM MyTable WHERE test='123' AND test2='456'");
$num_rows = mysqli_num_rows($result);
If you want only count then you can directly use count(*) like this:-
$result = mysqli_query($con,"SELECT COUNT(*) FROM MyTable WHERE test='123' AND test2='456'");
echo $result;
I'll do some thing like this:
$result = mysqli_query($con,"SELECT COUNT(*) FROM MyTable WHERE test='123' AND test2='456'");
echo $result

'Counting' the number of records that match a certain criteria and displaying the numbers (using PHP and MySQL)

I have a MySQL database containing a user's country and whether they are an individual or an organisation. The field names are 'country' and 'type'.
Using PHP, I'd like to 'count' the number of countries, the number of individuals and the number of organisations in the database and then display the numbers in the following example format:
<p>So far, <strong>500</strong> individuals and <strong>210</strong> organisations from <strong>40</strong> countries have registered their support.</p>
I am currently listing the total number of records using the below code if this helps:
<?php
$link = mysql_connect("localhost", "username", "password");
mysql_select_db("database_name", $link);
$result = mysql_query("SELECT * FROM table_name", $link);
$num_rows = mysql_num_rows($result);
echo " $num_rows\n ";
?>
My PHP / MySQL skills are very limited so I'm really struggling with this one.
Many thanks in advance!
Ben
To get the number of countries:
SELECT COUNT(DISTINCT country) AS NumCountries FROM tableName
To get the number of individuals, or the number of organisations:
SELECT COUNT(*) AS NumIndividuals FROM tableName WHERE type = 'individual'
SELECT COUNT(*) AS NumOrganisations FROM tableName WHERE type = 'organisation'
What you are looking for is a count based on a grouping. Try something like this:
$sql = "SELECT type, count(*) as cnt FROM users GROUP BY type";
$result = mysql_query($sql);
$counts = array();
while ($row = mysql_fetch_assoc($result)) {
$counts[$row['type']] = $row['cnt'];
}
This will give you an array like
Array (
'individual' => 500,
'organization' => 210
)
For counting the countries, use the first statement as posted by Hammerite.
EDIT: added a verbose example for counting the countries
$sql = "SELECT COUNT(DISTINCT country) AS NumCountries FROM users";
$result = mysql_query($sql);
$number_of_countries = 0;
if ($row = mysql_fetch_assoc($result)) {
$number_of_countries = $row['NumCountries'];
}
This altogether you can then print out:
printf('<p>So far, <strong>%d</strong> individuals and <strong>%d</strong> '.
'organisations from <strong>%d</strong> countries have registered '.
'their support.</p>', $counts['individual'], $counts['organization'],
$number_of_countries);
The answer is to retrieve the answer by using the COUNT(*) function in SQL:
SELECT COUNT(*) AS individual_count FROM user WHERE type = 'individual';
SELECT COUNT(*) AS organization_count FROM user WHERE type = 'organization';
SELECT COUNT(*) AS country_count FROM user GROUP BY country;
The last will group your query set by the country name, and will result in one row for each country. Using COUNT on this result set will give the count of distinct coutries.
You can then fetch this value by using mysql_fetch_assoc on your $result from mysql_query, and the answer will be contained in 'invididual_count', 'organization_count' and 'country_count' for each query.
Thank you for all of your help with this (especially Cassy).
I think it's worthwhile displaying the full code in case anybody else comes across a similar requirement in the future:
<?php
$link = mysql_connect("localhost", "username", "password");
mysql_select_db("database_name", $link);
$sql = "SELECT type, COUNT(*) as cnt FROM table_name GROUP BY type";
$result = mysql_query($sql);
$counts = array();
while ($row = mysql_fetch_assoc($result)) {
$counts[$row['type']] = $row['cnt'];
}
$sql = "SELECT COUNT(DISTINCT country) AS NumCountries FROM table_name";
$result = mysql_query($sql);
$number_of_countries = 0;
if ($row = mysql_fetch_assoc($result)) {
$number_of_countries = $row['NumCountries'];
}
printf('<p><strong>So far, <em class="count">%d</em> individuals and <em class="count">%d</em> organisations from <em class="count">%d</em> countries have registered their support.', $counts['Individual'], $counts['Organisation'], $number_of_countries); ?>
If you're just looking for the number of rows returned try this:
$result = mysql_db_query($db, $query);
$num_rows = mysql_num_rows($result);
Another option would be to execute a separate query with the mysql count function and use the result from that.

Categories