将mysql更改为mysqli并且无法连接到数据库

时间:2017-01-27 13:49:53

标签: mysqli

我在这个领域并不是很有经验,所以这就是我寻求帮助的原因。

我最近更改了主机并且脚本停止了工作。在移动所有文件之后,新主机说mysql已被弃用,所以在做了一些搜索之后我决定用mysqli更改mysql并且不推荐使用的错误消失了。但现在我收到一个新错误(错误选择db)。我想我必须对脚本进行adidtional更改才能连接mysqli。这是我的文件

<?php

class Model{
	var $conn;
	
	public function openDb($dbhost, $dbuser, $dbpass, $dbname, $conn) 
	{
		//echo "Se creo la conexion ";
		$conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysqli');
		mysqli_select_db($dbname) or die('Error select db');
		mysqli_query("SET NAMES utf8");
		return $conn;
	}

	public function closeDb($conn) 
	{
		mysqli_close($conn);
	}

	public function query($query) 
	{
		if ($result = mysqli_query($query) or die("Error de Query: </br >" . mysqli_error()."<br/>".$query)) {
		//if ($result = mysqli_query($query)) {
		} else {
			$result = false;
		}
		return $result;
	}
	
	function __construct()
	{
		$this->openDb(dbhost, dbuser, dbpass, dbname, $conn);
	}
	
	 //insertGenerico con indedices asiciativos
    function insertar($tabla, $datos) 
    {
        $columnas = implode(", ", array_keys($datos));
        $valores = implode(", ", $datos);
        $query = "INSERT INTO $tabla
        ($columnas)
        VALUES
        (" . $valores . ")";
        return $this -> query($query);
    }

    function insertarRelacionArray($tabla, $tablaRelacion, $datos) 
    {
        foreach ($datos as $row) {
            $query = "INSERT INTO $tabla
            ($tablaRelacion[0],$tablaRelacion[2])
            VALUES
            ($tablaRelacion[1],$row)";
            //echo '<br>'.$query;
            $this -> query($query);
        }
    }

    //getGenerico
    function get($tabla, $where = false, $order = false) 
    {
        $query = "SELECT *
        FROM $tabla
        $where
        $order";
        return $this -> query($query);
    }

    //deleteGenerico
    function delete($tabla, $id, $idTag = false) 
    {
    	if($idTag==false)
            $idTag = "id";
        $query = "DELETE FROM $tabla
        WHERE $idTag = $id";
        return $this -> query($query);
    }

    //update generico
    function update($tabla, $datos, $id, $idTag = false) 
    {
        $columnas = array_keys($datos);
        $SET = 'SET ';
        $i = 0;
        foreach ($datos as $key => $value) {
            if (next($datos)) {
                $SET .= "$key = $value ,";
            } else {
                $SET .= "$key = $value ";
            }
        }
        if($idTag==false)
            $idTag = "id";
        $query = "UPDATE $tabla $SET WHERE $idTag = $id;";
        return $this -> query($query);
    }

	
}
?>

1 个答案:

答案 0 :(得分:1)

http://php.net/manual/en/mysqli.select-db.php所述,第一个参数应为$ link。

bool mysqli_select_db ( mysqli $link , string $dbname )

所以改变

mysqli_select_db($dbname) or die('Error select db');

mysqli_select_db($conn, $dbname) or die('Error select db');

它应该有效。 我认为你需要在你的_construct函数中返回$ conn,或者将它存储在对象中($ this-&gt; conn = $ conn),这样你就可以再次使用它了。

此外(感谢KiJéy)您还应该更新

mysql_query($query)
to
mysqli_query($conn, $query)

mysql_real_escape_string($string)
to
mysqli_real_escape_string ($conn, $string)