$db = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$name = 'zhangsan"';
$db->query('select * from test where name = "' . mysql_real_escape_string($name) . '"');
/*
结果:Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO)Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established
$db = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$name = 'zhangsan"';
$db->query('select * from test where name = "' . mysql_real_escape_string($name, $db) . '"');
/*
结果:Warning: mysql_real_escape_string() expects parameter 2 to be resource, object given
*/我的mysql实实在在的连上了,不知道这个到底该如何使用呢?

解决方案 »

  1.   

    两个星星的问题,那只有让大哥来回答你的问题了手册上是这么说的
    本函数将 unescaped_string中的特殊字符转义,并计及连接的当前字符集,因此可以安全用于 mysql_query()。
    <?php
    $item = "Zak's and Derick's Laptop";
    $escaped_item = mysql_real_escape_string($item);
    printf ("Escaped string: %s\n", $escaped_item);
    ?> 以上例子将产生如下输出: 
    Escaped string: Zak\'s and Derick\'s Laptop
      

  2.   

    你怎么在混用 PDO 和 mysql函数集呢?PDO 已经提供了  quote 方法再说虽然你现在传给 PDO 的 dsn 是mysql,但下次出给他的是 mssql 那由如何转义呢?
      

  3.   

    谢谢1楼,在mysql_connect中完全没有问题,值发生在PDO连接的数据库中,差了下baidu、google什么的但是依然没有个正确答案
      

  4.   


    怪我没看仔细,原来在pdo中用quote代替了那个方法了