1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

what is this ':user_id'

Discussion in 'PHP' started by ameerulislam43, Sep 18, 2013.

  1. #1
    The code
    $params = array( ':user_id' => $_SESSION['smth]['user_id'] );
    $sql = "SELECT * FROM `smtble` WHERE `user_id` = :user_id;";
    $stmt = parent::query($ssafdsf, $sjrlj);
    PHP:
    What does this colon means in front of user_id ?
     
    Solved! View solution.
    ameerulislam43, Sep 18, 2013 IP
  2. Basti

    Basti Active Member

    Messages:
    625
    Likes Received:
    6
    Best Answers:
    3
    Trophy Points:
    90
    #2
    It is something PDO use. Instead of escaping user input / other variables when using them in a database query like standart mysql queries, you give it a namespace like that and then later can define the namespace as INT or String and let pdo do the escaping internally.

    Hope that makes a bit sense, i am not the best in explaining things
     
    Basti, Sep 18, 2013 IP
  3. #3
    Basically, the : indicates it's a label for external content to be plugged in from the query. You're code snippet is incomplete as you are missing the EXECUTE part of things.

    Though really there is little reason for those extra variables to exist... or for the single quotes in the query (since none of the values have spaces in them)... and the vars passed to ::query are gibberish, much less accessing via the parent element (which is some really noodle doodle crap) -- Just what is that from and/or could we see more of it?

    Properly written and assuming $db was a connected PDO object, that would read something like this:
    $statement = $db->prepare('
    	SELECT * FROM smtble
    	WHERE user_id = :user_id
    ');
    $statement->execute(array(
    	':user_id' => $_SESSION['smth]['user_id']
    ));
    Code (markup):
    Basically prepared queries (what that appears to be setting up for) lets you re-use the same query statement more than once, and 'auto-sanitizes' values so as to prevent sql script injections (a nasty form of hijacking a server). The array inside ->execute contains the values to plug in where the labels (the part starting with a : is) in the prepared query.

    Net result is it would be the same crappy-old-insecure old-school as:

    mysql_query("
    	SELECT * FROM smtble
    	WHERE user_id = '" . mysql_real_escape_string($_SESSION['smth]['user_id']) . "'
    ");
    Code (markup):
    Except of course it's much more secure than adding your query string together, and you can re-use the same prepared statement with multiple values.

    For example:

    $statement = $db->prepare('
    	INSERT INTO users ( name ) VALUES ( :newName )
    ');
    
    for (var $t=0; $t<10; $t++) {
    	$statement->execute(array(
    		':newName' => 'Test User '.$t
    	));
    }
    Code (markup):
    Would make ten new users using one prepare executed multiple times with different data.

    ... and because prepared queries are auto sanitized, you can do things like dump $_GET or $_POST directly into the VALUES with no fear of getting cracked via code injections.
     
    deathshadow, Sep 18, 2013 IP
  4. eritrea1

    eritrea1 Active Member

    Messages:
    182
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    70
    #4
    A session is not like a $_SERVER global variable, It can hold multiple incursion levels. That means $_SESSION['foo'] is as valid as $_SESSION['foo']['1'] or $_SESSION['name']['john']['lastname']['smith']; Because it stores values in a multi-dimentional array.
     
    eritrea1, Sep 18, 2013 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    Which has WHAT exactly to do with the OP's question?!?
     
    deathshadow, Sep 18, 2013 IP
  6. eritrea1

    eritrea1 Active Member

    Messages:
    182
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    70
    #6
    I thought that was where he was stuck. I think I gave a right answer there, if not you could always prove me wrong
     
    eritrea1, Sep 18, 2013 IP
  7. ameerulislam43

    ameerulislam43 Active Member

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #7
    thanks for your answer but if you see my subject you can guess where I was stuck.
     
    ameerulislam43, Sep 18, 2013 IP
  8. ameerulislam43

    ameerulislam43 Active Member

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #8
    Fascinating. Since when this is being used? It's been a while since when I last time coded few lines, now when I restarted I see a lot of developments in this area.

     
    ameerulislam43, Sep 18, 2013 IP
  9. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #9
    It's called PDO, and has been available for years. It's the definitive preferred way to code against a database in PHP,due to the fact that it's a lot more secure from the get-go, compared to other abstraction layers like mysql_ and mysqli_. It also supports a wealth of other databases
     
    PoPSiCLe, Sep 18, 2013 IP
  10. Strider64

    Strider64 Member

    Messages:
    40
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    25
    #10
    mysqli is secured, the big difference between mysqli and PDO is transferability between types of of databases. PDO easily transferable and msyqli nope. Then there are people who say PDO is easier to use, I tend to be in that category. In my opinion PDO gives a person better structure when writing code instead of ???? marks.
     
    Strider64, Sep 19, 2013 IP