SA-MP Forums Archive
MySQL loop - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: MySQL loop (/showthread.php?tid=607184)



MySQL loop - saffierr - 15.05.2016

How can I loop through my database with MySQL?
Lets say, I want to check if all players in the database their Org == 1?


Re: MySQL loop - KevinReinke - 15.05.2016

With an SQL statement (SELECT `Org` WHERE `Org` = '0' LIMIT 1 bla bla bla).

Then get the result's row count and if it's 0, then everyone's "Org" is set to 1.

Simple as that. Good luck!


Re: MySQL loop - saffierr - 15.05.2016

Well as I am not experienced with MySQL, I'd appreciate it to have an example.


Re: MySQL loop - zPain - 15.05.2016

You can do this:

PHP код:
SELECT ((SELECT COUNT(*) FROM Player WHERE Org=1) = (SELECT COUNT(*) FROM Player)) AS ret
// After you send the query, fetch the value of "ret". 
I believe there are more optimized ways.


Re: MySQL loop - Konstantinos - 15.05.2016

PHP код:
"SELECT IF(COUNT(*) = SUM(CASE WHEN Org=1 THEN 1 ELSE 0 END),1,0) FROM Player" 
Result will be either 1 or 0 depending on whether the total players is equal to the player that have "Org" set to 1.

EDIT: Use zPain's version. The speed is not even noticeable (~20-50 microseconds for 650 rows) but more readable and understandable.


Re: MySQL loop - Private200 - 15.05.2016

The query shall be something like:

Код:
SELECT * FROM `table` WHERE `Org` = '1'
At the part you want to loop through all the results you've got, you will have to switch to the next row after finishing reading one.

Код:
mysql_next_row
It all depends on the MySQL version you are currently using.