SQL code inside PHP code -


i want make code clean possible , how looks @ moment:

$query = "insert users"         ."(avatar, biography, birth_date, email, location, password, profile_views, real_name, reputation, signup_date, username) values "         ."('default',"         ." 'user since ".date("d-m-y")."',"         ." '0000-00-00',"         ." '".$email."',"         ." 'default',"         ." '".hash("sha256", $password)."',"         ." 0,"         ." 'default',"         ." 0,"         ." '".date('y-m-d h:i:s')."',"         ." '".$this->username."'"         .");"; 

as can see it's quite messy, so, what's way put sql code inside php?

heredoc , parameterized queries. do not being victim of sql injection, right? of course.

$query = <<<_e_ insert users   (avatar, biography, birth_date, email, location, password, profile_views, real_name, reputation, signup_date, username)   values ('default', :bio, '0000-00-00', :email, 'default', :pass, 0, 'default', 0, :singup, :uname); _e_; $params = array(   'bio'    => 'user since'.date("d-m-y"),   'email'  => $email,   'pass'   => hash("sha256", $password),    'singup' => date('y-m-d h:i:s'),   'uname'  => $this->username, );  // checking return values, right? // of course. if( ! $stmt = $dbh->prepare($query) ) { die($dbh->errorinfo()); } if( ! $stmt->execute($params) ) { die($stmt->errorinfo()); } 

that more or less assumes pdo, mysqli similar if recall correctly.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -