1.PHP function: block SQL injection
SQL injection or SQLi common attack site means, use the code below to help you prevent
/** * Returns the string or array * @param processed by addslashes $string the string or array to be processed * @return mixed*/functionNew_addslashes ($string){ if(!Is_array($string))return addslashes($string); foreach($string as $key=$val)$string[$key] = New_addslashes ($val); return $string;}/** * Returns the string or array * @param processed by stripslashes $string the string or array to be processed * @return mixed*/functionNew_stripslashes ($string) { if(!Is_array($string))return stripslashes($string); foreach($string as $key=$val)$string[$key] = New_stripslashes ($val); return $string;}/** * Returns the string or array * @param processed by Htmlspecialchars $obj the string or array to be processed * @return mixed*/functionNew_html_special_chars ($string) { $encoding= ' Utf-8 '; if(Strtolower(CHARSET) = = ' GBK ')$encoding= ' iso-8859-15 '; if(!Is_array($string))return Htmlspecialchars($string, Ent_quotes,$encoding); foreach($string as $key=$val)$string[$key] = New_html_special_chars ($val); return $string;}
To escape the data entered by the user:
if (! GET_MAGIC_QUOTES_GPC ()) { $search=new_addslashes ($search);
}
The output does stripslashes,htmlspecialchars to defend against XSS attacks when outputting html, which is different from the defense SQL injection above.
2. Mysqli database connection with PHP instance
Use method one: Using the traditional process-oriented approach
The PHP code is as follows:
<? php $connect = mysqli_connect (' localhost ', ' Root ', ', ' volunteer ') or die (' Unale to connect ' $sql = "SELECT * from Vol_msg" ; $result = mysqli_query ( $connect , $sql while ( $row = mysqli_fetch_row ( $result echo $row [0 ]; } ?>
Using method Two: Invoking an interface using an object-oriented method (recommended)
Look at the PHP code as follows:
<?PHP//create the object and open the connection, and the last parameter is the name of the selected database$db=NewMysqli (' localhost ', ' root ', ' ', ' volunteer ')); //Check if the connection is successfulif(Mysqli_connect_errno()){ //Note Mysqli_connect_error () new features die(' Unable to connect! ').Mysqli_connect_error(); } $sql= "SELECT * FROM Vol_msg"; //Execute SQL statements, fully object-oriented$result=$db->query ($sql);
$num _results= $result->num_rows;
Echo ' shared '. $num _results. ' Record ' while($row=$result-Fetch_array ()) { Echo $row[0];
}
$result->free ();
$db->close (); ?>
The above two PHP instances run exactly the same results, and you can clearly see the benefits of building a database connection using the Mysqli class Object!
Inserting and modifying records I don't have to say anything, just change the SQL statement!
Defensive SQL Injection (input database):
PDO Bindparam or Mysqli_stmt_bind_param: Avoid SQL injection.
Addslashes: Use backslashes to escape all single quotes, double quotes, backslashes, and Nul ' s, to some extent avoiding SQL injection.
Mysqli_real_escape_string: Escapes special characters in the SQL statement.
With Bind_param, you don't need to use ADDSLASHES,MYSQLI_REAL_ESCAPE_STRING,MAGIC_QUOTES_GPC these features.
Like what:
PdoMySQL://Method 1 (question-mark placeholder)$stmt=$db->prepare (' UPDATE posts SET post_title =?, Post_content =? WHERE id =? ');$stmt->execute (Array($title,$content,$id));//all values are considered PDO::P ARAM_STR Processing//Method 2 (named placeholders)$stmt=$db->prepare (' UPDATE posts SET post_title =: Title, Post_content =: content WHERE id =: ID ');$stmt->execute (Array(': title ' = =$title, ': content ' =$content, ': id ' = =$id));//all values as PDO::P ARAM_STR Processing//Method 3$stmt=$db->prepare (' UPDATE posts SET post_title =?, Post_content =? WHERE id =? ');$stmt->bindparam (1,$titlePdo::param_str);$stmt->bindparam (2,$contentPdo::param_str);$stmt->bindparam (3,$idPdo::param_int);$stmt-execute ();//Method 4$stmt=$db->prepare (' UPDATE posts SET post_title =: Title, Post_content =: content WHERE id =: ID ');$stmt->bindparam (': Title ',$titlePdo::param_str);$stmt->bindparam (': Content ',$contentPdo::param_str);$stmt->bindparam (': Id ',$idPdo::param_int);$stmt-execute (); Mysqli://Mysqli only needs to perform bind_param once, which is more concise than PDO, MYSQLI does not support named placeholders.$stmt->bind_param (' SSI ',$title,$content,$id);
PHP Common Code