vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Parameter value not passed correctly to a query (https://vborg.vbsupport.ru/showthread.php?t=73293)

sv1cec 12-25-2004 04:17 PM

Parameter value not passed correctly to a query
 
Gentlemen,

I am trying to use various parameters in an ORDER BY clause in a query, and I just came up with an issue I can't overpass.

Suppose we have the followings:

PHP Code:

$s11t='w.warned_time ASC';
$s12t='w.warned_time DESC'

These variables are then assigned to another variable, depending on the user's selection, in other words:

PHP Code:

if ($selection==1)
{
       
$orderby=$s11t;
}
if (
$selection==2)
{
       
$orderby=$s12t;


and the query which gets run after these lines, is:

PHP Code:

$get_warns=$DB_site->query("
select w.*,u.username as wusername,u.userid as wuserid,wt.*,wuser.username 
as warned_username_v,wuser.userid as warned_userid_v, ruser.username as 
removed_by_v from
"
.TABLE_PREFIX."warnings w
left join "
.TABLE_PREFIX."user wuser on(wuser.userid=w.warned_user)
left join "
.TABLE_PREFIX."user u on(u.userid=w.warned_by)
left join "
.TABLE_PREFIX."user ruser on(ruser.userid=w.removed_by)
left join "
.TABLE_PREFIX."warning_types wt on(wt.tid=w.warned_warning_id) ORDER BY ".$orderby." LIMIT $startat$perpage"); 

These work fine.

Now, I am trying to sort on a field, which does not exist as a column in any of the tables, but is calculated as below:

PHP Code:

$s14t='(w.warned_time+wt.warn_maturity*24*60*60) DESC'

Now, if I put the line above, right below the two other lines, and run the program, I get the following error:

Quote:

Database error in vBulletin 3.0.3:

Invalid SQL: select w.*,u.username as wusername,u.userid as
wuserid,wt.*,wuser.username as warned_username_v,wuser.userid as
warned_userid_v, ruser.username as removed_by_v from
warnings w
left join user wuser on(wuser.userid=w.warned_user)
left join user u on(u.userid=w.warned_by)
left join user ruser on(ruser.userid=w.removed_by)
left join warning_types wt on(wt.tid=w.warned_warning_id)
ORDER BY (w.warned_time wt.warn_maturity*24*60*60) DESC
LIMIT 0, 15
mysql error: You have an error in your SQL syntax near
'wt.warn_maturity*24*60*60) DESC LIMIT 0, 15' at line 6

mysql error number: 1064

Date: Saturday 25th of December 2004 01:07:49 PM
Script: http://xxxxx.xxxx.com/admincp/admin_...p?act=viewlogs
&script=/admincp/admin_warn.php&perpage=15
&orderby=(w.warned_time+wt.warn_maturity*24*60*60) %20DESC&page=1
Referer: http://xxxxx.xxx.com/admincp/admin_w...p?act=viewlogs
Username: John
IP Address: 55.55.55.55
Note the underlined section in this error, there should be a plus sign between the w.warned_time and wt.warn_maturity*24*60*60. As it shows, that plus sign is not passed to the query. Now, if I run this query directly from AdminCP, replacing the $orderby with the string defined in $s14t, it works just fine, so I have to assume that the problem has something to do with the way vB is handling the replacement of the variable in the query.

I tried escaping the plus sign using a \, I tried enclosing the variable in double quotes, and then use " . $orderby .", I tried whatever I could think of, but admitedly I am not good at this, so I end up asking for your help here.

Any ideas? They will be greatly appreciated.

Merry Christmas to all

Dean C 12-27-2004 10:08 PM

I've added some php and code tags to your post to make it a little more readable. I'm going to bed now and will take a look in the morning if I get a chance :)

Edit:

Ok i see your error already, you have to define the calculated field within your first batch of SQL. So add this to the first part of the sql:

Code:

(w.warned_time+wt.warn_maturity*24*60*60) AS calculated_field
Then add this clause:

Code:

ORDER BY calculated_field DESC

sv1cec 12-28-2004 06:21 AM

Quote:

Originally Posted by Dean C
(w.warned_time+wt.warn_maturity*24*60*60) AS

Dean, much apprecaited, you helped me avoid adding another column in that table.

Thanks a lot. I got it working now.

Sorry I missed those php tags, I should have known better, but I was so frustrated when I posted that message.

Again tnx and Rgds

Dean C 12-28-2004 10:22 AM

No problem, good luck with whatever you're coding ;)

sv1cec 12-28-2004 10:30 AM

Quote:

Originally Posted by Dean C
No problem, good luck with whatever you're coding ;)

I am coming up with the latest version of Advanced Warning System (that's my hack) and I wanted to give the user the ability to sort a list, by various columns.

Thanks for the tip Dean, sincerely appreciated.

Happy New Year to you and yours.


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

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01880 seconds
  • Memory Usage 1,738KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_code_printable
  • (4)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (5)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete