Select Random Set Of Records from Mysql
To select a random set of records from mysql, use the following statement:
SELECT * FROM table_name ORDER BY RAND() limit 5;
A random set of 5 records should be returned by the query.
Select Random Set Of Records from Mysql
To select a random set of records from mysql, use the following statement:
SELECT * FROM table_name ORDER BY RAND() limit 5;
A random set of 5 records should be returned by the query.
Enable MySql module in PHP On Windows
You know mysql is not enabled if you get the following error:
Here is an article that describes how to fix it.
Search for full words in mysql with php
The easiest way to search for full words in mysql is to use regular expressions. According to this article you need to use these markers - [[:<:]], [[:>:]] -
that stand for word boundaries.
First, build a variable from the search parameter :
$val = addslashes($_GET[’search’]);
Construct a regular expression pattern:
$reg =’[[:<:]]’. $val .’[[:>:]]’ ;
Finally, build a sql statement:
Select comments from table1 where comments REGEXP ‘$reg’
So far, it has worked for me.
PHP and Mysql special characters
One can use the php function mysql_escape_string() to construct mysql queries that contain special characters like quotes, backslashes, etc. This function handles sinqle quotes well but does not seem to handle double quotes appropriately.
I got better results with the addslashes() function that handles both single and double quotes very well.
How to limit the number of records retrieved with a select statement:
Select * from myTable LIMIT 10
Here is how I use the MySql date field.
I created a field of date type in the existing table as follows:
ALTER TABLE tableName ADD COLUMN crdate DATE;
The current date is inserted using the now() function:
INSERT INTO tableName (field1, crdate) VALUES (’value1′, NOW())
Then, the rows with the current date are selected using the curdate() function:
SELECT * FROM tableName WHERE crdate = CURDATE();