php与数据库的连接类型 php与数据库链接( 二 )


}
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
function query($sql, $type = '')
{
if ($this-link === NULL)
{
【php与数据库的连接类型 php与数据库链接】$this-connect($this-settings['dbhost'], $this-settings['dbuser'], $this-settings['dbpw'], $this-settings['dbname'], $this-settings['charset'], $this-settings['pconnect']);
$this-settings = array();
}
if ($this-queryCount++ = 99)
{
$this-queryLog[] = $sql;
}
if ($this-queryTime == '')
{
if (PHP_VERSION = '5.0.0')
{
$this-queryTime = microtime(true);
}
else
{
$this-queryTime = microtime();
}
}
if (!($query = mysql_query($sql, $this-link))$type != 'SILENT')
{
$this-error_message[]['message'] = 'MySQL Query Error';
$this-error_message[]['sql'] = $sql;
$this-error_message[]['error'] = mysql_error($this-link);
$this-error_message[]['errno'] = mysql_errno($this-link);
$this-ErrorMsg();
return false;
}
return $query;
}
function affected_rows()
{
return mysql_affected_rows($this-link);
}
function num_fields($query)
{
return mysql_num_fields($query);
}
function error()
{
return mysql_error($this-link);
}
function errno()
{
return mysql_errno($this-link);
}
function num_rows($query)
{
return mysql_num_rows($query);
}
function insert_id()
{
return mysql_insert_id($this-link);
}
function fetchRow($query)
{
return mysql_fetch_assoc($query);
}
function fetcharray($query)
{
return mysql_fetch_array($query);
}
function version()
{
return $this-version;
}
function close()
{
return mysql_close($this-link);
}
function ErrorMsg($message = '', $sql = '')
{
if ($message)
{
echo "$message\n\n";
}
else
{
echo "bMySQL server error report:";
print_r($this-error_message);
}
exit;
}
function getCol($sql)
{
$res = $this-query($sql);
if ($res !== false)
{
$arr = array();
while ($row = mysql_fetch_row($res))
{
$arr[] = $row[0];
}
return $arr;
}
else
{
return false;
}
}
function getOne($sql, $limited = false)
{
if ($limited == true)
{
$sql = trim($sql . ' LIMIT 1');
}
$res = $this-query($sql);
if ($res !== false)
{
$row = mysql_fetch_row($res);
if ($row !== false)
{
return $row[0];
}
else
{
return '';
}
}
else
{
return false;
}
}
function getAll($sql)
{
$res = $this-query($sql);
if ($res !== false)
{
$arr = array();
while ($row = mysql_fetch_assoc($res))
{
$arr[] = $row;
}
return $arr;
}
else
{
return false;
}
}
//使用: getRow($sql,true) 如果有true那值是 limit 1,读取一条信息
function getRow($sql, $limited = false)
{
if ($limited == true)
{
$sql = trim($sql . ' LIMIT 1');
}
$res = $this-query($sql);
if ($res !== false)
{
return mysql_fetch_assoc($res);
}
else
{
return false;
}
}
}
?
PHP与数据库连接的方式有哪些?1.最简单php与数据库的连接类型的方式
?php$con = mysql_connect("localhost","root","");if (!$con){die('Could not connect: ' . mysql_error());}mysql_close($con);?
2.面向对象mysqli(详细教程)

推荐阅读