LIKE and vsprintf

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mirux
Forum Commoner
Posts: 29
Joined: Sun May 13, 2007 7:11 pm

LIKE and vsprintf

Post 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.
Last edited by Mirux on Sun Jun 17, 2007 11:37 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

%% is the escape for one percentage mark.
Mirux
Forum Commoner
Posts: 29
Joined: Sun May 13, 2007 7:11 pm

Post by Mirux »

And... ?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

.... And turn all of your "%" that aren't used by your sprintf into "%%."
Mirux
Forum Commoner
Posts: 29
Joined: Sun May 13, 2007 7:11 pm

Post 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());
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Try it.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post 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().
Mirux
Forum Commoner
Posts: 29
Joined: Sun May 13, 2007 7:11 pm

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Mirux wrote:Check by yourself why.
I was just curious, making some (potentially) helpful observations, it's your code not mine.
Mirux
Forum Commoner
Posts: 29
Joined: Sun May 13, 2007 7:11 pm

Post 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. :<
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I didn't read it thoroughly, but you'll want to use $numOfPages to determine if there should be a "next" link.
Mirux
Forum Commoner
Posts: 29
Joined: Sun May 13, 2007 7:11 pm

Post 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? : <
Mirux
Forum Commoner
Posts: 29
Joined: Sun May 13, 2007 7:11 pm

Post 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.
Post Reply