• Quick PHP MySQL Query help!
    5 replies, posted
Hey, I'm trying to setup a PHP script which will check my vBulletin's MySQL database in the table 'vb_paymenttransaction' and the column 'amount' but I want it to check between dates (There is a date column within the MySQL database with the following time format: YYYY-MM-DD HH:MM and then SUM up the total for that month. The problem? I can get it to do a sum off the whole table, but not when I use the dates. Here is my code: [php] // Date range information $start_time = '2013-02-01 00:00'; $end_time = '2013-02-28 00:00'; // Gathering the details, sorting it out and outputting it. $result = mysql_query('SELECT SUM(amount) AS amount_sum FROM vb_paymenttransaction WHERE dateline >= "$start_time" AND dateline <= "$end_time"'); $row = mysql_fetch_assoc($result); echo $sum = $row['amount_sum']; [/php] This is my first PHP script so I'm slightly unsure if I did it right or wrong (I know I didn't include the MySQL Connection details, I know that they're right)
The $start_time and $end_time won't be inserted query properly since you're using single quotes for the query.[php] $result = mysql_query("SELECT SUM(amount) AS amount_sum FROM `vb_paymenttransaction` WHERE `dateline` >= $start_time AND `dateline` <= $end_time"); [/php]
[QUOTE=SataniX;39599634]The $start_time and $end_time won't be inserted query properly since you're using single quotes for the query.[php] $result = mysql_query("SELECT SUM(amount) AS amount_sum FROM `vb_paymenttransaction` WHERE `dateline` >= $start_time AND `dateline` <= $end_time"); [/php][/QUOTE] With that being said, I'm produced with the following error when I execute that query: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /index.php on line 11. Where line 11 would be: [php]$row = mysql_fetch_assoc($result); [/php]
Stop using the old outdated MySQL library. Use mysqli or pdo instead. Also your error doesn't help as that's just telling you the query failed without giving you the reason from MySQL. Did you use his query exactly as shown?
[QUOTE=SteveUK;39600055]Stop using the old outdated MySQL library. Use mysqli or pdo instead. Also your error doesn't help as that's just telling you the query failed without giving you the reason from MySQL. Did you use his query exactly as shown?[/QUOTE] The problem is the same as the problem with OpenGL: All of the tutorials are really old.
Problem has been resolved, I moved over to the MySQLi library and everything seems to be working fine now, thank you both SteveUK and SataniX for your help!
Sorry, you need to Log In to post a reply to this thread.