0

I tried to look for questions with the same issue but I couldn't get their solutions to fit my code.

I keep getting the error:

Object of class mysqli could not be converted to string in line 5

Code:

<?php

    function aggiornamento($utente) {
        global $conn;   
        global $_CONFIG;

        $selezione = mysqli_query ($conn, "SELECT * FROM ".$_CONFIG['db_account'].".account WHERE login = '".$utente."' LIMIT 1");

        while ($account = mysqli_fetch_array($selezione)) {
            $_SESSION['IShop_Login']= $account['login'];
            $_SESSION['IShop_DR'] = $account['dr'];
            $_SESSION['IShop_DB'] = $account['db'];
            $_SESSION['IShop_AID'] =   $account['id'];
            $_SESSION['IShop_Admin'] = $account['Admin_IShop'];
        }
    }

?>

I tried a few solutions from already asked questions but to no avail. So I'm kindly asking you to correct my code for me and maybe a little explanation of what's going on so I'd learn.

My $conn:

$conn = mysql_connect($_CONFIG['host'], $_CONFIG['user'], $_CONFIG['pass']);

Whereas $_CONFIG['host'], $_CONFIG['user'], $_CONFIG['pass'] defined as:

$_CONFIG['host'] = "SERVER IP";
$_CONFIG['user'] = "root";
$_CONFIG['pass'] = "PW";

And most importantly, $_CONFIG['db_account'] = "account";. But I strongly believe the problem isn't with my $conn, I could be wrong tho.

Rizier123
  • 58,877
  • 16
  • 101
  • 156
P4P4DR4G0N
  • 35
  • 5
  • Show your `$conn` variable, please. – Andrius Nov 18 '15 at 11:06
  • check with print_r($selezione);exit();and say the result – Vigneswaran S Nov 18 '15 at 11:09
  • Hi, the output is mysqli_result Object ( [current_field] => 0 [field_count] => 43 [lengths] => [num_rows] => 1 [type] => 0 ) – P4P4DR4G0N Nov 18 '15 at 11:14
  • ah - you are mixing mysql with mysqli ~ the connection uses `mysql_connect` but you execute the statements using `mysqli` – Professor Abronsius Nov 18 '15 at 11:16
  • 1) Don't use the keyword global, just pass the variables as parameters 2) You can't mix MYSQL API's. Take a look into the [documentation](http://php.net/manual/en/mysqli.construct.php). Also I highly recommend to use [prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) or change to [PDO](http://php.net/manual/en/book.pdo.php) 3) Your error message doesn't match the code. – Rizier123 Nov 18 '15 at 12:30

5 Answers5

1

There are some problems you should take care about

  1. Try to avoid using global variables inside functions. It may be dangerous in any application because you might change them without wanting it.
  2. Take care of SQL injection. You may consider starting to use prepared statements in order to avoid it.
  3. You are limiting the results to 1 but still use a loop to get the results
  4. You are using mysqli functions but the connection is done using mysql_connect
  5. You didn't select any database to work with, when created the connection

The connection should be done using mysqli_connect:

$mysql = mysqli_connect($host, $user, $password, $databaseName);
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
  • Database is already selected with $conn ! Also i get this now mysqli_connect() expects parameter 1 to be string, object given :/ can you PLEASE make a full of modification of my already existing code? I'm really tired nothing wants to work... – P4P4DR4G0N Nov 18 '15 at 11:30
0

I don't know if this will help?

General `mysqli` connection:
----------------------------

   $host    =   'localhost';
   $uname   =   'root'; 
   $pwd     =   'xxx'; 
   $db      =   'xxx';

   $conn=new mysqli( $host, $uname, $pwd, $db );


function aggiornamento( $utente=false ){
    global $conn;
    global $_CONFIG;

    if( !isset( $utente ) or empty( $utente ) ) return false;

    $sql="select `login`,`dr`,`db`,`id`,`admin_ishop` from `{$_CONFIG['db_account']}`.`account` where `login`=? limit 1;";

    $stmt=$conn->prepare( $sql );
    $stmt->bind_param( 's', $param );
    $param=$utente;

    $result=$stmt->execute();
    $stmt->bind_result( $login, $dr, $db, $id, $admin_ishop );
    $stmt->fetch();

    if( $result ){

        $_SESSION['IShop_Login']    = $login;
        $_SESSION['IShop_DR']       = $dr;
        $_SESSION['IShop_DB']       = $db;
        $_SESSION['IShop_AID']      = $id;
        $_SESSION['IShop_Admin']    = $admin_ishop;

        $stmt->close();
        return true;
    }
    $conn->close();
    return false;
}

Test

