Jump to content

Recommended Posts

Hi,

 

i found this coding from a web site. Can anyone what is the meaning of this code when selecting data.

$sql =("SELECT `customer_id`, `nic`, `full_name`, `name_with_initials`, `address`, `contact_number`, `gender` FROM `customer` WHERE  `nic`='%s'", mysql_real_escape_string($_POST['nic']) );

  $result=mysql_query($sql) or die( mysql_error() );

 

Specially that part where `nic`='%s'" and real_escape_string().

 

Thanks,

Link to comment
https://forums.phpfreaks.com/topic/212335-problem-regarding-a-code/
Share on other sites

- % is used for the like condition in SQL. This gives some background

  http://www.techonthenet.com/sql/like.php If i am correct it checks if the input ends with the letter "s"

- mysql_real_escape_string is a sanitizing function to transform bad input. in this case user submitted by the post variable 'nic' If you would not do that users may be able to execute arbitrary sql like drop table.

 

So in this case it checks:

Whether the sanitized input by the end-user exists in the table customer_id ending with the letter "s"

and if it doesn't it dies and errors out

fortnox is incorrect based on the format of the sentence.

 

this sentence is incomplete

$sql =("SELECT `customer_id`, `nic`, `full_name`, `name_with_initials`, `address`, `contact_number`, `gender` FROM `customer` WHERE  `nic`='%s'", mysql_real_escape_string($_POST['nic']) )

;

 

should be write this way

 

$sql = sprintf("SELECT `customer_id`, `nic`, `full_name`, `name_with_initials`, `address`, `contact_number`, `gender` FROM `customer` WHERE  `nic`='%s'", mysql_real_escape_string($_POST['nic']) );

 

there '%s'  is formating mysql_real_escape_string($_POST['nic']) as a string.

 

http://www.php.net/manual/en/function.sprintf.php

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.