Page 1 of 1

LIKE and vsprintf

Posted: Sun Jun 17, 2007 10:03 am
by Mirux
Howdy, Quick and detailed.

I am making a search function that browse one of my tables for results. I am using LIKE but I am having problems because I use vsprintf. I can not change this because my query functions is defined like that for the whole site in a Class.

I'll paste the query:

Code: Select all

$sql= "SELECT * FROM files WHERE author LIKE '%%s%' OR title LIKE '%%s%'";
$vars= array($_POST['search'],$_POST['search']);
$result= $this->db->query($sql, $vars) or die (mysql_error());
So I've tryed to not pass $vars in the query but it returns me a vsprintf error. I have not found a way to make it work using vsprintf. It seems to get confused with the others %

Thank you very much.

Posted: Sun Jun 17, 2007 10:12 am
by feyd
%% is the escape for one percentage mark.

Posted: Sun Jun 17, 2007 10:45 am
by Mirux
And... ?

Posted: Sun Jun 17, 2007 11:29 am
by superdezign
.... And turn all of your "%" that aren't used by your sprintf into "%%."

Posted: Sun Jun 17, 2007 11:38 am
by Mirux
So, based on what you suggest my query should look like:

Code: Select all

$sql= "SELECT * FROM files WHERE author LIKE '%%%s%%' OR title LIKE '%%%s%%'";
$vars= array($_POST['search'],$_POST['search']);
$result= $this->db->query($sql, $vars) or die (mysql_error());

Posted: Sun Jun 17, 2007 12:01 pm
by superdezign
Try it.

Posted: Sun Jun 17, 2007 12:13 pm
by bdlang
Aside from your issue, I'm curious as to why you're 1) using mysql_error() with a database handler class, and 2) allowing an object to kill a script with die().

Posted: Sun Jun 17, 2007 12:39 pm
by Mirux
Check by yourself why.

There are orders in my site so If I use echo, it wouldn't be printed on bottom of the site, sometimes on top so if I use DIE ir will just display a message in a different page and not including the coding as content.

http://www.codersphp.com/mirux/DHH

Posted: Sun Jun 17, 2007 12:44 pm
by superdezign
I'm pretty sure that he was referring to the fact that a database class should handle errors itself. In the class, you should have it echo or log the error for you.

It's odd to see mysql_error() right next to a database handler, considering that it's something that the database handler should... handle.

Posted: Sun Jun 17, 2007 12:46 pm
by bdlang
Mirux wrote:Check by yourself why.
I was just curious, making some (potentially) helpful observations, it's your code not mine.

Posted: Sun Jun 17, 2007 6:54 pm
by Mirux
Ohh you are right but I just put the mysql_error(); to test. In the end, It is the same for me if I echo a "ERROR" instead. But I don't use it once it is working properly.

I am having terrible problems with this search function. When I go to the site and press the link 'Buscador' it goes to 'index.php?p=buscar' and then imagine I get 21 results and I only want to display 15 per peage. I have a pagecalc script I made but when I click on page 2 it goes to 'index.php?p=buscar&n=15' where n= number of page. For example n=0 -> page 1, n=15 -> page 2, etc.

I still can't figure it out how to make it work. ;[ when I click page 2 it shows me a blank page, or shows nothing.

This is my pagecalc script I made for search function:

Code: Select all

public function CalcPagesSearch($total)
	{
		$eachpage= 15;
		$numofpages= ceil($total/$eachpage);
		if ($_GET['n'] == "0" || $_GET['n'] < "15")
			$echo.= "&laquo;";
		else
		{
			$previous= $_GET['n']-$eachpage;
			$echo.= "<a href='./?p=buscar&n=$previous'>&laquo;</a>";
		}
		for ($x=1; $x <= $numofpages; $x++)
		{
			$start= ($x*$eachpage)-$eachpage;
			if ($_GET['n'] == $start)
			$echo.= " $x";
			else $echo.=" <a href='./index.php?p=buscar&n=$start'>$x</a>";
		}
		$last= ($numofpages*$eachpage)-$eachpage;
		if ($_GET['n'] == $last || $last < "0")
		$echo.= " &raquo;";
		else
		{
			$next= $_GET['n']+$eachpage;
			$echo.= "<a href='./index.php?p=buscar&n=$next'> &raquo;</a>";
		}		
		echo "<center>".$echo."</center>";
	}
I hope you guys can help me with this. I've been trying to make it work all day long. :<

Posted: Sun Jun 17, 2007 6:59 pm
by jayshields
I don't have time to check your code thoroughly but the first thing that pops out is that you're doing mathematical comparisons on strings. You could solve this by using type casting and/or getting rid of the double quotes surrounding some of your numbers.

Posted: Sun Jun 17, 2007 7:04 pm
by superdezign
I didn't read it thoroughly, but you'll want to use $numOfPages to determine if there should be a "next" link.

Posted: Mon Jun 18, 2007 3:30 pm
by Mirux

Code: Select all

$sql= "SELECT * FROM files WHERE author LIKE '%%%s%%' OR title LIKE '%%%s%%' ORDER BY date DESC";
$vars= array($searchthis,$searchthis);
$result= $this->db->query($sql, $vars) or die ("ERROR");
Well, with the double % is not working either, can anyone tell the code just take in count the %s as an vsprintf arguement? It seems it gets confused with the other % characters.

Any idea how to fix this? : <

Posted: Mon Jun 18, 2007 3:47 pm
by Mirux
Okay, thanks for your help anyway but I solved it myself.

I made this:

Code: Select all

$sql= "SELECT * FROM files WHERE author LIKE %s OR title LIKE %s ORDER BY date DESC";
$vars= array("%$searchthis%","%$searchthis%");
$result= $this->db->query($sql, $vars);
Thanks for the support, peace.