fatal error in mysql_select_db( )

Asked

Viewed 2,229 times

0

I’m new to PHP and I am currently studying PDO, made a simple structure and has an error that I cannot identify. I have seen several examples of people using the same structure and it works.

<?php
$host = "localhost";
$user = "root";
$pass = "";
$dbname = "test";

$conexao = mysqli_connect($host, $user, $pass);
$db = mysql_select_db($conexao, $dbname);

$nome = "exemplo";

$consulta = mysql_query("SELECT * FROM usuarios_teste WHERE nome = $nome  ");
?>

The mistakes I get are:

Fatal error: Uncaught Error: Call to Undefined Function mysql_select_db()

Error: Call to Undefined Function mysql_select_db()

  • 2

    The functions mysql_ no longer exist in php7, you must use mysqli_

  • I tried to find something like "mysqli_select_db" in the php manual and did not find, I found that this specific function had not changed, thanks

  • If you are studying PDO I would advise you to take a look at this: http://php.net/manual/book.pdo.php

1 answer

2

The function mysql_select_db() has been removed since version 7.0.0, as indicated in the documentation itself:

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. See also Mysql: Choosing an API guide and Related FAQ for more information. Alternatives to this Function include:

mysqli_select_db() PDO::__Construct() (part of dsn)

If you are using a newer version, you will come across this problem. One solution is to use your replacement mysqli_select_db (notice the "i"):

$con = mysqli_connect("localhost", "my_user", "my_password");

mysqli_select_db ( $con, $dbname );

More details: http://php.net/manual/en/mysqli.select-db.php

  • I looked for something similar to "mysqli_select_db" in the manual and did not find, I found that this function in specific had not changed, thanks!

  • 1

    Even if the author uses a PHP version that has the function, it will not work with Mysqli - Of curiosity, DB can be passed directly at the time of the connection, if you want to delete the select_db line entirely from the play

Browser other questions tagged

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