1

I know that this is same with a lot of other posts here but i can't figure it out!

my code looks like this:

$i=0;
$shelves = array();
$shelves['position'] = array();
$query = "select id, cat_id, book_title, writer, publisher, issue_year, copies, abstract from library where $table like '%$search_param%'";
$result = mysql_query($query);
while ( $data = mysql_fetch_assoc($result) ) {
   error_log($data['id']);
   $shelves['position'][$i]['id'] = $data['id'];
   $shelves['position'][$i]['cat_id'] = $data['cat_id'];
   $shelves['position'][$i]['book_title'] = $data['book_title'];
   $shelves['position'][$i]['writer'] = $data['writer'];
   $shelves['position'][$i]['publisher'] = $data['publisher'];
   $shelves['position'][$i]['issue_year'] = $data['issue_year'];
   $shelves['position'][$i]['copies'] = $data['copies'];
   $shelves['position'][$i]['abstract'] = $data['abstract'];
   ++$i;
}
error_log( count($shelves['position']) );

And because there are tones of other posts like this one i tried their solution:

$query = sprintf("select id, cat_id, book_title, writer, publisher, issue_year, copies, abstract from library where %s like'%%%s%'",mysql_real_escape_string($table),mysql_real_escape_string($search_param) );

Or Something like that:

$query = "select id, cat_id, book_title, writer, publisher, issue_year, copies, abstract from library where $table like '%{$search_param}%'";

I also tried run the query without dynamic variables just text and i got the same thing.

$query = "select id, cat_id, book_title, writer, publisher, issue_year, copies, abstract from library where book_title like '%lord%'";

Nothing seems to work.

I've tested my query on mysql workbench and it works like a charm!

In all of three queries i never get a log of the first error_log and the second one yells at me 0 every time!

Could someone light the way please?

9
  • What result are you getting? Commented Jul 11, 2013 at 3:54
  • 1
    Lets do something simple, first mysql_query($query) or die(mysql_error(); so we can see if there is any error. Commented Jul 11, 2013 at 3:58
  • @swapnesh the $table variable is holding a sting like book_title! Commented Jul 11, 2013 at 4:12
  • @TomMcPadden On my php script i'm not getting any result! Commented Jul 11, 2013 at 4:13
  • 1
    @GeorgeSfitis So it's blank? Could it be that nothing in the database matches the query? Commented Jul 11, 2013 at 4:15

3 Answers 3

2

Well, the only thing suspicious here is the charset/collation you're using, which might result in a case sensitiveness issue. Try forcing a non-sensitive collation and see what happens.

SET NAMES 'utf8' COLLATE 'utf8_general_ci';

A utf8_bin (or any *_bin for that matter) makes comparison case-sensitive. If setting the connection collation to insensitive works, that would explain the difference between your script and MySQL Workbench.

Anyway, I would set the column collation to case-insensitive to avoid this kind of issues.

Sign up to request clarification or add additional context in comments.

2 Comments

My table collation is utf8 - utf8_bin. About the forcing thing can you give me an example i haven't done it till now! Thanks.
Thanks that was my problem hope this helps other people!
0
The query getting correct answer.. 

Try this:

$i=0; // before start while loop,you need to initialize i

while ( $data = mysql_fetch_assoc($result) ) {
   error_log($data['id']);
   $shelves['position'][$i]['id'] = $data['id'];
   $shelves['position'][$i]['cat_id'] = $data['cat_id'];
   $shelves['position'][$i]['book_title'] = $data['book_title'];
   $shelves['position'][$i]['writer'] = $data['writer'];
   $shelves['position'][$i]['publisher'] = $data['publisher'];
   $shelves['position'][$i]['issue_year'] = $data['issue_year'];
   $shelves['position'][$i]['copies'] = $data['copies'];
   $shelves['position'][$i]['abstract'] = $data['abstract'];
   ++$i;
}

2 Comments

Sorry about that i've got the line you mentioned i just forgot to put in on the question!
it's ok.. you try fetch array "while($data = mysql_fetch_array($result))"
0

Try this it may help.

$query ="select id,
                cat_id, 
                book_title,
                writer, 
                publisher, 
                issue_year, 
                copies, 
                abstract 
                from library 
                where ".$table." like "%".$search_param."%";

But it is not tested yet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.