-1

So I recently updated my PHP to 7 and now my blog stopped working, I know it has to do with the outdated code, but I can't really figure out a way to update it, any suggestions?

This is my code

<?php

function add_post(){
$title      = mysql_real_escape_string($title);
$contents   = mysql_real_escape_string($contents);

mysql_query("INSERT INTO `posts` SET
            `title`      = '{$title}',
            `contents`   = '{$contents}',
            `date_posted`= NOW()");
}
function edit_post($id,$title,$contents)
{
$id = (int)$id;
$title = mysql_real_escape_string($title);
$contents = mysql_real_escape_string($contents);
}

function delete($table, $id){
$table = mysql_real_escape_string($table);
$id    = (int)$id;
mysql_query("DELETE FROM `{$table}` WHERE `id`= {$id} ");

}
function get_posts($id = null, $cat_id = null){
$posts = array();
$query = "SELECT
          `posts`.`id` AS `post_id` ,
           `title`,`contents`,`date_posted`.`name`
           FROM `posts`
           INNER JOIN `categories` ON `categories`.`id` = `posts`.`cat_id` " ;
if(isset($id)){
    $id = (int)$id;
    $query .= " WHERE `posts`.`id` = {$id} ";
         }
if(isset($cat_id)){
    $cat_id = (int)$cat_id;
    $query .= " WHERE `cat_id` = {$cat_id} ";
         }         

$query .= "ORDER BY `post_id` DESC";




   return $posts;
}

Thanks for your time.

  • The mysql extension has been removed from 7.0.0. You should switch to mysqli (note the extra i) or PDO. – Decent Dabbler May 24 '17 at 10:02
  • Better use `mysqli` or `PDO` as `mysql` is deprecated . More Info here :https://www.w3schools.com/php/php_mysql_connect.asp – Vikrant May 24 '17 at 10:03
  • 1
    @Vikrant don't send newbies to w3schools. That site is harmful. – tereško May 24 '17 at 10:03
  • We need to use pdo for school and they advice w3schools, I'll take a look – Kim Allenboy Nijenhuis May 24 '17 at 10:04
  • use mysqli or PDO... for mysqli replace `mysql_*` with `mysqli_*` and first parameter to the function must be connection object. eg: `mysqli_query($con,"YOUR QUERY")`... Note this is quick approach... better approach is use PDO with prepared statement – undefined_variable May 24 '17 at 10:04
  • @KimAllenboyNijenhuis for migrating to PDO my recommendation would be to use this article: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers. And I am serious - don't go to w3schools. – tereško May 24 '17 at 10:07

1 Answers1

-2

Mysql_real_escape_string and mysql_query were removed in php 7

you can use Mysqli as a replacement.

http://php.net/manual/en/mysqli.real-escape-string.php

rchatburn
  • 732
  • 4
  • 13