Is there a syntax error in this php code?

Asked

Viewed 80 times

-3

I’m still beginner in php, here’s the code, here’s giving syntax error

<?php
function conexao(){ 
  $banco = "curosphp_mysql";
  $usuario = "root";
  $senha = "";
  $host = "localhost";
  mysqli_select_db($conn, $banco);  
}
 conexao();
?>

Update:

<?php 
function conexao(){ 
    $banco = "curosphp_mysql";
    $usuario = "root";
    $senha = "";
    $host = "localhost";
    $conn = mysqli_connect($host, $usuario, $senha, $banco); 
    mysqli_select_db($conn, $banco); 
} 

conexao();
?> 
  • 2

    $conn does not exist in the scope of the function and another future problem ... its function does not return value (the connection in particular).

  • 1

    Well, the variable $conn, that in mysqli_select_db does not exist, so yes, there is the error.

  • $Conn = mysqli_connect($host, $user, $password, $bank);

  • this code solves the problem?

  • Now you are giving the error giving you an unexpected } please help me

  • How is your code?

  • <?php Function connected(){ $bank = "curosphp_mysql"; $user = "root"; $password = ""; $host = "localhost"; $Conn = mysqli_connect($host, $user, $password, $bank); mysqli_select_db($Conn, $bank); } connected(); ?>

  • The documentation has complete examples of how to do, both procedural and OOP: http://php.net/manual/en/book.mysqli.php - documentation is made for this very thing, to read, to learn, to study, and more specific doubts that escape the basic use of Apis bring here to the site, see example of procedural connection explained in doc: http://php.net/manual/en/mysqli.construct.php#example-1784

Show 3 more comments

1 answer

0


You don’t need mysqli_select_db($conn, $banco); if you are not switching databases.

<?php 
    function conexao(){
        $host = "localhost";
        $usuario = "root";
        $senha = "";
        $banco = "curosphp_mysql";

        $con = mysqli_connect($host, $usuario, $senha, $banco);

        // Não precisa do código abaixo, porque você não está trocando de banco de dados
        // mysqli_select_db($con, $banco);

        return $con;
    } 

    conexao();
  • 1

    Thank you very much, it was right :)

Browser other questions tagged

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