调用未定义的函数mysql_connect()使用WP时出错

时间:2017-02-11 01:38:46

标签: php wordpress

我有一个位于PHP 5.6之上的WordPress站点版本4.7.2。我正在使用PHP Code for Posts插件来调出一个单独的活动。它在更新到4.7之后工作,但随后某个地方的PHP代码开始抛出错误

  

调用未定义的函数mysql_connect()。

我使用的代码是

mysql_connect ("localhost", "database-user","database-password") or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database-name");

$url = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$postid = url_to_postid( $url );

$query = "SELECT slug FROM all_cases WHERE wp_lesson_post_id = $postid";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

$slug = $row['slug'];

echo '<script type="text/javascript">
           window.location = "https://sitename'.$slug.'"
      </script>';

//header("Location: https://sitename/$slug");

我也尝试过PHP Code Snippit并得到同样的错误。关于如何解决这个问题的任何想法?

更新

我不熟悉PHP。它会是这样的吗?

$host = 'localhost';
$db   = 'database-name';
$user = 'user-name';
$pass = 'password';
$charset = 'utf8';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
$pdo = new PDO($dsn, $user, $pass, $opt);

$url = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$postid = url_to_postid( $url );

$query = "SELECT slug FROM all_cases WHERE wp_lesson_post_id = $postid";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

$slug = $row['slug'];

echo '<script type="text/javascript">
           window.location = "https://sitename'.$slug.'"
      </script>';

header("Location: https://sitename/$slug");

0 个答案:

没有答案