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