473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How would I write a regexp on a MySQL query

Hello,

Using PHP 4 and MySQL 4, how would I write a function such that it
takes a valid MySQL query, and a column on which to order the query,
and return a new query ordered by that column?

$query = formNewQuery($query, $orderByCol);

An assumption you can make is that the query will never have multiple
columns on which to order -- e.g. ("ORDER BY COL_A, COL_B"), however
the query could contain the "DESC" keyword -- "ORDER BY COL_A DESC".

Thanks, - Dave

Mar 1 '06 #1
2 1417
la***********@zipmail.com wrote:

Using PHP 4 and MySQL 4, how would I write a function such that it
takes a valid MySQL query, and a column on which to order the query,
and return a new query ordered by that column?

$query = formNewQuery($query, $orderByCol);

An assumption you can make is that the query will never have multiple
columns on which to order -- e.g. ("ORDER BY COL_A, COL_B"), however
the query could contain the "DESC" keyword -- "ORDER BY COL_A DESC".


require_once ('DB.php'); // PEAR::DB
//.. $db =& initialize connection, etc.

$query = 'SELECT * FROM table ORDER BY ! !';
$param = array('column_name', $order);
// where $order = 'ASC' or 'DESC' or ''
$res =& $db->query($query, $param);

// voilá!

//Marcin
Mar 1 '06 #2
Alternatively you can, ofcourse, do something like this:

function munchQuery($query, $col_name = '', $sort_order = '') {
if ( !empty($col_name)) {
$query .= ' ORDER BY '.$col_name;
if ( !(empty($sort_order)) {
$query .= ' '.$sort_order;
}
}
return $query
}

// but IMHO that's hard to maintain properly.
Mar 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Martin Lucas-Smith | last post by:
Can anyone point me to a regular expression in PHP which could be used to check that a proposed (My)SQL database/table/column name is valid, i.e. shouldn't result in an SQL error when created? ...
2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
2
by: Jon Maz | last post by:
Hi, I have written a page to test a function of mine that strips non-English accents and various other characters out of a given string. The function itself (called 'StripAll' in the code below)...
0
by: Tanamon | last post by:
Hello All, I am a MySQL newbie trying to write a query that selects file_name records possessing the highest numbered version for that unique file_name. I show sample data and two trial queries...
0
by: Ed Leafe | last post by:
I recently upgraded to 4.1 alpha (MySQL 4.1.0-alpha-standard-log) on my Linux server, and came across a problem with a query that had been working in 3.23 that no longer worked in 4.1a. I've...
4
by: Dave | last post by:
I have a MS sql 2000 db that needs to sync with a Mysql db. When a password is changed in the MS SQL table, I need to make sure that the same value is updated in the Mysql db. I dont have any...
1
by: Federico Bari | last post by:
Hello everyone, sorry for my bad English. I have to create a query under mySQL for a small research engine; I have to rate the results of a simple regular expression matches inside a text field....
1
by: Good Man | last post by:
Hi there If I have a "Filename" column in a database, and I want to grab the EXTENSION of this filename... can I do it in a query using REGEXP or something similar? I've got this going: ...
0
by: problematic | last post by:
Hello guys Actually i dont know does mysql support prepare with regexp but i have this query. prepare stm from "select count(id) as total from products where content regexp ']?]' "; set @a =...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.