php连接数据库
当php页面的字符编码和mysql的编码不一致时,容易出现中文乱码,这里就是解决乱码的代码.
<?php
header(“Content-type:text/html;charset=GBK”);
//Connect To Database
$hostname=’192.168.1.1′;
$username=’root’;
$password=’123456789′;
$dbname=’info’;
$usertable=’bx_askclass’;
$yourfield = ‘askclassname’;
$link=mysql_connect($hostname,$username, $password) OR DIE (‘Unable to connect to database! Please try again later.’);
mysql_select_db($dbname);
//mysql_query(“SET NAMES ‘gbk'”);
//mysql_query(‘set character_set_client = gbk’);
//mysql_query(‘set character_set_connection = utf8’);
mysql_query(‘set character_set_results = gbk’);
$query = “SELECT $yourfield FROM $usertable”;
$result = mysql_query($query);
if($result) {
while($row = mysql_fetch_array($result)){
$name = $row[“$yourfield”];
echo ‘名称: ‘.$name;
}
}
欢迎转载,请注明出处:亲亲宝宝