Conexão PHP - Mysql

SQL

PHP

HTML5

25/05/2019

Galera, to tentando fazer uma conexão entre PHP e Mysql, com o seguinte código:

<?php
session_start();

$username ="";
$email ="";
$errors = array();

//connect to the database
$db = mysqli_connect('localhost', 'root', '', 'registration');

//se o botão de registro for clicado
if(isset($_POST['register'])) {
$username = mysql_real_escape_string($_POST['username']);
$email = mysql_real_escape_string($_POST['email']);
$password_1 = mysql_real_escape_string($_POST['password_1']);
$password_2 = mysql_real_escape_string($_POST['password_2']);

//ensure that from fields are field properly
if(empty($username)) {
array_push($errors ,"Username is required");
}
if(empty($email)) {
array_push($errors ,"Email is required");
}
if(empty($password_1)) {
array_push($errors ,"Password is required");
}
if($passowrd_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}

//if there are not errors, save user to database
if(count() == 0) {
$password = md5($password_1);
$sql = "INSERT INTO users(username, email, password)
VALUES ('$username', '$email', '$password')";
mysqli_query($db, $sql);
$_SESSION['username'] = $username;
$_SESSION['sucess'] = "You are now logged in";
header('location: index.php');
}
}

//log user in from login page
if (isset($_POST['login'])) {
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
}

if(empty($username)) {
array_push($errors ,"Username is required");
}
if(empty($password)) {
array_push($errors ,"Password is required");
}

if (count($errors) == 0){
$password = md5($password);
$query = "SELCT * FROM users WHERE username= '$username' AND password = '$password'";
$result = mysqli_query($db, $query);
if (mysqli_num_rows($result) == 1) {

$_SESSION['username'] = $username;
$_SESSION['sucess'] = "You are now logged in";
header('location: index.php');

}else{
array_push($errors, "wrong username/password combination");
}
}

//logout
if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['username']);
header('location: login.php');
}

?>

E estou obtendo o seguinte erro: Call to undefined function mysql_real_escape_string() in C:\\xampp\\htdocs\\registration\\server.php:46 Stack trace: #0 C:\\xampp\\htdocs\\registration\\login.php(1): include() #1 thrown in C:\\xampp\\htdocs\\registration\\server.php on line 46

Não faço a menor ideia de como arrumar esse erro, me ajudem por favor...
Julio Cesar

Julio Cesar

Curtidas 0

Respostas

William Nascimento

William Nascimento

25/05/2019

Galera, to tentando fazer uma conexão entre PHP e Mysql, com o seguinte código:

<?php
session_start();

$username ="";
$email ="";
$errors = array();

//connect to the database
$db = mysqli_connect('localhost', 'root', '', 'registration');

//se o botão de registro for clicado
if(isset($_POST['register'])) {
$username = mysql_real_escape_string($_POST['username']);
$email = mysql_real_escape_string($_POST['email']);
$password_1 = mysql_real_escape_string($_POST['password_1']);
$password_2 = mysql_real_escape_string($_POST['password_2']);

//ensure that from fields are field properly
if(empty($username)) {
array_push($errors ,"Username is required");
}
if(empty($email)) {
array_push($errors ,"Email is required");
}
if(empty($password_1)) {
array_push($errors ,"Password is required");
}
if($passowrd_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}

//if there are not errors, save user to database
if(count() == 0) {
$password = md5($password_1);
$sql = "INSERT INTO users(username, email, password)
VALUES ('$username', '$email', '$password')";
mysqli_query($db, $sql);
$_SESSION['username'] = $username;
$_SESSION['sucess'] = "You are now logged in";
header('location: index.php');
}
}

//log user in from login page
if (isset($_POST['login'])) {
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
}

if(empty($username)) {
array_push($errors ,"Username is required");
}
if(empty($password)) {
array_push($errors ,"Password is required");
}

if (count($errors) == 0){
$password = md5($password);
$query = "SELCT * FROM users WHERE username= '$username' AND password = '$password'";
$result = mysqli_query($db, $query);
if (mysqli_num_rows($result) == 1) {

$_SESSION['username'] = $username;
$_SESSION['sucess'] = "You are now logged in";
header('location: index.php');

}else{
array_push($errors, "wrong username/password combination");
}
}

//logout
if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['username']);
header('location: login.php');
}

?>

E estou obtendo o seguinte erro: Call to undefined function mysql_real_escape_string() in C:\\\\xampp\\\\htdocs\\\\registration\\\\server.php:46 Stack trace: #0 C:\\\\xampp\\\\htdocs\\\\registration\\\\login.php(1): include() #1 thrown in C:\\\\xampp\\\\htdocs\\\\registration\\\\server.php on line 46

Não faço a menor ideia de como arrumar esse erro, me ajudem por favor...


Olá Julio, acredito que você tenha que ir até o seu
php.ini
e verificar se o esse cara:
extension=mysql.so
está descomentado sem o
;
antes dele.
Geralmente o seu
php.ini
fica no local onde no seu caso instalou o xaamp.
GOSTEI 0
POSTAR