Using mysql MAX() with PHP fetching data from database - php

I have some problems here.
I tried fetching a data from column(which datatype is float) with the highest number and displays it. But it only shows the count of row(which is 1)
how to I get the exact 'Number' that I need?
PHP
<?php
$sql= mysqli_query($con, "SELECT MAX(payment_price) FROM client_record WHERE payment_status ='Settled'");
$row = (int)mysqli_fetch_array($sql);
echo $row;
?>
What it shows:
1
What I need to see:
//the exact price that MAX() fetched
20,000

As function you use, has a name mysqli_fetch_array, this means that result of this function is array. Casting array type to int type doesn't do what you expect.
Instead, what you need to use is just:
$row = mysqli_fetch_array($sql);
print_r($row);
// what you need is something like
echo $row['MAX(payment_price)'];

Try the following:
<?php
$sql= mysqli_query($con, "SELECT MAX(payment_price) as max FROM client_record WHERE payment_status ='Settled'");
$row = mysqli_fetch_array($sql);
echo $row['max'];
When you convert an array into an int it only converts it as a boolean value. Not the value of the array. So, use the index 'max' to get the value you want, as you can see I edited the MySql query to add as max it will provide the value for 'max'.

Related

mysqli_fetch_row() not working as expected

Am I using mysqli_fetch_row() the right way ??
I know the customer name and I wanna find out the Env_lines value in that same record. There is only one record with that customer name. I'm not getting an output when i do echo $env_lines. What might be the problem?
<?php require_once("config.php") ?>
<?php require_once("functions.php") ?>
test
<?php
$customer = 'Bickery';
echo ' <br> 1)'.$customer;
$sql_customer = "SELECT * FROM `Customer` WHERE Cust_Name = '$customer' LIMIT 0,1";
echo ' <br> 2)'.$sql_customer,'<br>';
$customer_selection = mysqli_query($conn,$sql_customer);
var_dump($customer_selection);
$customer_row = mysqli_fetch_row($conn, $customer_selection);
$env_lines = $customer_row["Env_Lines"];
echo ' <br> 3)'.$env_lines;
?>
https://gist.github.com/anonymous/520319ac72e7f08419c4
Manual Says
mixed mysqli_fetch_row ( mysqli_result $result )
Does that show anywhere that you need to give it your connection reference? No
Remove your first parameter from here
$customer_row = mysqli_fetch_row($conn, $customer_selection);
^
Then
Fetches one row of data from the result set and returns it as an enumerated array, where each column is stored in an array offset starting from 0 (zero). Each subsequent call to this function will return the next row within the result set, or NULL if there are no more rows
That means this is not the right function to fetch an associative array, mysqli_fetch_assoc is (amongst others).
$customer_row = mysqli_fetch_assoc($customer_selection);
And oh, PHP guys on SO think this is our duty; here it goes
How can I prevent SQL injection in PHP?

PHP function issues with array

I have a postgres table with four columns labelled dstart which is date data type,
dend which is also a date data type, dcontract which is a date data type and id which is a integer. I am trying to run a php code to get the data using an array and use it in the body of my application. But when I test the array and try to echo some values... My browser just displays the word array... Is there anyway I can be able to retrieve the data or fix this code? Please see code below
<?php
function getLiveDate($campid)
{
global $conn,$agencies;
$return=array(
'livedate'=>'',
'campid'=>'',
'enddate'=>'',
'dateContract'=>'',
);
$sql = "SELECT id, dcontract, dstart, dend
FROM campaigns
WHERE id = '".$campid."' ORDER BY dstart DESC LIMIT 1";
$r = pg_query($conn,$sql);
if ($r)
{
$d = pg_fetch_array($r);
if (!empty($d))
{
$return['livedate'] = $d['dstart'];
$return['campid'] = $d['id'];
$return['enddate'] = $d['dend'];
$return['dateContract'] = $d['dcontract'];
}
}
#pg_free_result($r);
return $return;
}
I am pretty sure, your array $d is "multi-dimensional" and pg_fetch_array() returns an array of arrays, because the result of SQL queries in general may contain multiple rows. You limited it to one row, but you certainly get the correct values by assinging $return['livedata'] = $d[0]['dstart']; or $return['livedata'] = $d['dstart'][0]; and so on (I am not familiar with that particularly function for I usually use MySQL instead of Postgre).
Besides, try echoing your data by means of print_r() instead of echo.
The $return variable is an array, if you want shows the content, you must use print_r or var_dump not echo.

