Query the database where used for loops
I have a problem with calling the database where I used to for loops.
database query 1
SELECT id,qtyslot FROM rack WHERE theid = '1'
Eg id = 2
andqtyslot = 12
We create for loops
for ($x = 1; $x <= 12; $x++) {
Here I am calling the database with a slot id of 1 or 2.4 (with a comma from 2 to 4 slots), etc.
HOW TO CHOOSE
SELECT * FROM books WHERE idrack = '2' AND slot"......." // $x
Database structure as shown below.
----------------------- | id | slot | name | ----------------------- | 1 | 1 | name | ----------------------- | 2 | 2.4 | name | -> How to define this with the above loop ($ x) ----------------------- | 3 | 5.6 | name | -> How to define this with the above loop ($ x) ----------------------- | 4 | 7 | name | -----------------------
And the result like this in the table
----------------------------------------- | loop qtyslot | In slot | remark | ----------------------------------------- | 1 | name slot 1 | remark | ----------------------------------------- | 2 | | | --------------- | | | | 3 | Name slot 2.4 | remark | --------------- | | | | 4 | | | ----------------------------------------- | 5 | Name in | | --------------- | slot 5.6 | remark | | 6 | | | ------------------------------------------ etc up to 12
Can anyone help me.
+3
source to share
1 answer
You can use SUBSTRING_INDEX in mysql. Try the following:
SELECT * FROM books WHERE idrack = '2' AND $x BETWEEN SUBSTRING_INDEX(slot, ',', 1) and SUBSTRING_INDEX(slot, ',', -1)
+1
source to share