什么错这个PHP mysql_real_escape_string
Atomic Number Latin English Abbreviation* check the variables for content */
/*** a list of filters ***/ $filters = array( 'searchtext' => array('filter' => FILTER_CALLBACK, 'options' => 'mysql_real_escape_string'), 'fieldname' => array('filter' => FILTER_CALLBACK, 'options' => 'mysql_real_escape_string') ); /*** escape all POST variables ***/ $input = filter_input_array(INPUT_POST, $filters); /*** check the values are not empty ***/ if(empty($input['fieldname']) || empty($input['searchtext'])) { echo 'Invalid search'; } else { /*** mysql hostname ***/ $hostname = 'localhost'; /*** mysql username ***/ $username = 'username'; /*** mysql password ***/ $password = 'password'; /*** mysql database name ***/ $dbname = 'periodic_table'; /*** connect to the database ***/ $link = @mysql_connect($hostname, $username, $password); /*** check if the link is a valid resource ***/ if(is_resource($link)) { /*** select the database we wish to use ***/ if(mysql_select_db($dbname, $link) === TRUE) { /*** sql to SELECT information***/ $sql = sprintf("SELECT * FROM elements WHERE %s = '%s'", $input['fieldname'], $input['searchtext']); /*** echo the sql query ***/ echo '<h3>'.$sql.'</h3>'; /*** run the query ***/ $result = mysql_query($sql); /*** check if the result is a valid resource ***/ if(is_resource($result)) { /*** check if we have more than zero rows ***/ if(mysql_num_rows($result) !== 0) { echo '<table>'; while($row=mysql_fetch_array($result)) { echo '<tr> <td>'.$row['atomicnumber'].'</td> <td>'.$row['latin'].'</td> <td>'.$row['english'].'</td> <td>'.$row['abbr'].'</td> </tr>'; } echo '</table>'; } else { /*** if zero results are found.. ***/ echo 'Zero results found'; } } else { /*** if the resource is not valid ***/ 'No valid resource found'; } } /*** if we are unable to select the database show an error ****/ else { echo 'Unable to select database '.$dbname; } /*** close the connection ***/ mysql_close($link); } else { /*** if we fail to connect ***/ echo 'Unable to connect'; } } }
其他 { 回声 '请选择一个元素'; } ?>
我从phppro.org教程网站得到了这段代码,我试图运行它。它给出 警告:mysql_real_escape_string()[function.mysql-real-escape-string]:无法建立到服务器的链接。 ... 警告:mysql_real_escape_string()[function.mysql-real-escape-string]:拒绝用户'ODBC'@'localhost'(使用密码:否)的访问....
我去了php.net并查找它“注意:在使用mysql_real_escape_string()之前需要使用MySQL连接,否则会生成级别为E_WARNING的错误,并返回FALSE。如果未定义link_identifier,则使用最后一个MySQL连接。
我的问题是: 1 - 为什么他们把单引号mysql_real_escape_string? 2-他们应该先建立一个连接,然后使用$ filter filter语句和mysql_real_escape_string?
SQL注入检测 – 2010-03-24 22:20:13
@ Col.Shrapnel的内容:不,全filter \ _input \ _array()thingy应该在\ _POST的每个元素上调用real \ _escape \ _string并将结果存储在$ input中。如果它按预期工作,那么就不会有sql注入漏洞。 – VolkerK 2010-03-24 22:47:47
@VolkerK那么是什么? real_escape_string不是让你“安全”的魔法。 – 2010-03-24 22:57:29