TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-07-2007, 05:32 AM   #1 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default php within a query

I couldnt seem to figure this out on my own and google was not being my friend on this one.

Im trying to do a normal MySQL query but with a php if inside it. sounds simple enough ive done it in coldfusion multiple times just cant get the syntax for it in php. Example

PHP Code:
$result mysql_query(
select * 
from 
$dbName 
//Next Line 
Where if(isset(
$cat) { $cat = '1' } 
order by updated DESC, time DESC 
LIMIT 
$from$max_results"); 
That Where clause is my problem. but i think i got the point across of what im needing.
CoryMathews is offline  
Reply With Quote
Old 12-07-2007, 08:49 AM   #2 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 60
Thanks: 5
Jay is on a distinguished road
Default

You cannot have PHP code inside of a MySQL query, as far as I know. You will have to patch the query together (you can use whatever methods you like [EG: printf])

PHP Code:
/**
 * Hard-coded method
 */
$dbname 'categories';
$cat 1// Remember this _HAS_ to be an integer
$from 0;
$max_results 15;

/**
 * Accept input from url
 */
$dbname 'categories'// this shouldn't be in the url
$cat = isset( $_REQUEST['cat'] ) ? (int)$_REQUEST['cat'] : 1;
$from = isset( $_REQUEST['from'] ) ? (int)$_REQUEST['from'] : 0;
$max_results = isset( $_REQUEST['max_results'] ) ? (int)$_REQUEST['max_results'] : 15;

$result mysql_query('SELECT * FROM '$dbname .' WHERE cat = '$cat .' ORDER BY updated DESC LIMIT '$from .','$max_results); 
I hope that helps, this is primitive
Jay is offline  
Reply With Quote
Old 12-07-2007, 02:27 PM   #3 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

thanks jay but thats not really my problem. I dont have to worry about the url variables cause its just an include and the variables are getting passed in from the page itself. But as for the code the category is a word (ex Other) and it might not always be defined. So your code would word anytime it is defined but not when the $cat is undefined. you see what i mean?
CoryMathews is offline  
Reply With Quote
Old 12-07-2007, 02:36 PM   #4 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Can't you just test for the existence of $cat outside the query and then insert data if you have any...

PHP Code:
$szCatQuery = isset($cat) ? "WHERE $cat = 1" '';

$result mysql_query(
select * 
from 
$dbName 
//Next Line 
$szCatQuery
order by updated DESC, time DESC 
LIMIT 
$from$max_results"); 
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 12-07-2007, 03:20 PM   #5 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

thats what i was lookin for karl thanks. Never thought it would of been done like that.
CoryMathews is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:12 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design