I am attempting to perform the following query:
SELECT SUM(cash) AS total_cash FROM users
The query should give me back a SUM() of the total cash which I've paid out to users in the form of a field named total_cash.
My HTML/PHP is as follows:
<p class="cash_count">
$<?
$total_cash = $db->GetNumRows($db->Query("SELECT SUM(cash) AS total_cash FROM users"));
echo number_format($total_cash);
?>
</p> paid out!
I know GetNumRows will always show 1 as there's only 1 row, but I don't know what to use instead, can anybody guide me?
You are using some class to connect to database - you have not specified anything about that class. If $db->Query() returns the actual mysql resource, you can do this:
$res=$db->Query("....");
$r=mysql_fetch_object($res);
echo $r->total_cash;
Yes, there's only 1 row, because your query ask for the SUM, and returns only 1 row with the sum.
You can do 2 things, or quit the SUM of the SQL or quit the $db->GetNumRows.
Related
I have this JSON values in MySQL database
Conditions
1- >
[{"offer_id":"158","offer_start":"2017-12-17 09:21:27","offer_ends":"2017-12-17 10:21:27"},{"offer_id":"167","offer_start":"2017-12-17 12:28:57","offer_ends":"2017-12-17 12:58:57"}]
2 ->
[{"offer_id":"170","offer_start":"2018-01-17 04:26:26","offer_ends":"2018-01-17 05:11:26"},{"offer_id":"167"}]
3 ->
[{"offer_id":"170","offer_start":"2017-12-11 20:49:12","offer_ends":"2017-12-11 20:49:12"}]
3 ->
[{"offer_id":"170"}]
So I need to get the nth offer_ends value like "2017-12-11 20:49:12" using mysql query
Considering that you need to find the data from table1 where the column name offers. You need to fetch data where the "offer_ends" has value 2017-12-11 20:49:12
SELECT JSON_CONTAINS(offers, '2017-12-11 20:49:12', '$.offer_ends')
FROM table1;
Get all the records:
SELECT offers, JSON_EXTRACT(offers, "$.offer_ends")
FROM table1
If you want then you can apply the order by on any column and then get the Top 1 record to get the last record.
Here i have one code which get all the offer_ends value by using MySQL -> SELECT REPLACE(REPLACE(JSON_EXTRACT('[{"offer_id":"23","offer_start":"2017-12-11 20:49:12","offer_ends":"2017-12-11 20:49:12"},{"offer_id":"23","offer_start":"2017-12-11 20:49:12","offer_ends":"2017-12-11 20:49:12"}]', '$[*].offer_ends'),'[',''),']','') as offer_dates
and the result is -> "2017-12-11 20:49:12", "2017-12-11 20:49:12" which shows all offer end dates
So i need to find the nth offer_ends value like this -> "2017-12-11 20:49:12"
Guys i found the solution
SELECT JSON_EXTRACT('[{"offer_id":"158","offer_start":"2018-02-21 13:03:15","offer_ends":"2018-02-21 14:03:15"},{"offer_id":"170","offer_start":"2018-02-21 15:03:15","offer_ends":"2018-02-21 16:03:15"}]',CONCAT("$[",JSON_LENGTH('[{"offer_id":"158","offer_start":"2018-02-21 13:03:15","offer_ends":"2018-02-21 14:03:15"},{"offer_id":"170","offer_start":"2018-02-21 15:03:15","offer_ends":"2018-02-21 16:03:15"}]')-1,"].offer_ends")) as offer
Thank you guys
I'm fairly new to mysql and php development. I'm trying to understand why the mysqli_num_rows doesn't return what I expect - zero rows when there are definitely no records returned when I run the SQL statement from phpmyadmin.
I've searched around and I have failed to find a specific reason for why this isn't working.
Thank you in advance for any assistance.
An extract from my code is shown below:
if (isset($_GET['q']) && !empty($_GET['q'])){
$sql_wo = "SELECT MAX(wo_nbr) AS wo_nbr FROM workorders WHERE proj_id = '".$_GET['q']."'";
$result_wo = mysqli_query($connect,$sql_wo);
$rowCount_wo = mysqli_num_rows($result_wo);
echo "$rowCount_wo";
//returns 1 instead of 0???
When you use MAX(), COUNT(), etc you will always get one row returned even if the count or MAX is zero.
mysqli_num_rows() function returns the number of rows in a result set. And when you use COUNT() or MAX(), there would always be one row giving/showing you the result set.
I'm a beginner who has problems with PHP :(
I have a PHP function which shows all the rows from the database table. Now I have to create paging to show only limited number of rows per one page.
I have a problem with retrieving a COUNT result from query. I want to create a condition where PHP & MySQL use LIMIT if number of rows is bigger than needed on one page. The following code:
$count = "SELECT COUNT(*) FROM articles";
$countq = $db->query($count);
$countrs = mysql_fetch_array($countq);
echo $countrs;
should display a number of rows. However, it does not. What am I doing wrong? I want to see a result to make sure that everything else will work fine. But I can't get it working.
Error: mysql_fetch_array() expects parameter 1 to be resource, object given
$db contains database connection information (server, user...) and is working
Use PDO for MySQL query.
$db = new PDO('mysql:host=#YOUR HOST#;dbname=#YOUR DB#;charset=utf8', '#YOUR LOGIN#', '#YOUR PASSWORD#');
$query = $db->query('SELECT COUNT(*) AS count FROM articles');
$countq = $query->fetch();
$query->closeCursor();
echo $countq['count'];
I hope this will help you
You will have to set the limit in the query like
$count = "SELECT COUNT(*) FROM articles LIMIT 5,10";
where 5 is the starting point and 10 is the total number of results you want.
You mention: $db but not what $db is? i mean is it a database object class? this will work directly if you are using the a database class, and if that's the case the class will also have functions which will allow you to query data without using mysql_fetch_array (actually mysqli_fetch_array).
I've spent the past couple hours combing this site and the web for a solution to my problem. I'm certain it is an issue of syntax, and that it's a simple fix. When running the following query in phpmyadmin, I get the desired results. I'm joining two tables and displaying sums from each. I used phpmyadmin to generate php code but return 0 lines. Changing the SELECT query in php to a simple line which gathers only the sum and records from one table outputs multiple rows. It is only when I use the multiple SELECTS from the more advanced query that I get no results.
SELECT
od.orderID,
od.owed,
od.freight,
op.paid
FROM
(
SELECT
acct,
orderID,
SUM(price * qty) AS owed,
freight
FROM
old_details
GROUP BY
orderID
) AS od
INNER JOIN (
SELECT
orderID,
SUM(payment) AS paid
FROM
old_payment
GROUP BY
orderID
) AS op ON (op.orderID = od.orderID)
WHERE od.acct='AS3576E'
The PHP script is...
<?php
include_once "connect.php";
$cID = 'AS3576E';
$query = "SELECT od.orderID, od.owed, od.freight, op.paid
FROM ( SELECT acct, orderID, SUM(price * qty) AS owed, freight
FROM old_details
GROUP BY orderID ) AS od
INNER JOIN (
SELECT orderID, SUM(payment) AS paid
FROM old_payment
GROUP BY orderID ) AS op ON (op.orderID = od.orderID)
WHERE od.acct='$cID'";
$line=0;
$results = mysqli_multi_query($link, $query);
while($row=mysqli_fetch_array($results)){
echo "oid$line=$row[orderID]&";
echo "odate$line=$row[invDate]&";
echo "oowed$line=$row[owed]&";
echo "opaid$line=$row[paid]&";
echo "ofreight$line=$row[freight]&";
$line++;
}
echo "line=$line";
mysqli_close($link);
?>
I have tried mysqli_multi_query and mysqli_query. I have tried many different tweaks. There isn't a problem connecting to the database, as a change in the php query outputs rows of data. Any help would be greatly appreciated.
You don't need to use mysqli_multi_query here. Even though you use subselects, this is still only a single query with a single result set usuitable for use with mysqli_query(). If one were to use mysqli_multi_query, you would need to use mysqil_use_result() or mysqli_store_result() to work with the result set(s).
Also, you should get in the habit of actually checking for and logging errors when working with the database. Here you start trying to work with $results before you even bother to check whether it contains a valid resource for the result set.
I would expect the following to output the number "5", since there are 5 rows in the database with with item 68 and user 1. But instead I'm getting this output "12345".
$resultb4 = mysql_query("SELECT COUNT(comparedRating) FROM recComparedRating WHERE user1='1' AND itemID='68' GROUP BY itemID AND user1");
while($rowb4 = mysql_fetch_array($resultb4)){
$countcomparedratings=$rowb4['COUNT(comparedRating)'];
}
echo $countcomparedratings;
What am I doing wrong?
The reason you are getting 12345 is because your query is returning 5 results and your code to output the count is simply outputting the concatenation of the returned array from the query.
Without understanding your database structure, I'm guessing that the reason you're getting the '12345' has something to do with your GROUP BY clause. Use a program like MySQLWOrkbench to connect to your database and test out your query before you include it into your code. It is a time saving technique to debug your queries.
Also, I would alias the COUNT value so that you simply refer to the alias when you refer to your column names.
SELECT COUNT(comparedRating) as ratingCount FROM recComparedRating WHERE user1='1' AND itemID='68' GROUP BY itemID AND user1");