Array sum giving wrong value PHP

What is wrong with this code?
$data455 = mysql_query("SELECT ROUND(SUM(gainloss), 4) FROM stats WHERE MONTH(date) = MONTH(CURRENT_TIMESTAMP) AND winloss = 'Win'");
$info455 = mysql_fetch_array($data455);
$testarray = array_sum($info455);
echo $testarray;
It does not display the correct sum. I want to get the sum of the last column where 'winloss' = 'win'
the output $testarray gives is 79.5843, but the correct sum is 41.6609.
Here is what my database looks like.
Try
mysql_fetch_assoc($data455);
Insted of
mysql_fetch_array($data455);
May this helps you because mysql_fetch_array returns both associative and indexing array you gets mixed result through it.
You don't have to use array_sum as in your query you already have the sum
$data455 = mysql_query("SELECT ROUND(SUM(gainloss), 4) AS total FROM stats WHERE MONTH(date) = MONTH(CURRENT_TIMESTAMP) AND winloss = 'Win'");
$info455 = mysql_fetch_array($data455);
echo $info455['total']; //this should give you the desired result
mysql_fetch_array by default returns the data from mysql as a numeric and associative array. You are essentially doubling your value by doing this. Change to mysql_fetch_assoc() and see what your result is.
http://php.net/manual/en/function.mysql-fetch-array.php
It is looks a problem of array sum. Looking at your SQL and the code line after that
$info455 = mysql_fetch_array($data455);
variable $info455 contains array something like
$info455['ROUND(SUM(gainloss), 4)'] = <some value>
$info455[0] = <some value>
So here your variable only holding one value twice. so array_sum will double the value. First try to see your SQL is giving the correct result and how many rows. If you want multiple rows from SQL and then sum using PHP then you have to iterate on the resultset

How to do I get PHP and MySQL to return an integer using the MAX() function?

I have a database with a column of only numbers and i need to use php to access it and use the MAX() function to get the largest number, when i use the max function and then try to "echo" the value it says it isn't a string but an object. How would i fix this?
$nextnumber=mysqli_query($con,"SELECT MAX(videonumber) AS maxnum FROM videos");
echo ($maxnum);
fetch the row/object from $nextnumber and then print the associated value with maxmum. For an example,
$nextnumber=mysqli_query($con,"SELECT MAX(videonumber) AS maxnum FROM videos");
$row = $nextnumber->fetch_assoc();
echo $row['maxnum'];
mysqli returns a result object, you need to extract the value from there:
http://www.php.net/manual/en/class.mysqli-result.php
i suggest you use fetch_assoc() to get it with the expected name
$row = $result->fetch_assoc();
mysqli_free_result($result);
then you can access the value by name and print it:
echo($row["maxnum"])

how to +1 to the MAX record from a field

.i have the following code:
$task_id = mysql_query("SELECT MAX(task_id) from it_task");
echo $task_id;
.what I want to do is to get the highest value from a field named task_id on the table it_task. Instead of getting a result which is an integer I get "Resource id #5". how do i get around this?
mysql_query generates a resource which you then have to read using another mysql_ function. In this case you'll need a 2nd line to read from the query's result:
$result = mysql_query("SELECT MAX(task_id) from it_task");
$task_id = mysql_result($result, 0, 0); // get the first row, first column
echo $task_id;
Any problem here??? In my postgres sql, I can have:
select max(task_id)+1 as max from it_task;
Yes, mysql_query always returns a resource (unless it returns false), which you have to work on using one of the mysql_ functions like mysql_fetch_assoc.

Categories