How to put db_connect with my_sqli

Asked

Viewed 189 times

1

My code:

<?php


class DB_CONNECT {

// constructor
function __construct() {
    // connecting to database
    $this->connect();
}

// destructor
function __destruct() {
    // closing db connection
    $this->close();
}

/**
 * Function to connect with database
 */
function connect() {
    // import database connection variables
    require_once __DIR__ . '/db_config.php';

    // Connecting to mysql database
    $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysqli_error());

    // Selecing database
    $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());

    // returing connection cursor
    return $con;
}
/**
 * Function to close db connection
 */
function close() {
    // closing db connection
    mysql_close();
}

}

?>

To solve superficially I saw that putting a @ in front of my_sql_connect would work.

 // Connecting to mysql database
    $con = @mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysqli_error());

But I see that soon will become obsolete, as would the code with my_sqli?

  • If there is no canonical answer to this, it should exist. Surely there are several questions that have already answered this.

  • Migrate from mysql to mysqi?

  • I’m sure I’ve been looking into this, and unfortunately I haven’t found anything that works for me. I read that practically it was only change what was written with my_sql to my_sqli, but the error in the parameters.

  • Syntax changes, for all functions mysqli_*(procedural mode) the first argument is always the connection.

  • 2

    The coolest of this question is that by putting a @ solves the problem: http://answall.com/q/50166/101

  • Would it be OK to leave @ Since it works, but it would be a problem in the future?

  • @bigown Just to clarify: does not solve the problem. Before PHP 7, no errors appear about the functions mysql_*, only warnings or warnings. The @ serves only to say "do not show warnings for functions prefixed with @". To not show errors one can resort to ini_set('display_errors', 'Off'); error_reporting(E_ALL);

  • 2

    @Eduardoalmeida you don’t understand the joke

  • 1

    @bigown Sorry!

Show 5 more comments

1 answer

0

connection is made with

$conexao = mysqli_connect("localhost", "user", "pass", "banco_de_dados");

and closed with

mysqli_close($conexao); 

Browser other questions tagged

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