JDev

Code, mysqlJune 25, 2007 7:36 pm

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. 

Code, php, mysqlMarch 5, 2007 4:54 pm

You know mysql is not enabled if you get the following error:

Fatal error: Call to undefined function: mysql_connect()

Here is an article that describes how to fix it.

php, mysqlOctober 21, 2006 7:31 pm

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, mysqlSeptember 18, 2006 11:56 am

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. 

mysqlJuly 31, 2006 5:25 pm

How to limit the number of records retrieved with a select statement: 

    Select * from myTable LIMIT 10

mysqlJune 7, 2006 1:05 pm

    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();