i try convert my php old fashion php connection to mysqli or PDO

connection.php

<?php
//---abstracted ! ------
class connection{
    public $host ="yourHostIp";
    public $user = "root";
    public $password = "";
    public $db="artiste";
    public $dbc;
   
    function __construct() {
        $con = mysqli_connect($this->host, $this->user, $this->password, $this->db);
       
        if(mysqli_errno($con)){
            echo"sum error";
           
        }
        else{
           $this->dbc = $con; // assign $con to $dbc
           echo"connected ";
        }
    }
}
?>

Open in new window


index.php

<?php
require_once('connexion.php');



session_start(); // début de session
$test = new connection();


?>




gives this  3 warnings; its a bit rough


( ! ) Warning: mysqli_connect(): in C:\wamp\www\kathleen3\php\login\connexion.php on line 11
Call Stack
#      Time      Memory      Function      Location
1      0.0010      133640      {main}( )      ..\index2.php:0
2      0.0010      138440      connection->__construct( )      ..\index2.php:16
3      0.0010      138472      mysqli_connect ( )      ..\connexion.php:11

( ! ) Warning: mysqli_connect(): in C:\wamp\www\kathleen3\php\login\connexion.php on line 11
Call Stack
#      Time      Memory      Function      Location
1      0.0010      133640      {main}( )      ..\index2.php:0
2      0.0010      138440      connection->__construct( )      ..\index2.php:16
3      0.0010      138472      mysqli_connect ( )      ..\connexion.php:11

( ! ) Warning: mysqli_errno() expects parameter 1 to be mysqli, boolean given in C:\wamp\www\kathleen3\php\login\connexion.php on line 13
Call Stack
#      Time      Memory      Function      Location
1      0.0010      133640      {main}( )      ..\index2.php:0
2      0.0010      138440      connection->__construct( )      ..\index2.php:16
3      2.2551      138992      mysqli_errno ( )      ..\connexion.php:13
connected
jerrrrryAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ahmed MerghaniSoftware EngineerCommented:
Hi jerrrrry,

What is the version of PHP?
Ray PaseurCommented:
Connection errors are reported differently from SQL query failures.  It's all in this article, with code examples.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);

// DID THE CONNECT/SELECT WORK OR FAIL?
if ($mysqli->connect_errno)
{
    $err
    = "CONNECT FAIL: "
    . $mysqli->connect_errno
    . ' '
    . $mysqli->connect_error
    ;
    trigger_error($err, E_USER_ERROR);
}

Open in new window

Ray PaseurCommented:
I think in your code sample, I would try it like this.  However this is untested code and may need your attention to ensure it works correctly.

<?php
//---abstracted ! ------
class connection
{
    public $host ="yourHostIp";
    public $user = "root";
    public $password = "";
    public $db = "artiste";
    public $dbc;
   
    public function __construct() 
    {
        $this->dbc = new mysqli($this->host, $this->user, $this->password, $this->db);
       
        // DID THE CONNECT/SELECT WORK OR FAIL?
        if ($this->dbc->connect_errno)
        {
            $err
            = "CONNECT FAIL: "
            . $this->dbc->connect_errno
            . ' '
            . $this->dbc->connect_error
            ;
            trigger_error($err, E_USER_ERROR);
        }
    }
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jerrrrryAuthor Commented:
hello what i want is a simple connection that i can reuse in my script with include_once, your script gave me this

php is 5.5.12

( ! ) Warning: mysqli::mysqli(): in C:\wamp\www\kathleen3\php\login\connexion.php on line 13
Call Stack
#      Time      Memory      Function      Location
1      0.0000      143760      {main}( )      ..\index.php:0
2      0.0000      149176      connection->__construct( )      ..\index.php:16
3      0.0156      149976      mysqli ( )      ..\connexion.php:13

( ! ) Warning: mysqli::mysqli(): in C:\wamp\www\kathleen3\php\login\connexion.php on line 13
Call Stack
#      Time      Memory      Function      Location
1      0.0000      143760      {main}( )      ..\index.php:0
2      0.0000      149176      connection->__construct( )      ..\index.php:16
3      0.0156      149976      mysqli ( )      ..\connexion.php:13

( ! ) Fatal error: in C:\wamp\www\kathleen3\php\login\connexion.php on line 24
Call Stack
#      Time      Memory      Function      Location
1      0.0000      143760      {main}( )      ..\index.php:0
2      0.0000      149176      connection->__construct( )      ..\index.php:16
3      2.3088      150584      trigger_error ( )      ..\connexion.php:24
Ray PaseurCommented:
Looks like the connection failed.  Possibly the credentials are incorrect?  This is your web site, not mine, and I cannot test these things, so we depend on your input to help debugging.  Can you connect to MySQL using the same credentials?  If so, you should be able to connect to MySQLi.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.