phpchick Posted December 3, 2011 Share Posted December 3, 2011 When I run 'select 1700-price as blah from goldclose as t2 order by dayid desc limit 1' by itself in mysql I get a numerical result: one row, one column. In my php script, the 1700 is actually a variable. so here it is $changequery = sprintf("select $goldprice-price as change from goldclose order by dayid desc limit 1"); $change = mysql_query(changequery); while ($row = mysql_fetch_array($change)) { printf("$row[0]"); } mysql_free_result($changeresult); I get the following error, Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/root/fuzzy/htmlmain4.php on line 99 Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in <b>/root/fuzzy/htmlmain4.php on line 103 Not sure why? All i want is to get the result of that select statement into a variable such as $change Quote Link to comment https://forums.phpfreaks.com/topic/252398-setting-a-variable-equal-to-the-result-of-a-mysql-select-statement/ Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 This means mysql_query is returning FALSE. Echo out $changequery and ensure it's a valid query. mysql_error will also help you with this. Quote Link to comment https://forums.phpfreaks.com/topic/252398-setting-a-variable-equal-to-the-result-of-a-mysql-select-statement/#findComment-1294033 Share on other sites More sharing options...
jcbones Posted December 3, 2011 Share Posted December 3, 2011 $goldprice most likely doesn't hold what you think it does. Quote Link to comment https://forums.phpfreaks.com/topic/252398-setting-a-variable-equal-to-the-result-of-a-mysql-select-statement/#findComment-1294040 Share on other sites More sharing options...
scootstah Posted December 3, 2011 Share Posted December 3, 2011 I think you want something more like this: select goldprice as change from goldclose WHERE goldprice = $goldprice order by dayid desc limit 1 Quote Link to comment https://forums.phpfreaks.com/topic/252398-setting-a-variable-equal-to-the-result-of-a-mysql-select-statement/#findComment-1294041 Share on other sites More sharing options...
phpchick Posted December 3, 2011 Author Share Posted December 3, 2011 i fixed it with this $changequery = sprintf("select $goldprice-price as change1 from goldclose order by dayid desc limit 1"); $changeget = mysql_query($changequery); $changearray = mysql_fetch_array($changeget); printf("$changearray[0]"); the problem was change can't be used as the name of a column. Quote Link to comment https://forums.phpfreaks.com/topic/252398-setting-a-variable-equal-to-the-result-of-a-mysql-select-statement/#findComment-1294044 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 To get around this, you can use backticks. SELECT `change` FROM `goldclose` ORDER BY `dayid` Quote Link to comment https://forums.phpfreaks.com/topic/252398-setting-a-variable-equal-to-the-result-of-a-mysql-select-statement/#findComment-1294048 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.