Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (6.1k points)

What can be the best MySQL command to calculate the total no. of rows in one table without any conditions used to it? I am implementing this using PHP, therefore perhaps there is a PHP function which does this for me? I do not know. Here is an example of my PHP:

<?php
$con = mysql_connect("server.com","user","pswd");
if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("db", $con);

$result = mysql_query("some command");
$row = mysql_fetch_array($result);

mysql_close($con);
?>

1 Answer

0 votes
by (12.7k points)
edited by

You can use the following code:

<?php
$con = mysql_connect("server.com","user","pswd");
if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("db", $con);

$result = mysql_query("select count(1) FROM table");
$row = mysql_fetch_array($result);

$total = $row[0];
echo "Total rows: " . $total;

mysql_close($con); 

?> 

 Want to know more about SQL? Join this SQL Course by Intellipaat.

Related questions

0 votes
1 answer
asked Nov 29, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer
asked Jan 1 in SQL by Appu (6.1k points)
0 votes
1 answer
asked Dec 19, 2020 in SQL by Appu (6.1k points)

Browse Categories

...