概述
php升级新版之后,对原来的mysql的访问方式有些变动,一般报错如下:Fatal error: Uncaught Error: Call to undefined function mysql_connect() in
原来的写法conn.php:<?php
class ConnectionMySQL{
private $host="127.0.0.1"; //主机
private $name="myname"; //数据库的username
private $pass="mypass"; //数据库的password
private $table="mydb"; //数据库名称
private $ut="utf-8"; //编码形式
//构造函数
function __construct(){
// $this->ut=$ut;
$this->connect();
}
//数据库的链接
function connect(){
//$link = new PDO($this->dsn,$this->name,$this->pass);
//$link->exec('set names utf8');
$link=mysql_connect($this->host,$this->name,$this->pass) or die ($this->error());
mysql_select_db($this->table,$link) or die("no database:".$this->table);
mysql_query("SET NAMES '$this->ut'");
}
?>
原本test.php:<?php
header('Content-Type: text/html; charset=utf-8');
include('conn.php');
$db = new ConnectionMySQL();
mysql_query("SET NAMES utf8");
$result = mysql_query("SELECT * FROM mytable where id='1' limit 0,1");
while($row = mysql_fetch_assoc($result))
{
$tablename1 = $row["tablename1"];
}
?>
这样子可以读取到值......
但是php升级之后,ConnectionMySQL->connect()链接方式得变更,原来的conn.php可改写成如下:<?php
$conn = mysqli_connect('127.0.0.1', 'changtu', 'changtu123!@#', 'changtu');
if(!$conn){
die("sql_connect error" . mysqli_connect_error());
}else{
echo"mysql connect ok";
}
?>
test.php改写如下:<?php
header('Content-Type: text/html; charset=utf-8');
include('conn.php');
//$db = new ConnectionMySQL();
//mysql_query("SET NAMES utf8");
$result = mysqli_query($conn,"SELECT * FROM mytable where id='1' limit 0,1");
while($row = mysqli_fetch_assoc($result))
{
$tablename1 = $row["tablename1"];
}
?>
有一些变化的部分:注意//注释掉的部分,mysql_query变成了mysqli_query,以及$conn,的调用.
其他有变化的可参考:
mysql_fetch_assoc 变为:mysqli_fetch_assoc
mysql_fetch_array 变为:mysqli_fetch_array
mysql_num_rows 变为:mysqli_num_rows
等等。但基本语法不变。
譬如更新:mysqli_query($conn,"UPDATE tablename SET biao1 = 'biao1',biao2= 'biao2'");
插入:mysqli_query($conn,"INSERT INTO tablename (biao1,biao2) VALUES('biao1','biao2')");
最后
以上就是坚定砖头为你收集整理的php mysql_connect 报错_php升级5.x版本之后mysql_connect报错的全部内容,希望文章能够帮你解决php mysql_connect 报错_php升级5.x版本之后mysql_connect报错所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复