How to change Mysql to Mysqli

Asked

Viewed 594 times

0

Well, half of my project is using mysql plus I added a new function that uses mysqli which is generating some conflicts in the database. Either it saves what is in mysql or what is in mysqli. I tried to change to mysqli but could not make it work.

When using this mysql script only mysqli does not work

<?php

error_reporting(0);
ini_set(“display_errors”, 0 );

$conectar = mysql_connect("localhost","root","") or die ("Erro na conexão");
mysql_select_db("tcc")or die ("Base não encontrada");
?>

Already this only works mysqli and not mysql

<?php
	$servidor = "localhost";
	$usuario = "root";
	$senha = "";
	$dbname = "usuarios";
	
	//Criar a conexao
	$conn = mysqli_connect($servidor, $usuario, $senha, $dbname);
?>

I wanted to know how I change mysqli pro mysql already tried to remove the i mysqli no more right.


I changed my connection to mysqli_ and I can’t log in possibly because it’s not connecting to the database because the codes are in mysql_

The login code

<?php
session_start();
$usuariot = $_POST['usuario'];
$senhat = $_POST['senha'];
include_once("conexao.php");

$result = mysql_query("SELECT * FROM usuarios WHERE login='$usuariot' AND senha='$senhat' LIMIT 1");
$resultado = mysql_fetch_assoc($result);
//echo "Usuario: ".$resultado['nome'];
if(empty($resultado)){
	//Mensagem de Erro
	$_SESSION['loginErro'] = "Usuário ou senha Inválido";
	
	//Manda o usuario para a tela de login
	header("Location: index.php");
}else{
	//Define os valores atribuidos na sessao do usuario
	$_SESSION['usuarioId'] 			= $resultado['id'];
	$_SESSION['usuarioNome'] 		= $resultado['nome'];
	$_SESSION['usuarioNivelAcesso'] = $resultado['nivel_acesso_id'];
	$_SESSION['usuarioLogin'] 		= $resultado['login'];
	$_SESSION['usuarioSenha'] 		= $resultado['senha'];
	
	if($_SESSION['usuarioNivelAcesso'] == 1){
		header("Location: administrativo.php");
	}else{
		header("Location: usuario.php");
	}
}
?>

I tried to change the mysql_query and the mysql_fetch_assoc adding the i but it wasn’t enough I can’t log in

  • 2

    pq vc will use obsolete functions?

  • 99% of my project is in mysql

  • It depends on how the code is but not too much work. The main difference is that in the functions mysqli_ the first argument is almost always the connection already in the mysql_ connection is the second argument and is not required.

  • That answer Have an example of how to migrate frommysql_ to mysqli_.

  • You can help me with this part because this is similar to the others so I can change everything to mysqli_

  • 1

    Reinforcing what @rray said, why use functions that no longer exist in php version 7? This doesn’t solve the problem, it just postpones it. Later will probably return to the same problem and will be forced to change even. Make correct and change everything to mysqli

Show 1 more comment
No answers

Browser other questions tagged

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