1

So I created while loop

$tradetoken = mysql_query("SELECT tradeid FROM `bot_tasks` WHERE id=".$time."");
    $tradetokenr['tradeid'] = null;
    while(empty($tradetokenr['tradeid'])){
        $tradetokenr = mysql_fetch_array($tradetoken);
    }

So basically, if the tradeid is empty then it tries to get it again. Then I filled the tradeid, but I didn't got an response. It was just loading and then showed Internal Error 500

Aleks Kpur
  • 121
  • 1
  • 2
  • 11
  • *So basically, if the tradeid is empty then it tries to get it again.* So you want to invert the condition with `!` and set it first to something else than null. – Rizier123 Jun 19 '16 at 22:46

1 Answers1

0

You are doing the query and loop in a wrong way.

just do it like this:

$tradetoken = mysql_query("SELECT tradeid FROM `bot_tasks` WHERE id=".$time."");

while($result = mysql_fetch_array($tradetoken)){
    $tradetokenr = $result;
}
  • Thanks, but at first the tradeid is empty. If it's empty then it will try to keep getting it until there is something in. – Aleks Kpur Jun 19 '16 at 22:57
  • @AleksKpur why not select non empty values for `tradeid` in the query? (`WHERE tradeid !=''`) –  Jun 19 '16 at 23:08
  • Naah, it has to be empty until "i" fill it. – Aleks Kpur Jun 19 '16 at 23:09
  • @AleksKpur can't get what you are trying to accomplish? the record in the database has tradeid empty? – Muhammad Sumon Molla Selim Jun 19 '16 at 23:13
  • Basically, the tradeid is being created then it's empty. Then the while loop just keeps getting it. Then finally if I fill it, then it echo's the value from there. – Aleks Kpur Jun 19 '16 at 23:14
  • Okay. But here what actually you are trying to do? – Muhammad Sumon Molla Selim Jun 19 '16 at 23:15
  • Basically, it's steam trade offer. If bot sends the trade offer, then there will be the tradeid which I will be able to show to user. Im not sure how can I do the while loop right way :/ – Aleks Kpur Jun 19 '16 at 23:17
  • If you want to only fetch the results that has tradeid, then you can just modify your query as the way @Dagon mentioned. – Muhammad Sumon Molla Selim Jun 19 '16 at 23:19
  • Cant I just do it, it basically just keeps testing the one which has the ID, until I there "comes" the value? – Aleks Kpur Jun 19 '16 at 23:21
  • the query will run once, then php will loop the results -- its not waiting for a value to appear in the db to run, it does not work like that –  Jun 19 '16 at 23:22
  • Hmm.. Wait, is there then other way basically? – Aleks Kpur Jun 19 '16 at 23:24
  • But that's the point of while loop? It will keep doing it until the thing is reached which is wanted. – Aleks Kpur Jun 19 '16 at 23:24
  • no its not the point of the while loop. its while there is still data coming from the db based on the query that was just run. –  Jun 19 '16 at 23:24
  • while loop will run as long as the mysql_query returns true (meaning gets a row). – Muhammad Sumon Molla Selim Jun 19 '16 at 23:25
  • if you want to keep checking the db, you need to run a new query every X seconds. –  Jun 19 '16 at 23:27
  • Then could I put into the database 0, then while it's 0 it's continues doing it until, it gets different value? – Aleks Kpur Jun 19 '16 at 23:27
  • no, does not work like that. some different approaches: http://stackoverflow.com/questions/23192097/php-check-mysql-database-every-10-seconds-for-any-new-rows http://stackoverflow.com/questions/11232915/how-to-continuously-monitor-a-table-in-a-mysql-database-and-triger-a-php-script http://stackoverflow.com/questions/13758379/whats-the-best-practice-for-php-to-continuously-check-for-changes-in-the-databa –  Jun 19 '16 at 23:29
  • there's problem like if the rows have traderid like 0, 0, 2, 0, 0 - then after first two row loop will get the value 2 - and it will stop. so the rows having 0 values after the value 2 will remain unfetched. – Muhammad Sumon Molla Selim Jun 19 '16 at 23:33
  • I mean, that's why I have $time, so it only gets that for one table. – Aleks Kpur Jun 19 '16 at 23:38
  • Okay, so is there any way I could do something like that? – Aleks Kpur Jun 19 '16 at 23:46