Sql sintaxis

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
User avatar
joseazr1987
Forum Newbie
Posts: 10
Joined: Fri Aug 27, 2010 11:30 am

Sql sintaxis

Post by joseazr1987 »

I need help.. i see this sintaxis of sql

Code: Select all

"INSERT INTO %s (id, login, pw, real_name, phone, radio, professional, office, service, special, email, job_listing, access_level, active) VALUES (NULL, %s, %s, %s, %s, %s, %d, %d, %d, %s, %s, %d, %d, 'n')
but I can't understand what it mean %s,%d.. can you help me please?!!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Sql sintaxis

Post by Jonah Bron »

%s and %d don't mean anything in SQL. That SQL string is likely passed through sprintf. Do you have the rest of the code?

http://php.net/function.sprintf
User avatar
joseazr1987
Forum Newbie
Posts: 10
Joined: Fri Aug 27, 2010 11:30 am

Re: Sql sintaxis

Post by joseazr1987 »

This is the code:

Code: Select all

$sql = sprintf("INSERT INTO %s (id, login, pw, real_name, phone, radio, professional, office, service, special, email, job_listing, access_level, active) VALUES (NULL, %s, %s, %s, %s, %s, %d, %d, %d, %s, %s, %d, %d, 'n')", 
$this->table_name,
$this->ins_string($first_login),
$this->ins_string(md5($first_password)),
$this->ins_string($first_name),
$this->ins_string($phone),
$this->ins_string($radio),
$professional,
$office,
$service,
$this->ins_string($special),
$this->ins_string($this->user_email),
$job_listing,
DEFAULT_ACCESS_LEVEL);
$ins_res = mysql_query($sql);
So the values are after the sql Insert?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Sql sintaxis

Post by Jonah Bron »

sprintf inserts all arguments after the first one into the provided string in the order in which the insert indicators come. In %s, the "s" stands for string, indicating that it should be inserted as a string type. So, since it does it in order, the first %s will insert $this->table_name, the second %s will insert $this->ins_string($first_login), the third %s will insert $this->ins_string(md5($first_password)), and so on.

This:

Code: Select all

$bar = sprintf("INSERT INTO %s ...", $somevar);
Is the same as

Code: Select all

$bar = "INSERT INTO " . $somevar . " ...";
But it looks neater.
User avatar
joseazr1987
Forum Newbie
Posts: 10
Joined: Fri Aug 27, 2010 11:30 am

Re: Sql sintaxis

Post by joseazr1987 »

Thank you very much.. I apreciate your help!!
Post Reply