Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySQL query with %s and %d

Tags:

php

mysql

SELECT COUNT(*) AS test FROM %s WHERE id = %d AND tmp_mail <> ''

What are %s and %d for?

like image 463
Delirium tremens Avatar asked Jun 07 '09 16:06

Delirium tremens


People also ask

What is %s and %D in MySQL?

%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. in your examples, $slug is a string and $this->id is an integer.

What does %s mean in MySQL?

%s is a placeholder used in functions like sprintf. Check the manual for other possible placeholders. $sql = sprintf($sql, "Test"); This would replace %s with the string "Test".

HOW include php variable in MySQL select query?

php require_once 'config. php'; $id = $_GET["id"]; //ID OF THE CURRENT CONTACT $user = $_GET["user"]; //ID OF THE CURRENT USERS $query = mysql_query("SELECT name FROM contacts WHERE contact_id='". mysql_real_escape_string( $id ) .


1 Answers

Those are format symbols used e.g. by sprintf(). Example:

<?php
 $sql_template = "SELECT COUNT(*) AS test FROM %s WHERE id = %d AND tmp_mail <> ''";
 $sql_real = sprintf($sql_template, 'sometable', 12345);
 echo $sql_real;
?>

Output:

SELECT COUNT(*) AS test FROM sometable WHERE id = 12345 AND tmp_mail <> ''
like image 139
Piskvor left the building Avatar answered Oct 11 '22 18:10

Piskvor left the building