Error when consulting Remote Database PHP 7

-1

I have a type 500 error when starting this code

When consulting the database my server only has PHP7 available As I read it may be because my code only works with PHP5

The code should show a series of queries, names and numbers but only send me the 500 error, it is worth mentioning that other free hosts if it works .     

include_once "includes/variables.php";
DEFINE ('DB_HOST', $host);
DEFINE ('DB_USER', $user);   
DEFINE ('DB_PASSWORD', $pass);
DEFINE ('DB_NAME', $database);

$mysqli = @mysqli_connect ($host,$user, $pass) OR die ('Could not connect to MySQL');
//mysqli_select_db ("DB_NAME") OR die (mysqli_error($mysqli));

   mysqli_select_db($mysqli, 'DB_NAME') or die(mysqli_error($mysqli));

mysql_query("SET NAMES 'utf8'"); 
//mysql_query('SET CHARACTER SET utf8');

if(isset($_GET['cat_id']))
{

        $query="SELECT image AS 'images', category_name AS 'cat_name', cid FROM tbl_category c, tbl_gallery n WHERE c.cid=n.cat_id and c.cid='".$_GET['cat_id']."' ORDER BY n.id DESC";         
        $resouter = mysql_query($query);

}
else if(isset($_GET['latest']))
{
        $limit=$_GET['latest'];     

        $query="SELECT * FROM tbl_category c,tbl_gallery n WHERE c.cid=n.cat_id ORDER BY n.id DESC LIMIT $limit";           
        $resouter = mysql_query($query);
}
else if(isset($_GET['custom_category']))
{       
        $query = "SELECT * FROM tbl_category c, tbl_gallery n WHERE c.cid = n.cat_id AND c.category_name = 'Nature' ORDER BY n.id DESC";            
        $resouter = mysql_query($query);
}
else
{   
        $query="SELECT * FROM tbl_category ORDER BY cid DESC";          
        $resouter = mysql_query($query);
}

$set = array();

$total_records = mysql_num_rows($resouter);
if($total_records >= 1){

  while ($link = mysql_fetch_array($resouter, MYSQL_ASSOC)){

    $set['MaterialWallpaper'][] = $link;
  }
}

 echo $val= str_replace('\/', '/', json_encode($set));


?>
    
asked by Bryan B Weiss 16.02.2017 в 17:14
source

1 answer

1

You are trying to run your querys using mysql_query, a function that is part of the php-mysql extension, and that does not exist in PHP7. On the other hand, I do not know how that code works on other servers, because you are not declaring a connection with the appropriate driver.

Anyway. Your queries must have the form $mysqli->query("SELECT ...")

For example, where it says:

$query="SELECT image AS 'images', category_name AS 'cat_name', cid FROM tbl_category c, tbl_gallery n WHERE c.cid=n.cat_id and c.cid='".$_GET['cat_id']."' ORDER BY n.id DESC";         
$resouter = mysql_query($query);

should say:

$query="SELECT image AS 'images', category_name AS 'cat_name', cid FROM tbl_category c, tbl_gallery n WHERE c.cid=n.cat_id and c.cid='".$_GET['cat_id']."' ORDER BY n.id DESC";         
$resouter = mysqli->query($query);

I take the opportunity to mention that the code you are showing has a serious SQL injection problem. It suffices to manipulate the parameter cat_id to inject an arbitrary query.

If you do not have time to learn about prepared sentences, at least you could cast the cat_id to integer (if it is a number) and use that variable in your query:

$cat_id=intval($_GET['cat_id']);
$query="SELECT image AS 'images', category_name AS 'cat_name', cid FROM tbl_category c, tbl_gallery n WHERE c.cid=n.cat_id and c.cid=$catid ORDER BY n.id DESC";         
$resouter = $mysqli->query($query);
    
answered by 16.02.2017 в 18:36