I can't count my users in the database with my current configurations.
<?php
require_once 'scripts/app_config.php';
mysqli_connect(DATABASE_HOST,DATABASE_USER,DATABASE_PASS,DATABASE_NAME);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT COUNT(*) FROM users";
$result = mysql_query($query);
$num = mysql_num_rows($result);
echo "There are: ". $num . " users in the database";
mysqli_close($con);
?>
Your problem is this:
$num = mysql_num_rows($result);
Your query only returns 1 row, so I presume the problem you're having is that it always outputs: There are 1 users in the database.
You should instead use:
$num = mysqli_fetch_array($query)[0];
Either use COUNT(*) OR use mysql_num_rows
$query = "SELECT COUNT(*) FROM users";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$num = $row[0];
echo "There are: ". $num . " users in the database";
Note : mysql_* function is deprecated, move on mysqli_* function asap.
Related
I am trying to add numbers located in my database. so I tried sum() in SQL but it didn't show the total number. I don't know whats wrong with my code.
This is my code
<?php
session_start();
$email = $_SESSION['login_user'];
$con = mysqli_connect("localhost" , "root" , "" , "db_name");
if (mysqli_connect_errno()) {
echo "failed to connect:" . mysqli_connect_error();
}
$sql = "SELECT SUM(points) FROM points WHERE email='$email '";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result, MYSQLI_NUM);
echo $row[0]. "total number" ;
mysqli_free_result($result);
mysqli_close($con);
?>
it just shows total number. i don't know what is wrong here
Use as total, and row of 'total' by mysqli_fetch_assoc.
Very basic but as a rookie I am struggling. The echo doesnt show any value, just the text. What am I doing wrong?
Connect.php:
<?php
$connection = mysqli_connect('test.com.mysql', 'test_com_systems', 'systems');
if (!$connection){
die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'swaut_com_systems');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($connection));
}
?>
Get.php:
<?php
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username=test";
$result2 = mysqli_query($connection, $query2);
echo ( 'SystemID: '.$result2);
?>
Assuming you have connected to the database successfully then the query is incorrect. You must wrap all text values in quotes like this
<?php
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username='test'";
$result2 = mysqli_query($connection, $query2);
Now the mysqli_query submits the query to the database where it is run and a result set built. To see the result set you need to read the result set back from the database using one of the fetch functions for example
$row = mysqli_fetch_assoc($result2);
echo 'SystemID: ' . $row['systemid'];
If there are more than one rows in the result set you must do that in a loop like this
while ($row = mysqli_fetch_assoc($result2)){
echo 'SystemID: ' . $row['systemid'];
}
You are printing the mysqli result object. In order to printthe result you have to use:
$row = mysqli_fetch_assoc($result2);
print_r($row);
You need to collect the results of the mysqli_query using the following:
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username=test";
$result2 = mysqli_query($connection, $query2);
while ($row = mysqli_fetch_assoc($result2))
{
echo "System ID is: " . $row['systemid'];
}
I want to display 24 unique id's but he is only giving me 1:
This is my php
<?php $sql = "SELECT id
FROM 15players
ORDER BY RAND()
LIMIT 24"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error());
$row = mysql_fetch_array($result);
echo $row['id']; ?>
But if I execute this SQL in phpmyadmin,
It returns me 24 id's
So what am I doing wrong?
EDIT:
Got the right answer!
<?php $sql = "SELECT id
FROM 15players
ORDER BY RAND()
LIMIT 0,24";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
while($row = mysql_fetch_array($result)) {
echo $row['id'];
}
?>
mysql_fetch_array only returns 1 row you need to loop through the results.
while($row = mysql_fetch_array($result)) {
echo $row['id'];
}
If multiple results are fetched via a query, you need to iterate over it.
<?php
$sql = "SELECT id
FROM 15players
ORDER BY RAND()
LIMIT 24"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['id'];
}
?>
Also mysql_* is deprecated, start using PDO.
mysql_fetch_array only get one row at a time. You need to loop through all of your results.
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo $row['id']. '<br>';
}
You should be using mysqli or PDO. The mysql_ is deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.
I know that $wins should be 3. Because I have 3 rows with the integer "1" in the "win" column on table "rated_teams" But for some reason this code will not work. Can you find the problem please? ALSO, I'm aware that some of this is depracted. I'll update the whole page, once I get it at least in working condition.
<?php
$sql = "SELECT SUM(win) FROM rated_teams WHERE server='$server' AND name='$myteam'";
$query = mysql_query($sql, $con)
or die('A error occured: ' . mysql_error());
while ((mysql_fetch_array($query))) {
$wins = $row['SUM(win)'];
}
?>
<h3>Total Wins: <?php echo $wins?> </h3>
Try with
$sql = "SELECT SUM(win) as sum FROM rated_teams WHERE server='$server' AND name='$myteam'";
and while you are getting give like
while ($row = mysql_fetch_array($query)) {
$wins = $row['sum'];
}
And my advice is try to avoid mysql_* functions due to they are deprecated.Instead use mysqli_* functions or PDO statements.
You do not set the $row variable. Edit your while to this.
while ($row = mysql_fetch_array($query))
You need to give your calculated column an alias. Try this:
<?php
$sql = "SELECT SUM(win) as sumwin FROM rated_teams WHERE server='$server' AND name='$myteam'";
$query = mysql_query($sql, $con) or die('A error occured: ' . mysql_error());
while ($row = mysql_fetch_array($query)) {
$wins = $row['sumwin'];
}
?>
<h3>Total Wins: <?php echo $wins?> </h3>
Please write sql query right manner.Write like this.
$sql = "SELECT SUM(win) as sumwin FROM rated_teams WHERE server='".$server."' AND name='".$myteam."'";
while ((mysql_fetch_array($query))) {
should be
while ($row = mysql_fetch_array($query) ) {
What is the best MySQL command to count the total number of rows in a table without any conditions applied to it? I'm doing this through php, so maybe there is a php function which does this for me? I don't 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);
?>
<?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);
?>
Either use COUNT in your MySQL query or do a SELECT * FROM table and do:
$result = mysql_query("SELECT * FROM table");
$rows = mysql_num_rows($result);
echo "There are " . $rows . " rows in my table.";
mysqli_num_rows is used in php 5 and above.
e.g
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
printf("Result set has %d rows.\n",$rowcount);
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
Use COUNT in a SELECT query.
$result = mysql_query('SELECT COUNT(1) FROM table');
$num_rows = mysql_result($result, 0, 0);
you can do it only in one line as below:
$cnt = mysqli_num_rows(mysql_query("SELECT COUNT(1) FROM TABLE"));
echo $cnt;
use num_rows to get correct count for queries with conditions
$result = $connect->query("select * from table where id='$iid'");
$count=$result->num_rows;
echo "$count";
for PHP 5.3 using PDO
<?php
$staff=$dbh->prepare("SELECT count(*) FROM staff_login");
$staff->execute();
$staffrow = $staff->fetch(PDO::FETCH_NUM);
$staffcount = $staffrow[0];
echo $staffcount;
?>
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
echo "number of rows: ",$rowcount;
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
it is best way (I think) to get the number of special row in mysql with php.
<?php
$conn=mysqli_connect("127.0.0.1:3306","root","","admin");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="select count('user_id') from login_user";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($result);
echo "$row[0]";
mysqli_close($conn);
?>
Still having problem visit my tutorial http://www.studentstutorial.com/php/php-count-rows.php
$sql = "select count(column_name) as count from table";
Well, I used the following approach to do the same: I have to get a count of many tables for listing the number of services, projects, etc on the dashboard. I hope it helps.
PHP Code
// create a function 'cnt' which accepts '$tableName' as the parameter.
function cnt($tableName){
global $conection;
$itemCount = mysqli_num_rows(mysqli_query($conection, "SELECT * FROM `$tableName`"));
echo'<h6>'.$itemCount.'</h6>';
}
Then when I need to get the count of items in the table, I call the function like following
<?php
cnt($tableName = 'projects');
?>
In my HTML front end, so it renders the count number
It's to be noted that I create the cnt() function as a global function in a separate file which I include in my head, so I can call it from anywhere in my code.