Database connection [closed]

-2

I'm having a problem making the connection to the Mysqli Database, is giving several errors, follow the errors below:

PHP Warning:  mysqli_query() expects parameter 1 to be mysqli, null given in D:\web\localuser\riobonitopiscinas\www\acessorios-para-construcao.php on line 63
PHP Warning:  mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in D:\web\localuser\riobonitopiscinas\www\acessorios-para-construcao.php on line 66
PHP Warning:  mysqli_close() expects parameter 1 to be mysqli, null given in D:\web\localuser\riobonitopiscinas\www\acessorios-para-construcao.php on line 84

And my page code is:

<?php
   include ('admin/include/conexao.php');

   $result = mysqli_query($con,"SELECT * FROM produto WHERE id_subcategoria=78");

   while($row = mysqli_fetch_array($result)) {
?>

<?php include ('includes/box.php'); ?>

<?php                       
   }  
   mysqli_close($con);
?>

My connection code:

<?php

$parseini = parse_ini_file('admin/ini/config.ini', true);

$dsn = sprintf('mysql:dbname=%s;host=%s',$parseini['bancodedados']['dbname'], $parseini['bancodedados']['host']);

try {
    $conexao = new PDO($dsn, $parseini['bancodedados']['usuario'], $parseini['bancodedados']['senha']);
} catch (PDOException $e) {
    die('Connection failed: ' . $e->getMessage());
}
?>
    
asked by anonymous 30.11.2018 / 13:09

2 answers

1

You have two errors in your code.

  • which does not even exist in your code.

  • You open a connection using PDO as a connection and then you use mysqli methods. PDO and Mysqli are two ways to connect to the bank in php, choose one and use it.

  • If you change the connection to mysqli should work since you use the mysqli methods in the other file, follow below using mysqli to connect.

    connection.php

    $parseini = parse_ini_file('admin/ini/config.ini', true);
    
    $con = mysqli_connect($parseini['bancodedados']['host'], $parseini['bancodedados']['usuario'], $parseini['bancodedados']['senha'], $parseini['bancodedados']['dbname']);
    
    if (mysqli_connect_errno()) {
      echo "Erro ao conectar no mysql: " . mysqli_connect_error();
    }
    

    Useful links: mysqli pdo

        
    30.11.2018 / 14:34
    0

    Your code has an error in the variable $con note that in your connection file you use $conexao

    try this way:

    <?php
       include ('admin/include/conexao.php');
    
       $result = mysqli_query($conexao,"SELECT * FROM produto WHERE id_subcategoria=78");
    
       while($row = mysqli_fetch_array($result)) {
    ?>
    
    <?php include ('includes/box.php'); ?>
    
    <?php                       
       }  
       mysqli_close($conexao);
    ?>
    
        
    30.11.2018 / 14:30