How do I get the outcome of
SELECT COUNT(user) FROM game_paths WHERE user = '$user'
to a PHP variable
I tried
mysql_num_rows
but it returns nothing.
You should use mysql_result and get the first column of the result. Like this:
$result = mysql_query("SELECT COUNT(user) FROM game_paths WHERE user='$user'");
$count = mysql_result($result, 0);
You can also alias the column like this:
$result = mysql_query("SELECT COUNT(user) AS total FROM game_paths WHERE user='$user'");
$data = mysql_fetch_assoc($result);
$count = $data['total'];
Which might be better if you're going to select several columns at the same time, and also for readability.
Try like this. you need to use mysql_fetch_assoc or mysql_fetch_array functions
$result = mysql_query(" SELECT COUNT(user) as total FROM game_paths WHERE user='$user' ");
$row = mysql_fetch_assoc($result);
echo $row['total'];
Or
$result = mysql_query(" SELECT COUNT(user) FROM game_paths WHERE user='$user' ");
$row = mysql_fetch_array($result);
echo $row[0];
Docs Link: http://us2.php.net/mysql_fetch_array
http://www.w3schools.com/php/func_mysql_fetch_array.asp
Note: mysql_* function are deprecated try to use mysqli or PDO
You can use the following code:
$result = mysql_query(" SELECT COUNT(user) FROM game_paths WHERE user='$user' ");
$row = mysql_fetch_array($result);
$count= $row[0];
or
$result = mysql_query("SELECT * FROM game_paths WHERE user='$user'");
$count=mysql_num_rows($result);
This will return the number of rows satisying the condition.
Hey friend Try this code, ithink this will solve your problem
<?php
$con=mysql_connect('hostname','DBusername','paassword');
mysql_select_db('Db_name',$conn);
$query="SELECT COUNT(user) as total FROM game_paths WHERE user='$user'";
$result=mysql_query($query,$con);
$row=mysql_fetch_array($result);
echo $row['total'];
?>
$result = mysqli_query("SELECT COUNT(user) AS user_count FROM game_paths WHERE user='$user'");
$result_array = mysql_fetch_assoc($result);
$user_count=$result_array['user_count'];
Please use mysqli_ instead of mysql_ as its deprecated in the new version
Related
I want to show the count of users which have the status 1 (see code) within PHP MySQL.
<?php
// something like this
$result = mysqli_query($mysqli, "SELECT COUNT(*) FROM users WHERE status = '1'");
echo "$result";
?>
Try this:
$query = "SELECT COUNT(*) as countvar FROM users where status = '1'";
$result = mysqli_query($con,$query);
$row = mysqli_fetch_array($result);
$count= $row['countvar '];
I have a MySql table "MyTable" with the column "order"
order
-----
3
4
2
1
I want to get the highest number. The sql statement works well inside MySql:
SELECT MAX(order) FROM MyTable"
But I do not know how to use it with php and echo it? Something like:
$result = mysqli_query($con, "SELECT MAX(order) FROM MyTable");
$result = mysqli_query($con, "SELECT MAX(order) FROM MyTable");
$row = mysqli_fetch_array($result);
echo $row[0];
If you don't provide an alias to a MySQL function it will be shown as you've written it:
$row = mysqli_fetch_array($result);
echo $row['MAX(order)'];
What you can do is write something like:
$result = mysqli_query($con, "SELECT MAX(order) as 'max' FROM MyTable");
$row = mysqli_fetch_array($result);
echo $row['max'];
Which is using an alias.
Hope this helps!
Alternatively you can use ORDER BY DESC and Limit result to 1 this way you can easily get the maximum of an column .
$result=$con->query("SELECT order FROM MyTable ORDER BY order DESC LIMIT 1");
$row=mysqli_fetch_array($result);
echo $row['order'];
Or using MAX
$result = mysqli_query($con, "SELECT MAX(order) FROM MyTable");
$row=mysqli_fetch_array($result);
echo $row[0];
This tutorial is directly from tizag, using the MySql Max function
MySQL Aggregate Functions - MAX()
// Make a MySQL Connection
$query = "SELECT type, MAX(price) FROM products GROUP BY type";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "The most expensive ". $row['type']. " is $" .$row['MAX(price)'];
echo "<br />";
}
Here's my answer:
$host = "localhost";
$username = "your_username";
$password = "your_password";
$db_name = "your_db_name";
$connection = mysql_connect($host, $username, $password) or die ("Error:: [1]");
mysql_select_db($db_name, $connection) or die ("Error:: [2]");
$query = "SELECT `order` FROM `MyTable` order by `order` desc";
$res = mysql_query($query, $connection);
$row = mysql_fetch_array($res);
print $row[0];
With this query , you always have the highest value of the give column.
Why isnt this working?
mysql_select_db('a2943462_Pages');
$num_rows = mysql_query("SELECT COUNT(*) FROM PagesInfo");
echo $num_rows;
mysql_query("INSERT INTO `a2943462_Pages`.`PagesInfo`(`ID`, `Title`, `Video`, `Posted`) VALUES ('".$num_rows."', '".$Title."', '".$Embed."', '".$name."')");
printf("Last inserted record has id %d\n", mysql_insert_id());
Because it now echoes:
Resource id #9
but i tought it would echo 4 instead because i have 3 rows inside the Table
Change this:
$num_rows = mysql_query("SELECT COUNT(*) FROM PagesInfo");
To this:
$num_rows = mysql_result(mysql_query("SELECT COUNT(*) FROM PagesInfo"), 0);
This is the PHP method:
$sql="SELECT * FROM pages_info";
$result = mysql_query($sql, $con) or die (echo "$sql" );
$rows = mysql_num_rows($result);
you should do two queries:
first query what you want to show:
SELECT SQL_CALC_FOUND_ROWS * FROM PagesInfo
and second query to get number rows returned:
SELECT FOUND_ROWS()
The in your insert the number of rows returned
The query in php is
$rows = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM PagesInfo");
$rs = mysql_query("SELECT FOUND_ROWS()");
$r = mysql_fetch_row($rs);
mysql_free_result($rs);
$row_num = $r[0];
I am trying to store a mysql value into a php variable. I have the following query which I know works. However, I the value for $count is always 0. Can someone explain what I need to do to get the count value? The count should be the count of x's w here name_x=.$id.
$query = "SELECT COUNT(name_x) FROM Status where name_x=.$id.";
$result = mysql_query($query);
$count = $result;
Is first letter in table name is really capital. Please check it first.
or Try :
$query = "SELECT COUNT(*) as totalno FROM Status where name_x=".$id;
$result = mysql_query($query);
while($data=mysql_fetch_array($result)){
$count = $data['totalno'];
}
echo $count;
$query = "SELECT COUNT(*) FROM `Status` where `name_x`= $id";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$count = $row[0];
please try it
$query = "SELECT COUNT(*) FROM Status where name_x=$id";
$result = mysql_query($query);
$count = mysql_result($result, 0);
You are missing single quotes around $id. Should be
name_x = '" . $id . "'";
I am able to get both the value and row of the mysql query result.
But I am struggling to get the single output of a query. e.g.:
$result = mysql_query("SELECT COUNT(*) FROM Students;");
I need the result to display. But I am not getting the result.
I have tried with the following methods:
mysql_fetch_assoc()
mysql_free_result()
mysql_fetch_row()
But I didn't succeed to display (get) the actual value.
You need to alias the aggregate using the as keyword in order to call it from mysql_fetch_assoc
$result=mysql_query("SELECT count(*) as total from Students");
$data=mysql_fetch_assoc($result);
echo $data['total'];
If you only need the value:
$result = mysql_query("SELECT count(*) from Students;");
echo mysql_result($result, 0);
$result = mysql_query("SELECT COUNT(*) AS `count` FROM `Students`");
$row = mysql_fetch_assoc($result);
$count = $row['count'];
Try this code.
Please start using PDO.
mysql_* is deprecated as of PHP 5.5.0 and will be removed entirely in 7. Let's make it easier to upgrade and start using it now.
$dbh = new \PDO($dsn, $user, $password);
$sth = $dbh->prepare('SELECT count(*) as total from Students');
$sth->execute();
print_r($sth->fetchColumn());
here is the code for showing no of rows in the table with PHP
$sql="select count(*) as total from student_table";
$result=mysqli_query($con,$sql);
$data=mysqli_fetch_assoc($result);
echo $data['total'];
$num_result = mysql_query("SELECT count(*) as total_count from Students ") or exit(mysql_error());
$row = mysql_fetch_object($num_result);
echo $row->total_count;
You can as well use this and upgrade to mysqli_ (stop using mysql_* extension...)
$result = mysqli_query($conn, "SELECT COUNT(*) AS `count` FROM `Students`");
$row = mysqli_fetch_array($result);
$count = $row['count'];
echo $count;
With mysql v5.7.20, here is how I was able to get the row count from a table using PHP v7.0.22:
$query = "select count(*) from bigtable";
$qresult = mysqli_query($this->conn, $query);
$row = mysqli_fetch_assoc($qresult);
$count = $row["count(*)"];
echo $count;
The third line will return a structure that looks like this:
array(1) {
["count(*)"]=>string(4) "1570"
}
In which case the ending echo statement will return:
1570
For mysqli users, the code will look like this:
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
$result = $mysqli->query("SELECT COUNT(*) AS Students_count FROM Students")->fetch_array();
var_dump($result['Students_count']);
or:
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
$result = $mysqli->query("SELECT COUNT(*) FROM Students")->fetch_array();
var_dump($result[0]);
$db = new PDO('mysql:host=localhost;dbname=java_db', 'root', 'pass');
$Sql = "SELECT count(*) as `total` FROM users";
$stmt = $db->query($Sql);
$stmt->execute();
$total = $stmt->fetch(PDO::FETCH_ASSOC);
print '<pre>';
print_r($total);
print '</pre>';
Result:
You need to alias the aggregate using the as keyword in order to call it from mysqli_fetch_assoc
$result=mysqli_query($conn,"SELECT count(*) as total from Students");
$data=mysqli_fetch_assoc($result);
echo $data['total'];
$howmanyuser_query=$conn->query('SELECT COUNT(uno) FROM userentry;');
$howmanyuser=$howmanyuser_query->fetch_array(MYSQLI_NUM);
echo $howmanyuser[0];
after the so many hours excellent :)