PHP mysqli_real_escape_string返回空字符串 [英] PHP mysqli_real_escape_string returning empty string

查看:33
本文介绍了PHP mysqli_real_escape_string返回空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我不使用mysql_real_escape_string函数,则代码可以正常工作.但是函数什么也没返回!我读到问题可能是由于我没有mysql连接,但事实并非如此!

The code works fine if I don't use the mysql_real_escape_string function. But the function is returning nothing! I read that the problem may be due to the fact that I do not have a mysql connection but that does not seem to be the case!

请帮助!

<?php
$con=mysqli_connect("localhost","root","pwd","mysql");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$title = mysqli_real_escape_string($_POST["title"]);
$comment = mysqli_real_escape_string($_POST["comment"]);
$type = $_POST["type"];
$time = date("Y-m-d H:i:s");


$sql="INSERT INTO posts
VALUES
('','$type','$time','$time','$title','$comment','0','0','0','0','0')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }



mysqli_close($con);
header ("location: index.php");
?>

推荐答案

您需要将连接传递给函数

You need to pass the connection to the function

$title = mysqli_real_escape_string($con, $_POST["title"]);

这篇关于PHP mysqli_real_escape_string返回空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