Fatal error generated in PHP code msql. How to update the code to mysqli and PHP 7 and fix the error?

Asked

Viewed 38 times

0

I’m a beginner in PHP, I’m doing a real estate project in PHP of a course I bought, and a problem appeared in the configuration part of the project’s login panel. The problem is that when I authenticate the login and password fields it accuses the following fatal Xdebug error:

( ! ) Fatal error: Uncaught Error: Call to Undefined Function mysql_select_db() in C: wamp64 www real estate admin index.php on line 52 ( ! ) Error: Call to Undefined Function mysql_select_db() in C: wamp64 www property admin index.php on line 52

The course is kind of old, the PHP version of the course is below version 5 and it still uses methods and functions of the discontinued mysql database connection. I am developing the project with wamp in version PHP 7 and with the ide visual studio code, and in this version of wamp only use the connections of standard mysqli and PDO. In the course the instructor uses a Dreamweaver menu feature that generates the connection code automatically in procedural format. The problem is that the generated code uses discontinued methods of mysql connection and I have tried everything to fix the connection problem, I have tried to replace the discontinued methods with those of mysqli but more error messages appear. I would like to know the correct way to update the code to mysqli and how to use better and simpler code than the one generated by dreanweaver and done in the nail. Below is the link configuration page code and the index page with the Dreamweaver code. Thank you, I’m waiting.

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_painel_config = "localhost";
$database_painel_config = "imobi";
$username_painel_config = "root";
$password_painel_config = "";
$painel_config = mysqli_connect($hostname_painel_config,
 $username_painel_config, $password_painel_config) 
 or trigger_error(mysqli_connect_error(),E_USER_ERROR); 
?>

page:

<?php require_once('C:\wamp64\www\imobiliaria\connections\painel_config.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['email'])) {
  $loginUsername=$_POST['email'];
  $password=$_POST['senha'];
  $MM_fldUserAuthorization = "usuarioNivel";
  $MM_redirectLoginSuccess = "painel.php";
  $MM_redirectLoginFailed = "restrito.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_painel_config, $painel_config);

  $LoginRS__query=sprintf("SELECT email, senha, usuarioNivel FROM freitas_clientes WHERE email=%s AND senha=%s",
  GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 

  $LoginRS = mysql_query($LoginRS__query, $painel_config) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {

    $loginStrGroup  = mysql_result($LoginRS,0,'usuarioNivel');

  if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;       

    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];  
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Faça seu Login - Freitas Imóveis</title>
<link rel="stylesheet" type="text/css" href="login_style.css">
</head>

<body>

<div id="login">
  <img src="images/logo.png" alt="" />
   <form name="login_painel" action="<?php echo $loginFormAction; ?>" method="POST">
    <label><span>E-mail: </span><input type="text" name="email" /></label>
    <label><span>Senha: </span><input type="password" name="senha" /></label>
    <p><a href="recover.php">[ Esqueci minha senha ]</a><p>
    <input type="submit" name="logar" value="Logar" class="btn" />
  </form>
</div>

</body>
</html>
  • mysqli_connect() pass as fourth argument the name of the database. There are more errors there, I suggest to read the links on the right.

  • I already did that and keep giving the same error message. I also tried to perform the procedure of a problem with the code similar to mine of this stack overlfow http://answall.com/questions/88119/mysqli-escape-string-expects-exactly-2-parameters-1-Given , in it the user says he solved the problem by calling the 'include' below the line:Function Getsqlvaluestring($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {, at the error: mysqli_real_escape_string($config, $theValue), he called $link. but I don’t know how to make the call

  • When migrating a code that uses the functions MySQL (obsolete) for the MySQLi a tip is, in almost all functions the first argument to be passed is the connection, at that link has the list of functions and which arguments should be passed. Detail there is no corresponding function for mysql_result() in Mysqli.

No answers

Browser other questions tagged

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