Things like "%s" in sql queries

Hey all,

Sometimes when I read other people’s code, I see stuff like: “SELECT filename FROM {system} WHERE name = ‘%s’ AND type = ‘%s’” - what is the “%s” called, and what does it do?

I Googled all morning on this, but since I don’t know the proper term for this, I didn’t have much luck.

Thanks!

Pattern matching - http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html

After reading the link jelena kindly posted, “%s” looks for any name ending in ‘s’. The ‘%’ stands for an arbitrary number of characters.

It’s most commonly used when the users input is going to vary and may not exactly match data in your database, like a site Search box for example.

It does depend on how the string or query is fomatted, take for example sprintf()


<?php
$format = "There are %d monkeys in the %s";
printf($format, $num, $location);
?>

This might output, “There are 5 monkeys in the tree”.

Spike

$query = sprintf("SELECT filename FROM system WHERE name = '%s' AND type = '%s'", $string_one, $string_two);

Things are often written this way by people from a perl background. The first string substitutes the first %s etc.

hehe, that’s pretty much what I meant bok!