Connection to a Database

Asked

Viewed 120 times

-2

I’m having a problem connecting to the Mysqli database, is making several mistakes, follow the mistakes 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 code of the connection :

<?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());
}
?>
  • 3

    "I’m with a problem in making connection with the Bank", If the problem is with the connection, you need to show us the file that makes the connection. All errors cited are null object, which indicates that their variable $con is void.

  • <?php $parseini = parse_ini_file('admin/ini/config.ini', true); $dsn = sprintf('mysql:dbname=%s;host=%s',$parseini['bancodedados']['dbname'], $parseini['bancodedados']['host']); Try { $connection = new PDO($dsn, $parseini['bancodedados']['user'], $parseini['bancodedados']['password']); } catch (Pdoexception $e) { die('Connection failed: ' . $e->getMessage()); } ?>

  • 1

    @Vinicius, put the connection code in the question itself and not here in the comments.

  • Ready this in the question

  • You connect the bank in $conexao, but uses $con? Why is that?

2 answers

1

There are two errors in your code.

  1. It has already been pointed out by other users in the comments is that you are initiating the connection with mysql and saving it in the variable $connected (in the connected file.php), when using the connection you use the variable $con that doesn’t even exist in your code.

  2. 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 php database, choose one and use it.

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

php connection.

$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();
}

Link uteis: mysqli Pdo

0

your code is with error in 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);
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.