PHP 5.4.31 Released

mysqli_connect

(PHP 5)

mysqli_connectAlias of mysqli::__construct()

Description

This function is an alias of: mysqli::__construct()

add a note add a note

User Contributed Notes 8 notes

up
65
john at host2help dot com
1 year ago
The above by djfenix_31_94 at hotmail dot com is excellent - but

$query = "SELECT name FROM mytable" or die("Error in the consult.." . mysqli_error($link));
# execute the query.
$result = $link->query($query);

This is wrong - the or die will never be called in the above. It should read:

$query = "SELECT name FROM mytable";
# execute the query.
$result = $link->query($query) or die("Error in the consult.." . mysqli_error($link));

it took me a few plays to see that, hope it saves someone else a few minutes.
up
12
djfenix_31_94 at hotmail dot com
1 year ago
Simple connection to the database using this extension mysqli.
<?php
//conection:
$link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link));

//consultation:

$query = "SELECT name FROM mytable" or die("Error in the consult.." . mysqli_error($link));

//execute the query.

$result = $link->query($query);

//display information:

while($row = mysqli_fetch_array($result)) {
  echo
$row["name"] . "<br>";
}
?>

or

<?php
//conection:
$link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link));

//consultation:

$query = "SELECT name FROM mytable" or die("Error in the consult.." . mysqli_error($link));

//execute the query.

$result = mysqli_query($link, $query);

//display information:

while($row = mysqli_fetch_array($result)) {
  echo
$row["name"] . "<br>";
}
?>

Thanks by freddy alcarazo.
up
2
Anonymous
8 months ago
To @cerny dot venda at gmail dot com

You have an error in your grammar, It's "mysqli_fetch_array" and you have "mysqli_fecth_array".
up
-1
david dot bleuse at outlook dot fr
11 months ago
in the example it is written
mysqli_fe<b>ct</b>h_array
instead of
mysqli_fe<b>tc</b>h_array
up
-1
sapioit at gmail dot com
2 months ago
Because these functions are not so easy to use, at the beginning, there is a simple basic script, using 3 files, showing how mysqli basic functions are working:

<?php
# db.php
   
   
$sql_login_host = "localhost"; # MySQL Host
   
$sql_login_user = "test"; # MySql UserName
   
$sql_login_pass = "test"; # MySql Password
   
$sql_login_db = "jocdecarti"; # MySql Database
   
   
define("sql_login_host",$sql_login_host);
   
define("sql_login_user",$sql_login_user);
   
define("sql_login_pass",$sql_login_pass);
   
define("sql_login_db",$sql_login_db);
   
   
$devmode = true; # show errors
   
?>

<?php
# functions.php
include ("db.php");

# show errors
ini_set('short_open_tag', true);
if (
$devmode) {
   
ini_set('display_errors', true); /*change to false in production mode */
    /* error_reporting(E_ALL); */
   
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
} else {
   
error_reporting(E_ALL ^E_NOTICE ^E_WARNING);
}

   
# conectare la baza de date
   
$con=mysqli_connect($sql_login_host, $sql_login_user, $sql_login_pass, $sql_login_db);
    if(!
$con){
        die(
"Conectare imposibila: ".mysqli_error($con));
    }
    if (
mysqli_connect_errno()) {
        die(
"Connect failed: ".mysqli_connect_errno()." : ". mysqli_connect_error());
    }
   
# obtinere de rezultate din baza de date
   
function db($sqlq){ # $sqlq = sql querry;
       
global $con;
        if( !
$sql=mysqli_query($con,$sqlq) ){
            die(
"Error:".mysqli_errno($sql).":".mysqli_error($sql)."</br>Query: ".$sqlq);
        } else {
           
mysqli_free_result($sqlq);
            return
$sql;
        }
    }
   
   
# functii pentru prelucrarea datelor din functia db
   
