Home mysqli connection Object oriented style dont work
Reply: 0

mysqli connection Object oriented style dont work

user37636
1#
user37636 Published in September 19, 2018, 9:39 am

This question already has an answer here:

  • Fatal error: Call to a member function prepare() on null 2 answers
  • mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource 31 answers
  • Fatal error: Call to a member function prepare() on boolean in 3 answers
  • Fatal error: Call to a member function prepare()? 1 answer
  • Fatal error Call to a member function prepare() on null 1 answer

i'm trying to connect to my db with Object oriented style in PHP, but the browser shows me the following error:

Fatal error: Call to a member function prepare() on null in /home/brasi862/public_html/lixeira/controller/Controller.php on line 13

it looks like the __construct function is not running on the other function. I already encountered several similar topics and followed the answers, but nothing worked.

if I put the contents of __construct inside the login function, the code works.

my code is:

class Controller{
    private $mysqli;
    function __construct(){
        $mysqli = new mysqli('localhost','root','','mytable'); 

        if($mysqli->connect_error){
            print_r('erro: '.$mysqli->connect_error);
        }
    }

    function login($user,$senha){
        if($stmt = $mysqli->prepare("SELECT username, senha FROM lixeira WHERE username = ? AND senha = PASSWORD(?)")){
            $stmt->bind_param('ss',$user,$senha);
            $stmt->execute();

            $result = $stmt->get_result();
            while($u = $result->fetch_array(MYSQLI_ASSOC)){
                $saida = array(
                    'username' => $u['username'],
                    'senha' => $u['senha']
                );
            }

            $stmt->store_result();
            if($stmt->num_rows > 0){
                $saida['success'] = true;
            }
            else{
                $saida['success'] = false;
            }
            $stmt->close();
        }
        $mysqli->close();
        return $saida;
    }
}

What is wrong with the code so it can not use the connection in the function?

share|improve this question

marked as duplicate by Jay Blanchard php StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var $hover = $(this).addClass('hover-bound'), $msg = $hover.siblings('.dupe-hammer-message'); $hover.hover( function() { $hover.showInfoMessage('', { messageElement: $msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Feb 12 at 18:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • mysqli does not exist in the context of any function except __construct. You need to be using $this->mysqli any time you want to set it or use it. – aynber Feb 12 at 18:05
  • thank you aynber! Your answer solved my problem. – Erick Crus Feb 12 at 18:26

active oldest votes

Browse other questions tagged php database class object mysqli or ask your own question.

StackExchange.ready(function(){$.get('/posts/48752730/ivc/0571');});
StackExchange.ready(function () { StackExchange.responsiveness.addSwitcher(); }) (function(i, s, o, g, r, a, m) { i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function() { (i[r].q = i[r].q || []).push(arguments) }, i[r].l = 1 * new Date(); a = s.createElement(o), m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga'); StackExchange.ready(function () { StackExchange.ga.init({ sendTitles: true, tracker: window.ga, trackingCodes: [ 'UA-108242619-1' ] }); StackExchange.ga.setDimension('dimension2', '|php|database|class|object|mysqli|'); StackExchange.ga.setDimension('dimension3', 'Questions/Show'); StackExchange.ga.trackPageView(); }); /**/ var _qevents = _qevents || [], _comscore = _comscore || []; (function() { var ssl = 'https:' == document.location.protocol, s = document.getElementsByTagName('script')[0], qc = document.createElement('script'); qc.async = true; qc.src = (ssl ? 'https://secure' : 'http://edge') + '.quantserve.com/quant.js'; s.parentNode.insertBefore(qc, s); _qevents.push({ qacct: "p-c1rF4kxgLUzNc" }); /**/ var sc = document.createElement('script'); sc.async = true; sc.src = (ssl ? 'https://sb' : 'http://b') + '.scorecardresearch.com/beacon.js'; s.parentNode.insertBefore(sc, s); _comscore.push({ c1: "2", c2: "17440561" }); })();
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.661051 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO