PHP 7的PHP代码迁移

时间:2018-06-02 07:25:27

标签: php

我是一名业余程序员。我有一个PHP代码来处理我的网站的登录,其中包含已在php 7.x.x中弃用的功能。代码是为php 5等制作的。

由于已经删除了MySQL for MySQL,我尝试通过使用mysqli等替换它来解决代码问题但得到错误:解析错误:语法错误,意外' {&#39 ;期待':'在第33行的/loginscript.php中

原始代码是:

<?php session_start();
require '../include/sqlconn.php';
$user = $_POST['user_name'];
$password = $_POST['password'];
$_SESSION['user'] =$user;
$sqlsel ="select * from user where user_name='$user' and password='$password'";

$result=mysql_query($sqlsel);
$n= mysql_num_rows($result);
if ($n >0)
{
    $auth = 1;
    $_SESSION['auth'] = $auth;
    while ($row =mysql_fetch_assoc($result))
{
    extract($row);
    $_SESSION['right'] =$right;
    $_SESSION['location'] =$location;

    if ($_SESSION['right'] == "Administrator")
    {
        header("location:../setup/main.php");
    }
    if ($_SESSION['right'] == "User")
    {
      header("location:../user/main.php");
    }

}
}
else
{
    header("location:logout.php");
}

?>

我试图做的(失败的)是:

<?php session_start();
require '../include/sqlconn.php';
$user = $_POST['user_name'];
$password = $_POST['password'];
$_SESSION['user'] =$user;
$con = mysqli_connect("f3.server.com", "2069590_test", "pass");
mysqli_select_db($con, "2069590_test");

$result=mysqli_query($con, "select * from user where user_name='$user' and password='$password'") or die(mysqli_error($con));

if (mysqli_num_rows($result)>0):
{
    $auth = 1;
    $_SESSION['auth'] = $auth;
    while ($row =mysql_fetch_assoc($result))
{
    extract($row);
    $_SESSION['right'] =$right;
    $_SESSION['location'] =$location;

    if ($_SESSION['right'] == "Administrator")
    {
        header("location:../setup/main.php");
    }
    if ($_SESSION['right'] == "User")
    {
      header("location:../user/main.php");
    }

}
}
else
{
    header("location:logout.php");
}
?>

请帮助我在php 7上使用此代码。谢谢

1 个答案:

答案 0 :(得分:1)

语法是:

if ( $x > $y ):
  // do something
endif;

if ( $x > $y ) {
  // do something
}

你有两种使用,这会产生错误。从您的行中删除:

if (mysqli_num_rows($result)>0):

你应该好好去。