<?php

    include __DIR__.'\conn.php';
    /*
        Where conn.php is simply:
        -------------------------

        <?php
        $host   =   'localhost';
        $uname  =   'root'; 
        $pwd    =   'xxx'; 
        $db     =   'so_experiments';

        $conn   =   new mysqli( $host, $uname, $pwd, $db );
        ?>
    */

    /* Defined so as not to break sql stmt */
    $_CONFIG['db_account']='so_experiments';


    function aggiornamento( $utente=false ){
        global $conn;
        global $_CONFIG;

        if( !isset( $utente ) or empty( $utente ) ) return false;

        $sql="select `login`,`dr`,`db`,`id`,`admin_ishop` from `{$_CONFIG['db_account']}`.`account` where `login`=? limit 1;";

        $stmt=$conn->prepare( $sql );
        $stmt->bind_param( 's', $param );
        $param=$utente;

        $result=$stmt->execute();
        $stmt->bind_result( $login, $dr, $db, $id, $admin_ishop );
        $stmt->fetch();

        if( $result ){

            $_SESSION['IShop_Login']    = $login;
            $_SESSION['IShop_DR']       = $dr;
            $_SESSION['IShop_DB']       = $db;
            $_SESSION['IShop_AID']      = $id;
            $_SESSION['IShop_Admin']    = $admin_ishop;

            /* only for demo debug */
            echo 'login:', $login,', db:', $db,', dr:', $dr,', id:', $id,', admin_ishop:', $admin_ishop;

            $stmt->close();
            return true;
        }
        $conn->close();
        return false;
    }

    /* call the function with a value for `$utente` = 'default' */
    call_user_func( 'aggiornamento', 'default' );

    /* 
        outputs: 
        login:default, db:store, dr:43, id:1, admin_ishop:1
    */
?>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in D:\xampp\htdocs\dashboard\ishop\inc\funzioni.inc.php on line 11 – P4P4DR4G0N Nov 18 '15 at 11:22
  • my bad - there was an extra "s" in there... finger troubles. – Professor Abronsius Nov 18 '15 at 11:39
  • still Object of class mysqli could not be converted to string in :( in line 14 which is $result=$stmt->execute(); – P4P4DR4G0N Nov 18 '15 at 11:42
  • just tried the code you updated..unexpected '`' in line 2 – P4P4DR4G0N Nov 18 '15 at 11:55
  • I used `include __DIR__.'\conn.php';` because the file `conn.php` just happens to be in the same directory as the test page I created ~ this might not be the case for you so you should include that in the manner you would normally. I don't understand the `unexpected "`" in line 2` error that you get though. – Professor Abronsius Nov 18 '15 at 12:14
0

You are using mysqli_query but mysql_connect, so use mysqli_connect() and it works

Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
0

You are mixing mysql and mysqli-functions, and this is known to fail.

Try replace your mysql_connect with mysqli_connect, but be aware it might require other/additional parameters.

Also, you should reconsider your usage of global variables.

olearos
  • 75
  • 1
  • 7
  • I tried, still... :( mysqli_connect() expects parameter 1 to be string, object given – P4P4DR4G0N Nov 18 '15 at 11:24
  • You still get the same error? Did you also made sure of that the mysqli_connect object is correct? (var_dump($conn);) Please also make sure the mysqli_connection-parameters are correct (as they differ from mysql_connect). – olearos Nov 18 '15 at 11:25
  • yes same error.. and idk how to make sure mysqli_connect object is correct.. my knowledge is near the zero guys im just a beginner... – P4P4DR4G0N Nov 18 '15 at 11:31
0

Try this

<?php
function aggiornamento($utente){
global $conn;   
global $_CONFIG;
       $selezione = mysqli_query ($conn, "SELECT * FROM ".$_CONFIG['db_account'].".account WHERE login = '".$utente."' LIMIT 1");
       while ($row = $selezione->fetch_assoc())
       {
$_SESSION['IShop_Login']= $row['login'];
$_SESSION['IShop_DR'] = $row['dr'];
$_SESSION['IShop_DB'] = $row['db'];
$_SESSION['IShop_AID'] =   $row['id'];
$_SESSION['IShop_Admin'] = $row['Admin_IShop'];
       }
}
?>
Vigneswaran S
  • 2,039
  • 1
  • 20
  • 32
  • Still the same error in line 5 :'((( – P4P4DR4G0N Nov 18 '15 at 12:08
  • @P4P4DR4G0N error in line 5. so error in Mysqli_query or fetch_array? – Vigneswaran S Nov 18 '15 at 12:36
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) out of the code really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Kyll Nov 18 '15 at 12:37
  • @Kyll should i explain my code? – Vigneswaran S Nov 18 '15 at 12:42
  • Yes, adding explanations improve the quality of answers. – Kyll Nov 18 '15 at 12:43
  • @Kyll I am accepting. But the code contains very basic things. whats there to explain – Vigneswaran S Nov 18 '15 at 12:47