Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does %s means here?

What does %s mean?

$sql = "SELECT * 
          FROM page_table 
         WHERE page_name = '%s' 
         LIMIT 1";
like image 716
ktm Avatar asked Apr 16 '11 13:04

ktm


People also ask

What is like %% in SQL?

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

What is %s and %D in PHP?

This syntax works outside of classes as well. From the documentation: d - the argument is treated as an integer, and presented as a (signed) decimal number. s - the argument is treated as and presented as a string.

What is @@ in SQL?

In SQL Server, symbol @@ is prefixed to global variables. The server maintains all the global variables.


1 Answers

It is a formatted string where %s is a placeholder. I suspect that $sql is handed to sprintf to transform it into a real query. Example:

$name = 'posts';
$sql = "SELECT * FROM page_table WHERE page_name = '%s' LIMIT 1";
$formattedSql = sprintf($sql, $name);

This will generate a query looking like:

SELECT * FROM page_table WHERE page_name = 'posts' LIMIT 1

This is very useful when you don't want to fiddle around with quotes and doublequotes.

like image 77
alexn Avatar answered Sep 20 '22 17:09

alexn