NONAME_2 Posted January 9, 2011 Share Posted January 9, 2011 What's Wrong With My SQL WHERE Cluase: SQL query failed. Check your query. Error Returned: Unknown column 'something' in 'where clause'Unknown column 'something' in 'where clause' <div class="box"> <form name="search" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post"> <div class="search_bd"> <div class="search_b"> <input type="text" name="qry_str" size="40" value="search" onblur="if(this.value=='') this.value='search';" onfocus="if(this.value=='search') this.value='';"> <input name="category" type="hidden" id="style"> <input class="search_btn" name="submit" type="submit" value="Search"> </div> <select name="by"> <option value="login" selected="selected">username</option> <option value="access_level" >access</option> <option value="firstname" >firstname</option> <option value="lastname" >lastname</option> </select></div> </form> </div> <?php if(!(isset($_GET['id']))) { //action: view users ----------------------------------------------------------------------------- //Include database connection details require_once('../config.php'); //Connect to mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } //Include the PS_Pagination class include('../ps_pagination.php'); //charset mysql_query("SET NAMES utf8"); mysql_set_charset("utf8"); if(isset($_POST['submit'])){ if(isset($_POST['category'])){ $by = $_POST['by']; if ($by == "access_level") { if ($_POST['qry_str'] == 'user'){ $_POST['qry_str'] = 1; }else{ if ($_POST['qry_str'] == 'admin') { $_POST['qry_str'] = 9; } }} $search = $_POST['qry_str']; function antisql($search){ $search=htmlspecialchars(trim($search)); if(get_magic_quotes_gpc()){ return mysql_real_escape_string(stripslashes($search)); }else return mysql_real_escape_string($search); } $query = "SELECT member_id, firstname , lastname, login, ". "access_level FROM members WHERE '.$by.' = '%$search%' ". " OR '.$by.' LIKE '%$search%'"; } }else{ $query = "SELECT member_id, firstname , lastname, login, ". "access_level FROM members";} $rs = mysql_query($query); $pager = new PS_Pagination($link,$query,5,1); $pager->setDebug(true); $rs = $pager->paginate(); if(!$rs) die(mysql_error()); ?> <table id="table"><thead> <tr><th>action</th><th>access</th><th>lastname</th><th>firstname</th><th>username</th></tr></thead> <tbody> <?php //show the users while ($row = mysql_fetch_assoc($rs)) { $result=mysql_query($query); ?> <tr> <td><a href='user.php?edit&id=<?php echo $row['member_id'];?>'> <img src="img/edit.jpg" alt="edit"></a> <a href='user.php?delete&id=<?php echo $row['member_id'];?>'> <img src="img/cancel.jpg" alt="delete"></a></td> <td><?php echo ($row['access_level']==9)?"admin":"user";?></td> <td><?php echo $row['lastname'];?></td> <td><?php echo $row['firstname'];?></td> <td><?php echo $row['login'];?></td> </tr> <?php } ?> </tbody></table><span></span><br /><br /><div class="pagination"> <?php echo $pager->renderFullNav();?></div><br /><br /> <?php } ?> by the way, i used collation utf-8;TNX. Quote Link to comment https://forums.phpfreaks.com/topic/223843-whats-wrong-with-my-sql-where-cluase/ Share on other sites More sharing options...
kartul Posted January 9, 2011 Share Posted January 9, 2011 From error I can see that field name something does not exist. Where did you get something anyway? I can only see login, access_level, first and lastname options. Quote Link to comment https://forums.phpfreaks.com/topic/223843-whats-wrong-with-my-sql-where-cluase/#findComment-1156952 Share on other sites More sharing options...
dragon_sa Posted January 9, 2011 Share Posted January 9, 2011 what if you rewrite $query = "SELECT member_id, firstname , lastname, login, ". "access_level FROM members WHERE '.$by.' = '%$search%' ". " OR '.$by.' LIKE '%$search%'"; to $query = "SELECT member_id, firstname , lastname, login, access_level FROM members WHERE '.$by.'='%$search%' OR '.$by.' LIKE '%$search%'"; I think that will work better same with $query = "SELECT member_id, firstname , lastname, login, ". "access_level FROM members"; to $query = "SELECT member_id, firstname , lastname, login, access_level FROM members"; Quote Link to comment https://forums.phpfreaks.com/topic/223843-whats-wrong-with-my-sql-where-cluase/#findComment-1156954 Share on other sites More sharing options...
trq Posted January 9, 2011 Share Posted January 9, 2011 Which of the two queries is failing? You have '. & .' around the $by variable which makes no sense. Quote Link to comment https://forums.phpfreaks.com/topic/223843-whats-wrong-with-my-sql-where-cluase/#findComment-1156958 Share on other sites More sharing options...
NONAME_2 Posted January 9, 2011 Author Share Posted January 9, 2011 First Query. Quote Link to comment https://forums.phpfreaks.com/topic/223843-whats-wrong-with-my-sql-where-cluase/#findComment-1156963 Share on other sites More sharing options...
trq Posted January 9, 2011 Share Posted January 9, 2011 You have '. & .' around the $by variable which makes no sense. Quote Link to comment https://forums.phpfreaks.com/topic/223843-whats-wrong-with-my-sql-where-cluase/#findComment-1156967 Share on other sites More sharing options...
NONAME_2 Posted January 9, 2011 Author Share Posted January 9, 2011 Hi ,you're right thorpe;TNX . Quote Link to comment https://forums.phpfreaks.com/topic/223843-whats-wrong-with-my-sql-where-cluase/#findComment-1156976 Share on other sites More sharing options...
NONAME_2 Posted January 10, 2011 Author Share Posted January 10, 2011 Hi, i'm back; i marked it as SOLVED! but (ding dong) there is a problem: in search Pagination=> where it paginates and i move between pages, results of search query doesn't show and fetch of second query is executed; Quote Link to comment https://forums.phpfreaks.com/topic/223843-whats-wrong-with-my-sql-where-cluase/#findComment-1157295 Share on other sites More sharing options...
NONAME_2 Posted January 12, 2011 Author Share Posted January 12, 2011 Hi, My Problem is solved by : $pager = new PS_Pagination($link,$query,2,1,"param1=$valu1¶m2=$valu2"); instead of valu# ,set your var and before and after code(s), control them. in :..MATCH(keywords) AGAINST("something" IN BOOLEAN MODE) ,it good but in LIKE shold becare!TNX. Quote Link to comment https://forums.phpfreaks.com/topic/223843-whats-wrong-with-my-sql-where-cluase/#findComment-1158291 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.