When you need more(less) timeout, you can use this. This only guarantees that host and port are opened. It is suitable on the very high speed networks(LAN) and where one second is not acceptable.
$timeoutInSeconds can be set to any numeric value
e.g. "1/2", "0.500","0.1","0.01" etc.
--------------------------------------------------------------
function getmicrotime()
{
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
$time_start = getmicrotime();
$fp = fsockopen("$mysqlHost",$mysqlPort, $errno, $errstr,$timeoutInSeconds);
if (!$fp)
{
//timeout mostly
echo "ERR: $errno - $errstr<br>\n";
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo "Timeout $time sekund";
}
else
{
fclose($fp);
}
mysqli_connect,
(PHP 5)
mysqli_connect, mysqli->__construct() — 新規に MySQL サーバへの接続をオープンする
説明
手続き型
mysqli mysqli_connect ( [string $host [, string $username [, string $passwd [, string $dbname [, int $port [, string $socket]]]]]] )オブジェクト指向型(コンストラクタ):
class mysqli {__construct ( [string $host [, string $username [, string $passwd [, string $dbname [, int $port [, string $socket]]]]]] )
}
実行中の MySQL サーバへの接続をオープンします。
パラメータ
- host
ホスト名または IP アドレスです。この引数に NULL または "localhost" を渡すと ローカルホストとみなされます。もし可能な場合、TCP/IP プロトコルの代わりに パイプが使用されます。
- username
MySQL のユーザ名。
- passwd
パスワードを指定しない場合(NULL 値が渡されます)、MySQL サーバは パスワードを持たないユーザレコードについてのみ認証を試みます。 これによってひとつのユーザ名において(パスワードが指定されたか 否かによって)違うパーミッションを与えることができます。
- dbname
指定した場合は、 クエリが行われるデフォルトのデータベースとなります。
- port
MySQL サーバに接続する際のポート番号を指定します。
- socket
使用するソケットあるいは名前つきパイプを指定します。
注意: socket 引数を指定しても、MySQL サーバへの 接続時の型を明示的に定義することにはなりません。MySQL サーバへの 接続方法については host 引数で定義されます。
返り値
MySQL サーバへの接続を表すオブジェクトを返します。接続に失敗した場合には FALSE を返します。
例
例 1433. オブジェクト指向型
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* 接続の状況をチェックします */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
printf("Host information: %s\n", $mysqli->host_info);
/* 接続を閉じます */
$mysqli->close();
?>
例 1434. 手続き型
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* 接続の状況をチェックします */
if (!$link) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
printf("Host information: %s\n", mysqli_get_host_info($link));
/* 接続を閉じます */
mysqli_close($link);
?>
上の例の出力は以下となります。
Host information: Localhost via UNIX socket
mysqli_connect
31-Jul-2007 01:53
06-Jul-2007 10:29
This helped me:
<?php
$db = mysqli_connect($host, $login, $pass, $dbName);
$db->set_charset('utf8');
...
?>
18-Feb-2007 04:52
Yes, it's totally odd that php.ini doesn't seem to allow you to global specify the client connection as utf-8. You might try this in the
my.cnf file
[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8
It didn't work for me, but others may have better luck. In the end I had to specify in the php code as noted below.
01-Feb-2007 04:13
A quick word about the connection encoding for this extension. It doesn't appear to be documented anywhere that I can find, but this defaults to latin1 encoding for the connection, regardless of PHP's or MySQL's settings. I run in a completely UTF-8 setup, and spent days trying to discover the cause of corrupted characters from the Db. If, like me, you need to work with other than latin1, use the set_charset function/method to switch to your encoding of choice.
24-Jan-2007 11:08
PHP520, MYSQL5027, WinServer2003r2
i created a database with no tables
i created a user with no privileges (testuser,testpw,localhost)
<?php
$MYhostname="localhost"; $MYusername="testuser"; $MYpassword="testpw"; $MYdatabase="mysqlitest";
$MYlink=mysqli_connect($MYhostname, $MYusername, $MYpassword, $MYdatabase) or trigger_error(mysqli_connect_error(),E_USER_ERROR);
printf("Host information: %s\n", mysqli_get_host_info($MYlink));
mysqli_close($MYlink);
?>
This gave an Access Denied error.
But it works with mysql_connect() !!!!!!!!!!!!!
Solution: give the user a GLOBAL SELECT privilege
or create a table and give a SELECT priviledge to the table.
Hope this helps.
06-Jan-2007 04:04
If you want to suppress any error messages displayed when using mysqli as an object, do the following:
<?php
$db = @ new mysqli('localhost', 'user', 'pass', 'dbname');
// test for errors
if (mysqli_connect_errno()) {
echo "Database connection error.\n\n";
exit();
}
?>
02-Jan-2007 09:01
Many here have said you can't produce a persistent connection, however this is false if you are using objects take below as an example with strict visibility; sorry about no comments just quite busy but hope this helps someone who wants to broaden their perspective;
class A
{
private $link;
public $objects;
//load all child objects with the connection
public function __construct($classes, $db, $host, $user, $pass)
{
$this->link = $this->connect($db, $host, $user, $pass);
$name = __CLASS__;
foreach ($classes as $id => $obj)
{
if(is_subclass_of($obj,$name))
{
$this->objects[$obj] = new $obj($this->link);
}
}
private function connect($db, $host, $user, $pass)
{
$link = new mysqli($host,$user,$pass,$db);
if($link->error) return "could not connect to database server, ". $link->error;
else
return $link;
}
public __destruct($void)
{
if($void) $this->link->close();
}
}
class B extends A
{
private $link;
public function __construct($link)
{
$this->link = $link
}
function getUsers()
{
$q = $this->link->prepare("select id,name,location from users");
$q->execute();
$q->bind_result($id,$name,$location);
$q->fetch();
return array($id,$name,$location);
}
}
example of use:
$classes = array('B');
$db = 'mydatabase';
$host = '127.0.0.1';
$user = 'username';
$pass = 'password';
$a = new A($classes, $db, $host, $user, $pass);
extract($a->objects, EXTR_PREFIX_SAME, 'obj');
$b->getUsers();
this example allows you to initiate one instance of every object as long as it is a child with the same connection. this is just a snippet of a much bigger model.
16-Aug-2006 10:09
Just so you know, mysqli_connect(); will also throw a warning on an unsuccessfull connect. To avoid such warnings being shown prefix it with an "@" symbol.
<?php
// Example:
@mysqli_connect();
?>