almightyegg Posted April 14, 2009 Share Posted April 14, 2009 I am customising a mysql_query depending on what a user has inserted on the form on the page before but I have an error in my syntax $psyche = $_POST['psyche']; $psymin = $_POST['psymin']; $psymax = $_POST['psymax']; $level = $_POST['level']; $lvlmin = $_POST['lvlmin']; $lvlmax = $_POST['lvlmax']; $lf = $_POST['lf']; $lfmin = $_POST['lfmin']; $lfmax = $_POST['lfmax']; $soc = $_POST['soc']; $sort = $_POST['sort']; $order = $_POST['order']; if($level == TRUE && is_numeric($lvlmin) == TRUE && is_numeric($lvlmax) == TRUE){ $info .= " level >= '$lvlmin' and level <= '$lvlmax'"; } if($psyche == TRUE && is_numeric($psymin) == TRUE && is_numeric($psymax) == TRUE){ $info .= " psyche >= '$psymin' and psyche <= '$psymax'"; } if($lf == TRUE && is_numeric($lfmin) == TRUE && is_numeric($lfmax) == TRUE){ $info .= " lifeforce >= '$lfmin' and lifeforce <= '$lfmax'"; } if($soc == all){ $info .= " clutch >= '0'"; }elseif($soc == notmine){ $info .= " clutch != '{$mem['clutch']}' and clutch > '0'"; }elseif($soc == mine){ $info .= " clutch = '{$mem['clutch']}'"; }elseif($soc == none){ $info .= " clutch = '0'"; } $info .= " SORT BY $sort $order"; $sel = mysql_query("SELECT * FROM users WHERE$info") or die(mysql_error()); This is the code the creates the mysql_query ($sel) but it shows: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'psyche >= '-5000' and psyche <= '-2000' clutch = '0' SORT BY level ASC' at line 1 Can anybody help me with this?? Link to comment https://forums.phpfreaks.com/topic/154007-solved-what-am-i-doing-wrong/ Share on other sites More sharing options...
chelnov63 Posted April 14, 2009 Share Posted April 14, 2009 put a comma before clutch = '0' Link to comment https://forums.phpfreaks.com/topic/154007-solved-what-am-i-doing-wrong/#findComment-809513 Share on other sites More sharing options...
AviNahum Posted April 14, 2009 Share Posted April 14, 2009 try to change this line to data without vars: $sel = mysql_query("SELECT * FROM users WHERE$info") or die(mysql_error()); change this to: $sel = mysql_query("SELECT * FROM users WHERE id=2") or die(mysql_error()); change it to your db details Link to comment https://forums.phpfreaks.com/topic/154007-solved-what-am-i-doing-wrong/#findComment-809515 Share on other sites More sharing options...
almightyegg Posted April 14, 2009 Author Share Posted April 14, 2009 eh? why?? And ,clutch = '0' or clutch ,= '0' or clutch = ,'0' or what?? $info is the db details put into a string Link to comment https://forums.phpfreaks.com/topic/154007-solved-what-am-i-doing-wrong/#findComment-809517 Share on other sites More sharing options...
chelnov63 Posted April 14, 2009 Share Posted April 14, 2009 assumed it wasnt there as your error statement says: 'psyche >= '-5000' and psyche <= '-2000' clutch = '0' anyway try echoing out the sql string and see what you get.. best way to debug when queries dont work: echo "SELECT * FROM users WHERE $info"; then copy and paste it and try running that statement in you mySQL GUI(phpmyadmin or whatever you use) and see what errors it gives you.. Link to comment https://forums.phpfreaks.com/topic/154007-solved-what-am-i-doing-wrong/#findComment-809521 Share on other sites More sharing options...
almightyegg Posted April 14, 2009 Author Share Posted April 14, 2009 I had got an error with that, a really stupid one too. Thanks for noticing that but I have another one I echoed it out: SELECT * FROM users WHERE level >= '1' and level <= '20' and psyche >= '-5000' and psyche <= '-2000' and lifeforce > '-1' and clutch = '0' SORT BY level ASC I can't see problems with it? I editted the code to fix some errors if($level == TRUE && is_numeric($lvlmin) == TRUE && is_numeric($lvlmax) == TRUE){ $info .= " level >= '$lvlmin' and level <= '$lvlmax'"; }else{ $info .= " level != '0'"; } if($psyche == TRUE && is_numeric($psymin) == TRUE && is_numeric($psymax) == TRUE){ $info .= " and psyche >= '$psymin' and psyche <= '$psymax'"; }else{ $info .= " and psyche != '0'"; } if($lf == TRUE && is_numeric($lfmin) == TRUE && is_numeric($lfmax) == TRUE){ $info .= " and lifeforce >= '$lfmin' and lifeforce <= '$lfmax'"; }else{ $info .= " and lifeforce > '-1'"; } if($soc == all){ $info .= " and clutch >= '0'"; }elseif($soc == notmine){ $info .= " and clutch != '{$mem['clutch']}' and clutch > '0'"; }elseif($soc == mine){ $info .= " and clutch = '{$mem['clutch']}'"; }elseif($soc == none){ $info .= " and clutch = '0'"; } $info .= " SORT BY $sort $order"; $sel = mysql_query("SELECT * FROM users WHERE$info"); Link to comment https://forums.phpfreaks.com/topic/154007-solved-what-am-i-doing-wrong/#findComment-809534 Share on other sites More sharing options...
chelnov63 Posted April 14, 2009 Share Posted April 14, 2009 are your fields level etc varchar fields or number fields such as int .. if they are number fields then try without the quotes around the numbers e.g SELECT * FROM users WHERE level >= 1 and level <= 20 and psyche >= -5000 and psyche <= -2000 and lifeforce > -1 and clutch = 0 SORT BY level ASC also have you tried running it directly in your GUI .. do you get any query errors now? .. Link to comment https://forums.phpfreaks.com/topic/154007-solved-what-am-i-doing-wrong/#findComment-809540 Share on other sites More sharing options...
almightyegg Posted April 14, 2009 Author Share Posted April 14, 2009 Most of the numbers are generated by $variables, would they require '? What do you mean run it in my GUI? Link to comment https://forums.phpfreaks.com/topic/154007-solved-what-am-i-doing-wrong/#findComment-809550 Share on other sites More sharing options...
almightyegg Posted April 14, 2009 Author Share Posted April 14, 2009 Oh my.... we both missed it :') $info .= " SORT BY $sort $order"; should have been $info .= " ORDER BY $sort $order"; Thanks for the help Link to comment https://forums.phpfreaks.com/topic/154007-solved-what-am-i-doing-wrong/#findComment-809575 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.