I need some help here.
I have a mysql table called dictionary like this:
id word
1 test
2 hello
4 bye
7 werd
In php, I want to query the db so that I can get an array of the id field
so the array would list 1 2 4 7.
With this array, I want to run the array_rand function in php so I can get a random number from 1 2 4 7.
Can someone show me the proper mysql query to list the id field and return it into php as an array?
and how I could run that array with random array function.
No need shuffle this in php, - effective is to
use "SELECT id FROM table ORDER BY rand();"
than take all records and store id into array ...
Use the shuffle function
If you plan on using the random row and then querying for another, I would just randomize on the MySQL query. Get a count of the rows in the table, create a random integer somewhere between 0 and that count, and then use LIMIT in your MySQL query to return a random row.
If you plan on keeping it all in memory, then David's answer would work better.
Related
I need to essentially create a reverse-incremented column starting from a variable set by a user.
A number will be inserted via an HTML form/PHP into a table. After that I need the following rows to increment down from that number.
I'm open to doing this with SQL or PHP or anything else that might work, I just can't wrap my mind around an effective way to do it.
my table looks like this:
RecID---Input
1 7 <--input by user
2 6 <--number drops by 1
3 5
4 4
5 3
Thanks in advance!
If data is coming from a mysql database, you can use the ORDER BY clause.
Ascending order:
SELECT RecID, Input FROM dbname ORDER BY Input ASC
Descending order:
SELECT RecID, Input FROM dbname ORDER BY Input DESC
cannot comment yet.
I don't know exactly what you want but you could use a for loop from the end instead of incrementing the number you decrement it.
$value=array();
for($i=_user_input_;$i > 0;$i--){
$value[]=$i;
}
or
$value=array('placeholder');
for($i=_user_input_;$i > 0;$i--){
$value[]=$i;
}
unset($value[0]); // if you want index to start with one
hope this helps
i have a string field that contains a json string in my table. lets name that tbl_table
lets say it has these 3 records in it:
id json
----------------
1 [123,45,1]
2 [23,4,5]
3 [7,8,9]
now I want to fetch records that contain a number in their json field (using active records in Yii2), so:
$res = Table::find()->where(['like','json','23'])->all();
this query returns these records:
id json
----------------
1 [123,45,1]
2 [23,4,5]
this is not actually a wrong result but what I want is to return only the record with id=2. how can I do this?
and so for ['like','json','5'] or ['like','json','4'] and so on? the numbers are similar but I want the exact number to be match.
and another question I want the last question answer with change in this part ['like','json',[4,5,6]].
in other words I want to pass an array to like operator and it automatically make an OR operation for each element. is it somehow possible? if not how can I iterate on array and make multiple Or_like conditions with last question functionality without multi query executions ?
use expression to use mysql functions and use JSON_CONTAINS function
use yii\db\Expression;
$expression = new Expression('JSON_CONTAINS(fied, value)');
$now = (new \yii\db\Query)->select($expression);
p.s json_contains begins from version MySQL 5.7
refs https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html
http://www.yiiframework.com/doc-2.0/yii-db-expression.html
I am getting an array of 25 items from a database using SQL. I want to take 16 of the items and random to use. I thought if I could randomly sort the array I could then take the first 16 items.
Is there a way to randomly sort an array in php, or perform an SQL SELECT that returns to results in a random order?
You can do this in SQL:
<your select here>
order by rand()
limit 16;
So I have a table with 45 records (but can be dynamic) and I use mysql_fetch_array() to get the data from the database. What is the best way to output 5 records at a time? So I need to do record 1-5, then have a link for records 6-10, 11-15, and so on. I thought about doing something with array_chunk but not sure how to keep track of the record number. Thanks for hints.
To get the first 5 results form a table:
SELECT * FROM table ORDER BY table.column_name ASC LIMIT 0, 5
Selects from `table`
Ordered by the column name Ascending
Limit 0,5 selects the first 5 results, starting at 0.
Change LIMIT 0,5 to 5,5 to list results 6-10 (start at record 5, and continue for 5 records.)
Ordering is just good practice to ensure consistency. Under most circumstances set this to 'id' if you have an auto-increment 'id' column. If you want results sorted by date, order by a timestamp column. If you want data reversed, order by DESC.
You can keep track of where your queries are though PHP Sessions, Passing GET parameters, temporary database tables, and probably a few more I missed.
Other solution:
Use the array returned from the mysql_fetch_array() and utilite http://php.net/manual/en/language.types.array.php
The obvious disadvantage to this approach is the fact that it fetches all rows in the table. This is okay if you'll NEVER have more than a manageable number of rows. In your case, 45 should be fine, assuming they're not gigantic rows. This approach may also may useful if you want data pre-loaded.
I'd suggest using limits and incremental offsets in your query. Your first query would then be:
select * from TABLE limit 0,5;
Your link has a parameter referencing the next offset so the next query would be:
select * from TABLE limit 5,5;
And so on.
You need in your query LIMIT 0,5. Search web for php paginator.
Is it possible in php to return a specific row of data from a mysql query?
None of the fetch statements that I've found return a 2 dimensional array to access specific rows.
I want to be able to return only 1 specific row, kinda like mysql_result... except for entire row instead of 1 cell in a row.
I don't want to loop through all the results either, I already know how to do that I just thought that there might be a better way that I'm not aware of. Thanks
For example, mysql_data_seek() and mysqli_stmt_data_seek() allow you to skip forward in a query result to a certain row.
If you are interested in one certain row only, why not adapt the query to return only the row you need (e.g. via a more specific WHERE clause, or LIMIT)? This would be more resource-effective.
You should add LIMIT to your mysql statement. And it will return only data you need. Like following:
-- returns 1 row after 2 row
SELECT * FROM table LIMIT 2, 1