十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
1.最简单的方式
创新互联建站拥有网站维护技术和项目管理团队,建立的售前、实施和售后服务体系,为客户提供定制化的成都网站制作、成都网站建设、网站维护、南充服务器托管解决方案。为客户网站安全和日常运维提供整体管家式外包优质服务。我们的网站维护服务覆盖集团企业、上市公司、外企网站、商城网站开发、政府网站等各类型客户群体,为全球千余家企业提供全方位网站维护、服务器维护解决方案。
?php$con = mysql_connect("localhost","root","");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_close($con);?
2.面向对象mysqli(详细教程)
?php$mysqli = new mysqli('localhost','root','','volunteer');if (mysqli_connect_errno()){ die('Unable to connect!'). mysqli_connect_error();}?
3.pdo连接mysql(详细教程)
?php $db = new PDO('mysql:host=localhost;dbname=test', 'root', '');try { foreach ($db-query('select * from user') as $row){ print_r($row); } $db = null; //关闭数据库} catch (PDOException $e) { echo $e-getMessage();}?
4.ADODB连接mysql(详细教程)
?phprequire_once './adodb5/adodb.inc.php';$conn = ADONewConnection('mysql');$conn-connect('localhost','root','','test');$conn-Execute("set names utf8");$res = $conn-Execute("select * from user");if (!$res){ echo $conn-ErrorMsg();}else{ var_dump($res);}?
我也刚刚学PHP,正在研究中,虽然你只给10分........
首先,将代码保存到一个文件,如:mysql.class.php
其次,在一个常用的文件里调用:比如头部文件header.php,因为我放在根目录所以用下面方式导入其他文件:
require dirname(__FILE__) . 'include/config.php';
//导入类文件
require dirname(__FILE__) . 'include/mysql.class.php';
//定义一个类及初始化数据库类
$db = new mysql($db_host, $db_user, $db_pass, $db_name);
$db_host = $db_user = $db_pass = $db_name = NULL;
然后,在test.php文件调用:
require_once dirname(__FILE__) . '/header.php';
使用方法:
$sql = "读取表";
$res = $db-query($sql);
$info = array();//定义数组
while($row=$db-fetchRow($res))
{
$arr['id'] = $row['id'];
$arr['title'] = $row['title'];
$info[] = $arr;
}
可以在显示的地方用:
foreach($info as $i)
{
echo $i['title']."br /";
}
或是直接使用while
还用另一种调用方式:
$here_area = $db-getRow("select areaid,areaname from {$table}area where areaid='$areaid'");
$here[] = array('name'=$here_area['areaname'],'id'=$here_area['areaid']);
测试通过,因为我正在使用.....................................
config.php代码:
?php
$db_host = "localhost";
$db_name = "test";
$db_user = "root";
$db_pass = "";
$table = "mini_";
$charset = "gb2312";
$dbcharset = "gbk";
?
mysql.class.php代码:
?php
class mysql
{
var $link = NULL;
//自动执行__construct php5类构建方法,如果PHP4和PHP5同时使用会自动使用PHP5的方法
function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)
{
//自动执行时调用mysql函数
$this-mysql($dbhost, $dbuser, $dbpw, $dbname, $pconnect, $quiet);
}
//php4类构建方法,如果没有 __construct 就自动执行此功能
function mysql($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)
{
if ($quiet)
{
$this-connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, $quiet);
}
else
{
$this-settings = array(
'dbhost' = $dbhost,
'dbuser' = $dbuser,
'dbpw' = $dbpw,
'dbname' = $dbname,
'charset' = $charset,
'pconnect' = $pconnect
);
}
}
function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)
{
global $dbcharset;
if ($pconnect)
{
if (!($this-link = @mysql_pconnect($dbhost, $dbuser, $dbpw)))
{
if (!$quiet)
{
$this-ErrorMsg("Can't pConnect MySQL Server($dbhost)!");
}
return false;
}
}
else
{
if (PHP_VERSION = '4.2')
{
$this-link = @mysql_connect($dbhost, $dbuser, $dbpw, true);
}
else
{
$this-link = @mysql_connect($dbhost, $dbuser, $dbpw);
mt_srand((double)microtime() * 1000000);
}
if (!$this-link)
{
if (!$quiet)
{
$this-ErrorMsg("Can't Connect MySQL Server($dbhost)!");
}
return false;
}
}
$this-dbhash = md5($this-root_path . $dbhost . $dbuser . $dbpw . $dbname);
$this-version = mysql_get_server_info($this-link);
if ($this-version '4.1')
{
if ($dbcharset != 'latin1')
{
mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $this-link);
}
if ($this-version '5.0.1')
{
mysql_query("SET sql_mode=''", $this-link);
}
}
if ($dbname)
{
if (mysql_select_db($dbname, $this-link) === false )
{
if (!$quiet)
{
$this-ErrorMsg("Can't select MySQL database($dbname)!");
}
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
function query($sql, $type = '')
{
if ($this-link === NULL)
{
$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链接mysql必备条件:
已安装mysql数据库;
检查php环境是否已开启mysql扩展(一般情况下是开启的);
检查方法:a.使用phpinfo();函数,看有没有mysql项;b.打开php.ini文件,检查php_mysql.dll前分号是否已取掉。
php链接代码如下:
?php
//设置编码格式
header("Content-type:text/html;charset=utf-8");
//定义数据库主机地址
$host="localhost";
//定义mysql数据库登录用户名
$user="root";
//定义mysql数据库登录密码
$pwd="";
//链接数据库
$conn = mysql_connect($host,$user,$pwd);
//对连接进行判断
if(!$conn){
die("数据库连接失败!".mysql_errno());
}else{
echo "数据库连接成功!";
}
?
PHP与数据库连接最常用的方法有四种,它们分别是MySQL、mysqli、pdo、adodb,其中以第一种方法最常用,方法如下:
//与数据库服务器连接
$conn = @mysql_connect('数据库主机名','用户名','密码');
//选择具体数据库
mysql_select('数据库名',$conn);
//如果只打开了一个数据库连接,则$conn可以省略。