Results 1 to 5 of 5

Thread: PHP and Mysqli_connect issue

  1. #1
    Join Date
    Aug 2005
    Posts
    510
    Rep Power
    0

    Default PHP and Mysqli_connect issue

    I created this class but now i have an issue passing the mysqi_connect into a global variable for other usage

    class database{

    protected $_link;
    protected $_result, $_numRows;

    public function __construct()
    {
    $this->_link = $this->connectDB();
    }

    private function connectDB()
    {
    $this->_link = mysqli_connect($this->server,$this->username,$this->password,$this->dbname);
    mysqli_select_db($this->_link, $this->dbname);
    var_dump($this->_link);

    echo "<br>Connection was create;";

    if (mysqli_connect_errno()) {
    echo '<p>Cannot connect to DB: ' . mysqli_connect_error() . '</p>';
    end;
    }
    }



    //verify a login session

    private function checkLogin($usr, $pass)
    {
    [extract]
    $str = "Select * from user where username='$usr' and password='$pass' ";
    $this->_result = mysqli_query(mysqli_connect($this->_link, $str); //$this_link is returning NULL....... Y?????????
    $this->_numRows = mysqli_num_rows($this->_result);

    if ($this->_numRows > 0)
    {
    echo $this->_numRows;
    return true;
    }
    else
    {
    return false;
    }
    }
    }

    i get a the following warning:-
    "Warning: mysqli_query() expects parameter 1 to be mysqli, null given in"

    i call the instance :-


    $mysql = new database;
    $mysql->checkLogin($_POST['usr'], $_POST['pass'])
    Why fight Information Technology when you can outsource IT

  2. #2
    Join Date
    Feb 2003
    Posts
    3,036
    Rep Power
    0

    Default

    put a echo in the constructor to see if it is being called

  3. #3
    Join Date
    Aug 2005
    Posts
    510
    Rep Power
    0

    Default

    i did... and it is..this is why i dont understand what is happening... i am unable to pass the connection to variable $_link... if u look there is on in the actural function... that say "connection created"
    Why fight Information Technology when you can outsource IT

  4. #4
    Join Date
    Aug 2005
    Posts
    510
    Rep Power
    0

    Default

    i got it work...

    in the constructor i replaced with this:- $_link = $this->connectDB();
    Why fight Information Technology when you can outsource IT

  5. #5
    Join Date
    May 2008
    Posts
    4
    Rep Power
    0

    Default

    Hello,

    Anyone needing help Working With JSON, JQuery, PHP and Mysql can checkout this tutorial

    //ESTABLISH DATABASE CONNECTION
    define("DB_SERVER", "localhost");
    define("DB_USER", "root");
    define("DB_PASS", "");
    define("DB_NAME", "myDB");

    //Connect to mysql server
    $link=@mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB _NAME);
    if(!$link) {
    print("Failed to establish connection to mysql server!");
    exit();
    }

    if(isset($_POST['keyword'])){
    $keyword = trim($_POST['keyword']);
    $keyword = mysqli_real_escape_string($link, $keyword);
    $query = "select meter_name,meter_location from meters where meter_name like '$keyword%'"; //MUST BEGIN WITH $KEYWORD

    //echo $query;
    $result = mysqli_query($link,$query);
    if($result){
    if(mysqli_affected_rows($link)!=0){
    while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
    echo '<span><a class="meterRow">'.$row['meter_name'].'</a><span>'.$row['meter_location'].'</span></span>';
    }
    }else {
    echo 'No Results for :"'.$_POST['keyword'].'"';
    }
    }
    }else {
    echo 'Parameter Missing';
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •