sum(value) gives undefined index [duplicate] - php

This question already has answers here:
Get sum of MySQL column in PHP
(9 answers)
Closed 1 year ago.
I'm having trouble calculating the sum of one column in the database from the first 5 rows. My code is:
$mysqli=mysqli_connect($host,$user,$password,$database);
$sql = "SELECT SUM(medie) FROM (SELECT medie FROM stelewar WHERE nume='".$nume."' ORDER BY id DESC LIMIT 5) medie";
$result=mysqli_query($mysqli,$sql);
$row=mysqli_fetch_assoc($result);
$sumamedie= $row["medie"];
The last row of code throws the error "Undefined index: medie" and I cannot figure out why...
Any thoughts?

$mysqli=mysqli_connect($host,$user,$password,$database);
$sql ="(SELECT SUM(media) AS mysumvalue FROM stelewar WHERE nume='".$nume."' ORDER BY id DESC LIMIT 5)";
$result=mysqli_query($mysqli,$sql);
while($row=mysqli_fetch_array($result))
{
$sumamedie= $row["mysumvalue"];
}

Related

I want to know the total products in my product table but it shows an error , What will be the right syntax [duplicate]

This question already has answers here:
How to get count of rows in MySQL table using PHP?
(3 answers)
Closed 3 years ago.
I want to know the total products in my product table but it shows an error. What will be the right syntax?
here is my code:
$sql = "SELECT COUNT(product_name) FROM product";
$result=mysqli_query($this->conn,$sql);
$row = $result->fetch_object();
$neeraj = $row->COUNT(product_name);
echo $neeraj;
If I remember correctly, to get the count you should submit COUNT(…) query and get the first column of the result.
So, correct code would be look like this:
$sql = "SELECT COUNT(product_name) FROM product";
$result = $this->conn->query($sql);
$neeraj = (int) $result->fetch_row()[0];
echo $neeraj;

Getting wrong number when counting rows in PHP [duplicate]

This question already has answers here:
MySQLi count(*) always returns 1
(4 answers)
Closed 5 years ago.
I have a table called quote in my database with 5 rows. However when I try to count the rows it always returns 1 instead of 5. I am using the code below:
$connection = mysqli_connect("host","username","password","database");
$querya = "SELECT COUNT(id) FROM quote";
$resulta = mysqli_query($connection, $querya);
$max = mysqli_num_rows($resulta);
$srow = rand(1,$max);
<br /> There are <?php echo $max ?> number of rows
I am counting the id column which is a primary key and therefore never null. I have also tried it by using count(*) but get the same result. Where am I going wrong?
You can't pair mysqli_num_rows with count. If you want to use mysqli_num_rows you'd have to select * (which would be slow). Instead select count(*) as total, and use total.
If you want to use COUNT() in your query give it an alias that you can return:
$querya = "SELECT COUNT(`id`) AS `Total` FROM `quote`";
$resulta = mysqli_query($connection, $querya);
$row = mysqli_fetch_assoc($resulta);
echo $row['Total']; // identifier here is the same as the alias in the query

SQL, "WHERE IN" returns automatically sorted result [duplicate]

This question already has answers here:
Ordering by specific field value first
(8 answers)
Maintaining order in MySQL "IN" query
(2 answers)
Closed 5 years ago.
When I run my query with an WHERE IN statement, it seems like it automaticly sorts the product_id that are returned.
$SQL = "SELECT * FROM PIM WHERE product_id in (10,8,1,3)";
foreach($conn->query($sql) as $row) {
echo $row['product_id'] . "<br>";
}
Result:
1
3
8
10
I want them returned in the order they entered in (10,8,1,3)
Since in your original query you did not specify which order MySQL should use then is using ASC, Try using ORDER BY FIELD() like this:
SELECT * FROM PIM WHERE product_id in (10,8,1,3) ORDER BY FIELD(product_id, 10,8,1,3);
Check this great answer for more details.
Try:
select * from PIM where id in (1,3,8,10) order by find_in_set(id,'10,8,1,3');

Invalid use of group function with MAX [duplicate]

This question already has answers here:
How do I select an entire row which has the largest ID in the table?
(6 answers)
Closed 9 years ago.
My code gives me the following error:
Invalid use of group function
$query = mysql_query("SELECT `text` FROM `text` WHERE `id`=max(id)");
if(!$query)
die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['text'];
}
Where is my mistake?
If you want the row with highest id you could use:
SELECT text FROM text ORDER BY id DESC LIMIT 1
WHERE clauses affect individual rows, whereas HAVING clauses affect aggregations (results of GROUP BY clauses). Row criteria must be limited to the WHERE clause, aggregate functions (like MAX) must be used in HAVING clauses.
You could do this:
SELECT *
FROM text
WHERE id = (SELECT MAX(id) FROM text);

PHP, MySQL: how to select random records without RAND (attempt to avoid RAND()) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to request a random row in SQL?
Because of a huge table I cannot use MySQL function RAND() and trying to avoid using RAND() this way:
$a = mysql_fetch_array(mysql_query("SELECT MIN(id), MAX(id) FROM table WHERE category_id='".$var."'"));
$id = rand($a[0],$a[1]);
$id2 = rand($a[0],$a[1]);
Which doesn't work, because: in $a I have the biggest and smallest ID with the respective category_id - that's ok.
The problem brings the rows with $id and $id2 - because there are used the minimal and maximal ID values, but PHP function rand() will take some random value between minimum and maximum - and there is a big probability, that the picked out record will not belongs into category_id.
So I would like to ask you about a way, how could I fix this example.
THank you
You could try this:
$row = mysql_fetch_assoc(
mysql_query(
"SELECT count(*) as count FROM table WHERE category_id='$var'"));
$randomrow = rand(0, $row['count'] -1);
$q = mysql_query("
SELECT * FROM table
WHERE category_id='$var'
LIMIT 1 OFFSET $randomrow");

Categories