1

I have a WordPress site version 4.7.2 sitting on top of PHP 5.6. I am using PHP Code for Posts plugin to call out a separate activity. It was working after updating to 4.7 but then somewhere along the line the php code started throwing an error of

Call to undefined function mysql_connect().

The code I'm using is

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");

I also tried PHP Code Snippit and am getting the same error. Any idea on how to fix this?

UPDATE

I'm not very versed with PHP. So would it look something like this?

$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");
  • 1
    You are absolutely sure you're not on PHP 7? – Jeff Feb 11 '17 at 01:42
  • 1
    anyway, you should not use mysql_* anymore. use mysqli_* or PDO! – Jeff Feb 11 '17 at 01:43
  • 1
    And while you're at it, when using MySQLi or PDO - make sure to use prepared statements with placeholders for every variable. – Qirel Feb 11 '17 at 01:43
  • ...looks like a perfect opportunity to update your db-functions! – Jeff Feb 11 '17 at 01:44
  • On top of that, why did you comment out your `header("Location: ..");` and use JS instead? – Qirel Feb 11 '17 at 01:46
  • @Qirel My guess: `header` didn't work, because of the error-message. – Jeff Feb 11 '17 at 01:53
  • This code was supplied by someone else. I'm not sure why it was commented out. – user2514362 Feb 11 '17 at 01:54
  • Why are you using any of this in WordPress? You should be using either the WordPress WP_Query or $wpdb or the WPAPI – topdown Feb 11 '17 at 02:54
  • While your edit contains a setup for PDO, you never actually use the PDO object. I would suggest reading the SO Docs on PDO starting with [this](http://stackoverflow.com/documentation/php/5828/pdo/2685/preventing-sql-injection-with-parameterized-queries) – Machavity Feb 11 '17 at 03:13

0 Answers0