mysqli_connect does not connect

Asked

Viewed 114 times

-1

I’m having difficulty solving the connection with php and BD

On the page of my project he presents the following message:

Warning: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, Boolean Given in D: wamp64 www Plha Index.php on line 71

this part of the program refers to a slide of images that searches the path in the BD

<div id="carouselSite" class="carousel Slide" data-ride="carousel">
        <a  href="#" data-toggle="modal" data-target="#siteModalWork" class="work">
        <button type="button" class="btn btn-success">Work it us</button>
        </a>

        <ol class="carousel-indicators">
                <div class="carousel-inner">
                        <?php
                        $control_active = 2;
                        $control_position = 1;
                        $cal_carousel = "SELECT * FROM carousel ORDER BY id ASC";

                        $result_carousel = mysqli_query ($con,$cal_carousel) or mysqli_error($con);
                        while($row_carousel = mysqli_fetch_assoc($result_carousel)){
                            if($control_active == 2){?>
                                <li data-target="#carouselSite" data-slide-to="0" class="active"></li>
                                <?php
                        $control_active = 1;
                        }else{?>
                            <li data-target="#carouselSite" data-slide-to="<?php echo $control_position;?>"></li><?php

                            $control_position++;
                        }}?>
        </ol>

        <div class="carousel-inner">
                <?php
                $control_active = 2;
                $cal_carousel = "SELECT * FROM carousel ORDER BY id ASC";
                $result_carousel = mysqli_query ($con,$cal_carousel);
                while($row_carousel = mysqli_fetch_assoc($result_carousel)){
                    if($control_active == 2){?>
            <div class=" carousel-item active">

                <img src="img/<?php echo $row_carousel ['imgCarousel']; ?>"  class="img-fluid d-block cover">

            </div><?php
                $control_active = 1;
                }else{?>
            <div class=" carousel-item">

                    <img src="img/<?php echo $row_carousel ['imgCarousel'];?>" class="img-fluid d-block cover">

                </div><?php
                }}?>


        </div>
        <a class="carousel-control-prev" href="#carouselSite" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
        </a>
        <a class="carousel-control-next" href="#carouselSite" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>
    </div>

But I have other applications that connect to the BD that get no answer

the $con variable it receives the connection by another file, the connect.php file

<?php
    $servidor = "localhost";
    $usuario = "root";
    $senha = "";
    $dbname = "plathanus";
    $con;

    $con = mysqli_connect($servidor,$usuario,$senha,$dbname) or mysqli_error($con);

Someone could give me a light?

  • This may help https://answall.com/questions/28184/erro-no-mysql-expects-parameter-1-to-be-resource-boolean-given-in

  • 1

    Why not use PDO?

2 answers

1


$result_carousel = mysqli_query ($con,$cal_carousel) or mysqli_error($con);

I believe you’ve forgotten the die:

$result_carousel = mysqli_query ($con,$cal_carousel) or die(mysqli_error($con));

The error that is appearing says that the mysqli_fetch_assoc() expects a variable of the type mysqli_result and actually gets one of the boolean type.

That one or caused the $result_carousel be treated as a Boolean

  • 2

    Thank you very much Ronaldo!

-1

Refactored code for a better understanding, using PDO as well as fault identification through the getMessage method();

<?php

$dsn = "mysql:dbname=db_sistema;host=localhost";
$dbuser = "root";
$dbpass = "";

try{
    $pdo = new PDO($dsn,$dbuser,$dbpass);
    if($pdo){
        echo "conexão efetuada com sucesso";
    }
}catch(PDOException $e){
    echo "Falhou a conexão: ". $e->getMessage();
}
  • Interesting! I am new to programming, and was unaware of this method, in case for me to use this Pdo the $Pdo variable would be the variable I was going to call in myslqi_query?

Browser other questions tagged

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