mysqli_select_db () expects parameter 1 to be mysqli, string given [duplicate]

2

I have a problem here with Mysql, it is giving errors, I have made all kinds of changes. I have tried to change the Mysql function to Mysqli and only increase errors.

Code:

<?php
   $sql["host"] = "localhost";
    $sql["usuario"] = "root";
    $sql["senha"] = "arfito";
    $sql["base"] = "digify";
    $conexao = mysql_connect($sql["host"],$sql["usuario"],$sql["senha"]);
    $select_database = mysql_select_db($sql["base"], $conexao);
    mysql_query("SET NAMES utf8");
?>

and gives the following error:

  

Fatal error: Uncaught Error: Call to undefined function mysql_connect () in /var/www/public_html/includes/config.php:6 Stack trace: # 0 /var/www/public_html/index.php (7): include () # 1 {main} thrown in /var/www/public_html/includes/config.php on line 6

When I switch from Mysql to Mysqli it gives the following error:

  

Warning: mysqli_select_db () expects parameter 1 to be mysqli, string given in /var/www/public_html/includes/config.php on line 7

     

Warning: mysqli_query () expects at least 2 parameters, 1 given in /var/www/public_html/includes/config.php on line 8

Fatal error: Uncaught Error: Call to undefined function mysql_fetch_array() in /var/www/public_html/index.php:9 Stack trace: #0 {main} thrown in /var/www/public_html/index.php on line 9

Line # 9 of the Index.php code:

$web = mysql_fetch_array(mysql_query("SELECT * FROM settings ORDER BY id DESC LIMIT 1"));
    
asked by anonymous 29.10.2017 / 14:41

2 answers

3

Notice the error that gives you to the mysql_connect function:

  

Fatal error: Uncaught Error: Call to undefined function   mysql_connect () ....

This is because in the environment you are running it does not even exist. When we look at the documentation of this function we see that it has been removed from PHP 7.0.0 :

  

Warning This extension was deprecated in PHP 5.5.0, and it was removed   in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used (...)

The documentation itself tells you to switch to MySQLi or PDO . If you have a version PHP 7 or higher you have to change.

Your code with MySQLi looks like this:

$sql["host"] = "localhost";
$sql["usuario"] = "root";
$sql["senha"] = "arfito";
$sql["base"] = "digify";

//a conexão agora passa a levar a base de dados com quarto parâmetro
$conexao = mysqli_connect($sql["host"],$sql["usuario"],$sql["senha"], $sql["base"]);

mysqli_query($conexao, "SET NAMES utf8");
//--------------^ conexão é agora o 1º parametro do mysqli_query

$web = mysqli_fetch_array(mysqli_query($conexao, "SELECT * FROM settings ORDER BY id DESC LIMIT 1"));
//--------------------------------------^ conexão é agora o 1º parametro do mysqli_query

The mysql_fetch_array has been changed to mysqli_fecth_array , and the mysql_query has passed mysqli_query , but you also have to take the connection as the first parameter

Documentation for mysqli_connect and for mysqli_query

    
29.10.2017 / 14:58
2

Correction to mysqli:

$sql["host"] = "localhost";
$sql["usuario"] = "root";
$sql["senha"] = "arfito";
$sql["base"] = "digify";
$conexao = mysqli_connect($sql["host"],$sql["usuario"],$sql["senha"]);
$select_database = mysqli_select_db($conexao, $sql["base"]);
mysqli_query($conexao, "SET NAMES utf8");

Useful links:

mysqli_select_db

mysqli_connect

mysqli_query

    
29.10.2017 / 14:57