•  0
    MySQLi

    How to connect with database using mysqli and php?

      Asmin Jismun    539        0        Report content

    This is a function to connect with database using mysqli.

    
    public function dbConnect()
    {
    $link = mysqli_connect('localhost', 'root', 'your_password','database_name');
    if (mysqli_connect_errno($link))
    {
    //echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    else
    {
    //echo 'Database Connected successfully ';
    }
    
    mysqli_select_db($link,"database_name");
    
    mb_language('uni');
    mb_internal_encoding('UTF-8');
    mysqli_query( $link,"SET character_set_results=utf8");
    mysqli_query( $link,"SET character_set_server=utf8");
    mysqli_query( $link,"SET character_set_database=utf8");
    mysqli_query( $link,"set names 'utf8'");
    
    return $link;
    }
    

    This function used to get data from database using mysqli

    
    public function getData($username,$password)
    {
    $con = $this->dbConnect();
    $sql = "
    SELECT *
    FROM tbl_login WHERE login_name = '".$username."' AND login_pass = '".$password."'
    ";
    
    $query = mysqli_query($con,$sql);
    if (!$query)
    {
    die(mysqli_error($con));
    }
    
    while($result = mysqli_fetch_assoc($query))
    {
    $dataList[] = $result;
    }
    mysqli_close($con);
    return $dataList;
    }
    

     

     


Leave a Comment

Please Login to insert comment.

 

Facebook Conversations