开发者

Using php and calling values from mySQL I am trying to call every 16 rows that contain the METAL GOLD

Here is the code I am tring to use:

<?php
function get_silver_time()
{
     $goldquery = mysql_query("SELECT * 
FROM ( 
    SELECT 
        @row := @row +1 AS rownum, time,metal
    FROM ( 
        SELECT @row :=0) r, metal_price 
    ) ranked 
WHERE rownum % 16 = 1
WHERE metal= 'GOLD'  
"); 


         while($result = mysql_fetch_array( $goldquery )) 
 { 

    echo "'" . date('g A', strToTime($result['time'])) . "'" . ", " ;  
 } 

         }
?>

This Returns This Error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/bellsnet/public_html/chart.php on line 125

Without trying to filter out the METAL type my code seems to work:

<?php
function get_silver_time()
{
     $goldquery = mysql_query("SELECT * 
FROM ( 
    SELECT 
        @row := @row +1 AS rownum, time,metal
    FROM ( 
        SELECT @row :=0) r, metal_price 
    ) ranked 
WHERE rownum % 16 = 1

"); 


         while($result = mysql_fetch_array( $goldquery )) 
 { 

    echo "'" . date('g A', strToTime($result['time'])) . "'" . ", " ;  
 } 

         }
?>

And it returns this:

'1 AM', '11 PM', '1 AM', '3 AM', '6 AM', '8 AM', '10 AM', '12 PM', '2 PM', '4 PM', '6 PM', '8 PM', '10 PM', '12 AM', '2 AM', '4 AM', '6 AM', '8 AM', '10 AM', '12 PM', '2 PM', '4 PM', '6 PM', '8 PM', '10 PM', '12 AM', '2 AM'开发者_C百科, '4 AM', '6 AM', '8 AM', '10 AM', '12 PM', '2 PM', '4 PM',

Any Help Would Be Appreciated! Thanks :)


After calling mysql_query you should always check if query was successful:

if(!$goldquery){
    die(mysql_error());
}

This will print you that you have error in your SQL statement. In your case it is doubled WHERE keyword, conditions in where clause should be separated by AND or OR, depending on what you want to check.

But if You want to select every 16th row only from GOLD, your second WHERE condition should be moved into ranked temp table, so it should look like this:

$goldquery = mysql_query("SELECT * 
FROM ( 
    SELECT 
        @row := @row +1 AS rownum, time, metal
    FROM 
        (SELECT @row :=0) r,
        metal_price 
    WHERE
        metal= 'GOLD'
    ) ranked 
WHERE rownum % 16 = 1
");


Wouldn't it make sense, then, to filter the FROM sub-query on the metal you want?

SELECT * FROM ( 
    SELECT 
        @row := @row +1 AS rownum, time,metal
    FROM ( 
        SELECT @row :=0) r, metal_price 
    WHERE metal = 'GOLD'
) ranked 
WHERE rownum % 16 = 1

Then the "ranked" table should return a result set containing only GOLD rows, and the rownum should count only those rows, allowing you to select every sixteenth GOLD row with your modulus filter.


You have a syntax error in your WHERE clause.

 $goldquery = mysql_query("SELECT * 
FROM ( 
    SELECT 
        @row := @row +1 AS rownum, time,metal
    FROM ( 
        SELECT @row :=0) r, metal_price 
    ) ranked 
WHERE rownum % 16 = 1
AND metal= 'GOLD'  
");

Note the change in the line containing 'GOLD' to use AND instead of WHERE.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