Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error on mysqli::connect_errno

Tags:

php

mysqli

I'm trying to start working with mysqli and OOP, but it isn't working out that well. I can't get my mysqli methods working starting with a simple connect_errno method.

Can anyone help me out?

The connection to the database seems to be made, since I tried it with an other Password (access denied). With the correct Password I got the following error:

Fatal error: Call to undefined method mysqli::connect_errno() in C:\xampp2\htdocs\badeendenrace\db\db.php on line 7

<?php
    $dbhost='localhost';
    $dbuser='Tjerk';
    $dbdata='badeendenrace';
    $dbpw='123test';
    $db = new mysqli($dbhost,$dbuser,$dbpw,$dbdata);
    if ($db->connect_errno())
    {
        echo "Connection failed: ".$db->connect_error()."\n";
        exit();
    }
?>

I'm using the latest version of Xampp (Reïnstalled it today) with PHP version 5.5.6

like image 433
Tjerk Hamer Avatar asked Nov 30 '25 20:11

Tjerk Hamer


2 Answers

As Rocket Hazmat said, connect_errno isn't a function. The same to connect_error. So just take away the round brackets after both in your code.

if ($db->connect_errno)
{
    echo "Connection failed: ".$db->connect_error."\n";
    exit();
}
like image 85
MaggusK Avatar answered Dec 02 '25 11:12

MaggusK


It seems that you have confused the object oriented function with the procedural. You mention OOP so I assume you want the OOP version.

$db = new mysqli($dbhost,$dbuser,$dbpw,$dbdata);
if ($db->connect_errno) //no parenthesis
{
    echo "Connection failed: ".$db->connect_error."\n"; //no parenthesis
    exit();
}

For procedural

$db = new mysqli($dbhost,$dbuser,$dbpw,$dbdata);
if (!$db)
{
    echo "Connection failed: ".mysqli_connect_error(); //parenthesis
    exit();
}
like image 45
EternalHour Avatar answered Dec 02 '25 10:12

EternalHour