All I need to do, is count all fields of the table, where postid=38.
But my code always prints "1" on screen, no matter what number I write in postid=38.
<?
$consulta2 = mysql_query("select count(*) from $tabla_db4 where postid='38';");
$result2 = mysql_num_rows($consulta2);
echo (string) $result2;
?>
You can do either this:
<?
$consulta2 = mysql_query("select count(*) as count from $tabla_db4 where postid='38';");
$result2 = mysql_fetch_array($consulta2);
echo $result2['count'];
?>
OR
$consulta2 = mysql_query("select* from $tabla_db4 where postid='38';");
$result2 = #mysql_num_rows($consulta2);
echo $result2;
Solved! Please close.
$sql = mysql_query(
"SELECT * FROM `comentarios` WHERE `postid` =
$registro[id] ORDER BY `postid` DESC",$conexion_db);
$cuenta = mysql_num_rows($sql);
echo $cuenta;
You can try this if you want to know how many rows there are:
<?php
$query = "select count(*) as amount from $tabla_db4 where postid='38';";
$result = mysql_query($query);
if(!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
while($row = mysql_fetch_assoc($result)) {
echo $row['amount'];
}
mysql_free_result($result);
?>
Also, are you sure postid is a string, since you're putting it in between quotes. It looks like it might be an integer. Mind the datatypes.
Related
I can not seem to come over this easy problem.
I have the following code, where I select the column "status" from a table. There's three different values of "status", 0, 1 and 3.
I would like to count how many 0's there are, 1's and 2's, so that I can display them via <?php echo $accepted; ?> etc.
<?php
$sql = "SELECT status FROM applications";
$result = mysqli_query($db, $sql);
$row = mysqli_fetch_array($result, MYSQLI_NUM);
$array = array_count_values($row);
$pending = $array[0];
$accepted = $array[1];
$denied = $array[2];
?>
MYSQL_NUM changed to MYSQLI_NUM accordingly to comment, thank you.
A simple way is get these count using SQL
$sql = "SELECT `status`, COUNT(*) as cnt FROM applications GROUP BY `status`";
you have an array of so you should
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "status = " . $array['status'] . ' = ' . $array['cnt'] .'<br />';
}
I am trying to know if there is an existing data in the database, we I try to use existing username, based on my coding it should be "exists already", but the result is "proceed". Can you point out where I am wrong?
$query = mysql_query("SELECT * FROM `membership` WHERE `username` = '$username'");
if (mysql_num_rows($query) > 0){
echo "exists already";
}
else {
echo "proceed";
}
this should work
$str = "select SQL_CALC_FOUND_ROWS * from table where username=".$your_value;
$con=db_connect();
$result1 = mysql_query($str,$con);
$str1 = "SELECT FOUND_ROWS() as totalRecord";
$result2 = mysql_query($str1,$con);
$totalRecords = mysql_fetch_array($result2);
if ($totalRecords[0]>0){
echo "exists already";
}
else{echo "proceed";}
also try using mysqli or pdo as mysql is depricated...
Hey guys i want a sum of an mysql query and an php variable. Is there a function to do this? Here is what i tried:
$result3 = mysql_query($sql3);
while ($resultarray3 = mysql_fetch_array($result3)){
$Ergebnis = $Menge + $resultarray['Bestand'];
echo $Ergebnis;
}
Can anyone help me on this?
Edit: I want a sum of an php variable and an mysql query not of two sql tables!!!!
$menge = '';
$result3 = mysql_query($sql3);
while ($resultarray3 = mysql_fetch_array($result3)){
$menge = $menge + $resultarray3['Bestand'];
}
// result => $menge
If there is only one row you don't need the .=
$result3 = mysql_query($sql3);
while ($resultarray3 = mysql_fetch_array($result3)){
$Ergebnis .= $Menge + $resultarray3['Bestand'];//notice the change on this line
echo $Ergebnis;
}
A query to show a result contains SUM(column), so you could use:
$sql3 = "SELECT bestand, SUM(bestand) as menge FROM database GROUP BY bestand";
And then in PHP
$result3 = mysql_query($sql3);
while ($resultarray3 = mysql_fetch_array($result3)){
echo $resultarray['bestand'] . ' = ' . $resultarray['menge'];
}
You define $resultarray3 but then you use $resultarray without the three.
$Ergebnis = $Menge + $resultarray3['Bestand'];
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 . "'";
$result = mysql_query("SELECT
car.id car_id,
FROM
car
WHERE car.id= $id ");
How can I echo the query above?
You're passing the query directly to the function. Store it in a variable if you want to echo it:
$query = "SELECT
car.id car_id,
FROM
car
WHERE car.id= $id ";
echo $query;
$result = mysql_query($query);
$query = "SELECT car.id,car_id FROM car WHERE car.id= $id ";
$result = mysql_query($query)
while ($car_details = mysql_fetch_array($result)){
echo "$car_details[id], $car_details[car_id]\n";
}
The above answer would echo the results of the query, if you simply want to echo the query string itself, store it as a string first, pass the string into the mysql_query function, and echo the string:
$sql = "SELECT car.id car_id, FROM car WHERE car.id= $id";
echo $sql;
$result = mysql_query($sql);