mysqli_select_db() expects Parameter 1 to be mysqli, string Given

Asked

Viewed 3,764 times

2

I have a problem here with Mysql, is giving errors, I’ve made all kinds of changes. I’ve 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 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 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 Index.php code:

$web = mysql_fetch_array(mysql_query("SELECT * FROM settings ORDER BY id DESC LIMIT 1"));
  • mysql* does not exist since php version 7 so you really have to use mysqli. Show which code you used for the mysqli version. I remember that mysqli_query takes as first parameter the link

  • I just have this code on top, I’m not very good with mysqli. I thought it was only necessary to change mysql to mysqli.

2 answers

3

Correction for 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

3


Note the error you give to the function mysql_connect:

Fatal error: Uncaught Error: Call to Undefined Function mysql_connect() ....

This is because in the environment that is running it does not even exist. When we look at the documentation of that function we see that it has been removed from PHP version 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 indicates that you must change to Mysqli or PDO. If you have a PHP version 7 or higher you really have to change.

Your code with Mysqli would look 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 passed to mysqli_fecth_array, and the mysql_query passed to mysqli_query but you also have to take the link as the first parameter

Documentation for the mysqli_connect and to the mysqli_query

  • Thank you, it worked. You made my day, I spent sleepless nights struggling with it. Thank you @Isac

  • 1

    @You’re welcome, I’m glad I could help

  • Thank you very much. God bless.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.