The direct connection in mysqli I do so...
$mysqli = new mysqli("localhost", "usuario", "senha", "bancodedados");
if ($mysqli->connect_errno) {
echo "Falha ao conectar com o mysql: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
if you need to send the connection door, it would look like this...
$mysqli = new mysqli("localhost", "usuario", "senha", "bancodedados", 3306);
If you want to work object-oriented you use the expensive PDO, I believe it will help you a lot more, and it is much easier to work object-oriented with it...
Below the class I have here that makes this connection, and an example of another file using this connection...
<?php
class Conexao {
public $conn;
function Conexao() {
$string = "mysql:";
$string .= "host=localhost ";
$string .= "port=5432 ";
$string .= "dbname=nomedobanco";
$string .= "user=usuario";
$string .= "password=senha";
try {
$this->conn = new PDO($string);
}catch(PDOException $e){
echo $e->getMessage();
}
}
}
?>
if you do not want to use the new PDO with the $string variable you can use it as well...
new PDO('mysql:host=localhost;dbname=testdb;charset=utf8mb4', 'username', 'password');
If you want a hint, create a "class.conexao.php" file with this code and in "class.suaclasse.php", you use, as in the example below...
<?php
require('class.conexao.php');
class Querys extends Conexao {
function validaLogin($login, $senha) {
$sql =
"
select nome, cpf from validaUsuario('".$login."', '".$senha."');
";
$stmt = $this->conn->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
}
this function in case validates login, then you can create for what Voce needs...
that part
$stmt = $this->conn->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
serves to prepare the query that Voce will execute, and then run and return the associated results....
Voce would use the return in this way..
<?php
require_once('class.querys.php');
class Login extends Querys {
function validarLogin() {
$query = new Querys();
$retorno = $query->validaLogin(aqui vem os parametros));
return $retorno;
}
}
?>
so there you are working object-oriented.
What mistake you made when you changed ?
– MagicHat
could you pass on more details please ?
– Chefe Druida
updated question with errors
– Éo Ronaldo Dll
Never use text prints that you can copy, please.
– Guilherme Nascimento
You said you switched to mysqli and still gives the error? What mistake, the same mistake? It would not make sense, if it is a new error explain what it is. Do not just go out changing something else, follow the examples of the documentation https://php.net/manual/en/book.mysqli.php, sometimes you should redo everything, no use copying, learn how it works, if it is not the same that you want to drive a car without knowing when to pass the march and take the road without knowing to change a "steppe". You’ll only get headaches
– Guilherme Nascimento
Hello William, have you come to criticize or try to help? as you can see in my question, there is an issue and just below is described the new problem I found.
– Éo Ronaldo Dll