Jump to content

What's Wrong With My SQL WHERE Cluase?


NONAME_2

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/223843-whats-wrong-with-my-sql-where-cluase/
Share on other sites

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";

Hi, My Problem is solved by :

$pager = new PS_Pagination($link,$query,2,1,"param1=$valu1&param2=$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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.