Unidentified database: "No database Selected"

Asked

Viewed 70 times

0

I’m making a shopping cart file. The code is apparently correct, but when including the file connecting to the database, the browser displays the message "In the database".

Note: Both the file, called "Request.php", and the connection to the database (connection.php) are in the same directory.

Here is the code:

    <?php
        include 'conexao.php'; 
        session_start();

        if(!isset($_SESSION['carrinho'])){
            $_SESSION['carrinho'] = array();
        }

        //ADICIONA PRODUTO

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

            //ADICIONA CARRINHO

            if($_GET['acao'] == 'add'){

                $id = intval($_GET['id']);
                if(!isset($_SESSION['carrinho'][$id])){
                    $_SESSION['carrinho'][$id] = 1;
                }else{
                    $_SESSION['carrinho'][$id] += 1;
                }
            }

        }



    ?>

    <table>

        <caption>Carrinho de compras</caption><br>

        <thead>
            <tr>
            <br><br>
                <th width="244">Produto</th>
                <th width="79">Quantidade</th>
                <th width="89">Preço    </th>
                <th width="100">Subtotal</th>
                <th width="64">Remover</th>
            </tr>
        </thead>

        <form action="?acao=up" method="POST">

        <tfoot>
            <tr>
            <td colspan="5"><input type="submit" value="Atualizar carrinho"/></td>
            </tr>
            <tr>
            <td colspan="5"><a href="principal.php">Continuar comprando</a></td>
            </tr>
        </tfoot>

        <tbody>

            <?php


                if(count($_SESSION['carrinho']) == 0){
                    echo '<tr><td colspan="5">Não há produtos no carrinho</tr>';
                }

                else{

                    foreach($_SESSION['carrinho'] as $id => $qtd){

                        $sql     = "SELECT * FROM produto WHERE id= '$id'";
                        $result  = mysql_query($sql) or die (mysql_error());
                        $produto = mysql_fetch_assoc($result);

                        $nome    = $produto['nome'];
                        $preco   = number_format($produto['preco'], 2, ',', '.');
                        $sub     = number_format($produto['preco'] * $qtd, 2, ',', '.');


                        echo '<tr>
                                    <td>'.$nome.'</td>
                                    <td><inpu type="text" size ="3" name="produto['.$id.'] value="'.$qtd.'"></td>
                                    <td>'.$preco.'</td>
                                    <td>'.$sub.'</td>
                                    <td><a href=?acao=del&id='.$id.'>Remover</a></td>
                              </tr>';
                    }

                }

            ?>


        </tbody>
        </form>

    </table>
</body>

2 answers

0

In your connection file, Voce needs to enter the name of the database. Here is an example:

<?php
$conn = mysql_connect(“localhost”, “root”, “123”);
$db = mysql_select_db(“dados”, $conn);
$query = mysql_query(“SELECT * FROM clientes”);
?>

A tip: Try to update your code and use Mysqli or PDO

http://blog.thiagobelem.net/guia-pratico-de-mysqli-no-php

0

This part I put in the file itself "conexao.php":

exec("set Names utf8"); }catch(Pdoexception $e){ echo "Error connecting with Mysql:". $e->getMessage(); } ?>

<?php
$conn = mysql_connect(“localhost”, “root”, “123”);
$db = mysql_select_db(“dados”, $conn);
$query = mysql_query(“SELECT * FROM clientes”);
?>

Browser other questions tagged

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