i am trying to echo the result of the SQL SELECT COUNT but it seems doesn't work for me, can anyone of you help me?
The error show that
mysql_fetch_array expects parameter 1 to be resources, boolean given
....
This is my sql
<?php
$query = ("SELECT COUNT(*) AS total FROM `errors` WHERE `sta` = 0");
$result = mysql_query($query);
$count = mysql_fetch_array($result);
echo $count['total'];
?>
Try to following code:
$query = ($con, "SELECT COUNT(*) AS total FROM errors WHERE sta = 0");
$count = mysqli_fetch_assoc($query);
echo $count['total'];
Hope will help you
You would use mysql_result() like shown below to retrieve the output as an int.
$query = mysql_query("SELECT COUNT(*) FROM `errors` WHERE `sta` = 0");
$result = mysql_result($query, 0, 0);
echo $result;
mysql_* extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0. Instead, either the mysqli or PDO_MySQL
extension should be used.
Instead of doing this, you can change your query and directly use mysql_num_rows as it also gives the count. And I guess you only need the count so it is better way, and here is the code :
<?php
$query = ("SELECT * FROM `errors` WHERE `sta` = 0");
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$count = mysql_num_rows($result);
echo $count; // this will give the total count
?>
// try this code...
$query = "SELECT * FROM `errors` WHERE `sta`='0'";
$result = mysql_query($query) or die (mysql_error());
$row2 = mysql_fetch_array($result);
$count= mysql_num_rows($result);
echo $count;
Related
How to get total number of row by using this function php mysql ?
i use this code for display data from mysql. It's work good,
Buy i want to know can i get total number of row by using this code ?
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>
Try this mysql_num_rows ($result)
Use this line of code
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
$number_of_rows = mysql_num_rows($result); // this will return the number of rows found by the $result query.
echo $number_of_rows;
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>
I'm not too good with PHP, but I know some other similar languages.
Now what I am trying to do is get how many rows are in a table. Here is my code.
$result = mysql_query("SELECT COUNT(*) FROM list");
$num_rows = mysql_num_rows($result);
echo $num_rows;
Now this code returns with 1, when there is actually 3. Why is that?
Thanks for any help.
Check This
$result = mysql_fetch_array(mysql_query("SELECT COUNT(*) as count FROM list"));
echo $result['count'];
Try like this:
$result = mysql_query("SELECT COUNT(*) FROM list");
$num_rows = mysql_fetch_object($result);
print_r($num_rows);
Note: Mysql_* functions are deprecated. Hence not recommended.
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT COUNT(*) as total_row FROM list");
echo "<table border='1'>
<tr>
<th>total_row</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['total_row'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
$result = mysql_query("SELECT COUNT(*) as total FROM list");
$num_rows = mysql_num_rows($result);
echo $num_rows; // output 1;
$rows = mysql_fetch_object($result);
echo $rows->total; // output 3;
You should do like this:
$result = mysql_query("SELECT * FROM list");
$num_rows = mysql_num_rows($result);
echo $num_rows;
If you want to get total number of row through COUNT, you can do like this:
$result = mysql_query("select count(*) FROM list");
$row = mysql_fetch_array($result);
echo $row[0];
It return number of rows count in single column, then only it return 1
Refer:
http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html
Only change your query
$result = mysql_query("SELECT * FROM list");
$num_rows = mysql_num_rows($result);
echo $num_rows;
It returns one row because, the query below returns only one value that is the number of rows.
$result = mysql_query("SELECT COUNT(*) FROM list");
I think this should help:
$result = mysql_query("SELECT COUNT(*) FROM list");
$num_rows = $result->fetchColumn();
echo $num_rows;
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) ) {
I am creating a function that will retrieve an id number from a table with multiple columns, put it in a variable which i want to use to send as a "claims number" in an email to a claimant and also echo on a confirmation screen. Here's what I have so far.
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
$claim_id = $result;
$result = mysql_fetch_array(mysql_query($sql));
$claim_id = $result['id'];
Supposing you are sure that you will receive 1 row.
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
$c = mysql_fetch_assoc($result);
$claim_id = $c['lname'];
NB*: And get ready for a lecture. Someone is going to tell you that mysql is depracated.
Start using MySQLi or PDO (recommended).
Try :
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$claim_id = $row[0];
or
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$claim_id = $row['id'];
}
$row = mysql_fetch_array($result);
$claim_id = $row['id'];
please note that you should also check for errors (e.g. if($result === FALSE)). Also, there are other ways to fetch data - I recommend looking at mysql_fetch_array, mysql_fetch_row, mysql_fetch_assoc and mysql_fetch_object
Also, don't rely on the statement always returning exactly one row - make sure that the result is as expected: mysql_num_rows
Give this a try:
$sql = "SELECT id FROM warranty_claim where lname='".$lname."'";
$result = mysql_query($sql);
$objResult = mysql_fetch_array($result);
$claim_id = $objResult['id'];
I want to get the number of rows in my MySQL table and store that number in a php variable. This is the code I'm using:
$size = #mysql_query("SELECT COUNT(*) FROM News");
$size ends up being "Resource ID #7." How do I put the number of rows directly into $size?
mysql_query returns a query resource id. In order to get values from it you need to use mysql_fetch_assoc on the resource id to fetch a row into an array.
$result = mysql_query("SELECT COUNT(*) FROM News");
$row = mysql_fetch_assoc($result);
$size = $row['COUNT(*)'];
You need to call mysql_fetch_row or one of its sister functions.
<?php
// untested
$result = #mysql_query("SELECT COUNT(*) FROM News");
// error handling
$row = mysql_fetch_row($result);
$count = $row[0];
?>
try the following:
$size = #mysql_query("SELECT COUNT(*) AS `total` FROM News");
$query = mysql_fetch_array($size);
echo $query['total'];
On a related note you can use the mysql_num_rows() function to obtain the number of rows from a given query. This is handy if you need to grab the data but also know the number of rows.
<?php
$result = #mysql_query("SELECT * FROM news");
$count = #mysql_num_rows($result);
?>