Page 1 of 1
Sql sintaxis
Posted: Fri Aug 27, 2010 11:36 am
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?!!
Re: Sql sintaxis
Posted: Fri Aug 27, 2010 11:50 am
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
Re: Sql sintaxis
Posted: Fri Aug 27, 2010 12:08 pm
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?
Re: Sql sintaxis
Posted: Fri Aug 27, 2010 12:35 pm
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.
Re: Sql sintaxis
Posted: Fri Aug 27, 2010 12:40 pm
by joseazr1987
Thank you very much.. I apreciate your help!!