function sql_get_lines($sql){
       
$sqlq=db($sql);
       
$nr=0;
       
$returned_array[][]=0;
       
$returned_array[0][0]=mysqli_num_rows($sqlq);
       
$returned_array[0][1]=mysqli_num_fields($sqlq);
        while(
$row=mysqli_fetch_row($sqlq)){
           
$nr++;
            foreach (
$row as $key => $value){
               
$returned_array[$nr][$key+1]=$value;
            }
            echo
"</br>";
        }
       
mysqli_free_result($sqlq);
        return
$returned_array;
    }
   
    function
sql_get_fields($sql){
       
$sqlq=db($sql);
       
$nr=0;
       
$returned_array[]=0;
       
$returned_array[0]=mysqli_num_rows($sqlq);
        while(
$row=mysqli_fetch_row($sqlq)){
           
$nr++;
            foreach (
$row as $value){
               
$returned_array[$nr]=$value;
            }
            echo
"</br>";
        }
       
mysqli_free_result($sqlq);
        return
$returned_array;
    }
    function
sql_get_value($sql){
       
$sqlq=db($sql);
       
$row=mysqli_fetch_row($sqlq);
       
mysqli_free_result($sqlq);
        return
$row[0];
    }
    function
sql_value($sql){
        return
sql_get_value($sql);
    }
   
   
# functii pentru afisarea datelor din functiile anterioare
   
function sql_print_lines($returned_array){
        for(
$i=1;$i<=$returned_array[0][0];$i++){
            for(
$j=1;$j<=$returned_array[0][1];$j++){
               
printf("%s : %s |= %s</br>", $i, $j, $returned_array[$i][$j]);
            }
        }
    }
    function
sql_print_fields($returned_array){
        for(
$i=1;$i<=$returned_array[0];$i++){
           
printf("%s |= %s</br>", $i, $returned_array[$i]);
        }
    }
   
   
# functii pentru afisarea directa din cod sql, folosind functiile anterioare
   
function echo_sql_print_lines($sql){
       
sql_print_lines(sql_get_lines($sql));
    }
    function
echo_sql_print_fields($sql){
       
sql_print_fields(sql_get_fields($sql));
    }
   
    function
echo_sql_value($sql){
        echo
sql_get_value($sql);
    }
    function
sql_print_value($sql){
        echo
sql_get_value($sql);
    }
   
   

?>

<?php
# test_functions.php
include ("functions.php");

   
# show entire table
   
$sql_querry="select * from multiplayer";
   
sql_print_lines(sql_get_lines($sql_querry));
   
echo_sql_print_lines($sql_querry);
   
   
# show table row
   
$sql_querry="select player2_hand from multiplayer";
   
sql_print_fields(sql_get_fields($sql_querry));
   
echo_sql_print_fields($sql_querry);
   
   
# show first value returned
   
$sql_querry="SELECT id FROM multiplayer WHERE id=1";
    echo
sql_value($sql_querry);
    echo
sql_get_value($sql_querry);
   
echo_sql_value($sql_querry);
   
sql_print_value($sql_querry);

mysqli_close($sql_link);

?>
up
-4
in0mad91 at gmail dot com
1 year ago
while($row = mysqli_fecth_array($result)) {
Need to be fixed to
while($row = mysqli_fetch_array($result)) {
up
-2
dev dot render at gmail dot com
2 months ago
<?php
//---abstracted ! ------
class connection{
    public
$host ="yourHostIp";
    public
$user = "UserName";
    public
$password = "password";
    public
$db="yourDatabase";
    public
$dbc;
   
    function
__construct() {
       
$con = mysqli_connect($this->host, $this->user, $this->password, $this->db);
       
        if(
mysqli_errno($con)){
            echo
"sum error";
           
        }
        else{
          
$this->dbc = $con; // assign $con to $dbc
          
echo"connected ";
        }
    }
}

$test = new connection();
?>
up
-21
cerny dot venda at gmail dot com
11 months ago
Hi,

I tried this new way connection and it ended as mysql error.
So I changed "mysqli_fecth_array($result)" to "...fetch..." and now its ok. Is it your mistake, typing error?

Thank you
To Top