Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 要存储在数据库中的文本在撇号前结束_Javascript_Php_Mysql_Stored Procedures_Pdo - Fatal编程技术网

Javascript 要存储在数据库中的文本在撇号前结束

Javascript 要存储在数据库中的文本在撇号前结束,javascript,php,mysql,stored-procedures,pdo,Javascript,Php,Mysql,Stored Procedures,Pdo,我有一个存储过程要存储在数据库中。如下所示: USE `login`; DROP procedure IF EXISTS `post_and_fetch_ans`; DELIMITER $$ USE `login`$$ CREATE DEFINER=`root`@`localhost` PROCEDURE `post_and_fetch_ans`(IN answerbody LONGTEXT,IN postid int,IN answerer int) BEGIN INSERT INTO

我有一个存储过程要存储在数据库中。如下所示:

USE `login`;
DROP procedure IF EXISTS `post_and_fetch_ans`;

DELIMITER $$
USE `login`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `post_and_fetch_ans`(IN answerbody LONGTEXT,IN postid int,IN answerer int)
BEGIN
   INSERT INTO `login`.`answers` (answer_body,userpost_post_id,users_user_id) VALUES (answerbody,postid,answerer) ;
   SELECT * FROM `login`.`answers` WHERE userpost_post_id = postid  ORDER BY answer_date DESC LIMIT 1;
END$$

DELIMITER ;
为了将文本存储在数据库中,我必须在PDO prepared语句中使用存储过程将值绑定到查询字符串。

并使用bindValue方法绑定参数:

public function post_and_fetch($sql,$params=array()){
            $this->_error=false;

             if($conn=$this->_pdo->prepare($sql)){
             $x=1;
                if(count($params)){
                  foreach($params as $param){

                      $conn->bindValue($x,$param);
                      $x++;

                  }

              }
              if($conn->execute()){

                  $this->_results=$conn->fetchAll(PDO::FETCH_OBJ);

                  $this->_count=$conn->rowCount();
                  return $this;

               }else{

                 $this->_error=true;

              }

         }
        }
但是当我试图保存一个字符串时

这是"考验"

只有

这是一个


存储在数据库中。如何在将文本存储到数据库之前从该文本中转义撇号。将收到任何帮助?

在传递到函数之前,您可以对值使用mysql\u real\u escape\u string

 $str = "Zak's and Derick's Laptop";
 $str_with_escape = mysql_real_escape_string($str);

然后,对于“解码”,您必须替换以下字符中的前缀反斜杠:\x00、\n、\r、\、',“和\x1a.

mysql\u real\u escape\u字符串从php 7.0开始就不推荐使用。此外,我也没有使用mysqli…我使用PDO来完成工作..如果我将数据库从mysql更改为其他形式,会发生什么?如果您不想要面向mysql的解决方案,则必须定义适当的字符串操作,避免使用特定的db相关函数。。对于编码和解码(一对存储过程可能是正确的解决方案),环顾四周,我没有找到任何pdo的通用解决方案。。
 $str = "Zak's and Derick's Laptop";
 $str_with_escape = mysql_real_escape_string($str);