doude1917 2018-06-02 07:25
浏览 31
已采纳

php 7的PHP代码迁移[重复]

This question already has an answer here:

I am an amateur programmer. I have a php code that handles the login of my site, which has functions that have been deprecated in php 7.x.x. The code was made for php 5 etc.

Since MySQL has been removed for php 7 I tried getting the code work by working around it by replacing it with mysqli etc but getting the error: Parse error: syntax error, unexpected '{', expecting ':' in /loginscript.php on line 33

The original code is:

<?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");
}

?>

What I tried to do (which failed) is:

<?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");
}
?>

Please help me get this code work on php 7. Thanks

</div>
  • 写回答

1条回答 默认 最新

  • douzhou7656 2018-06-02 07:42
    关注

    The syntax is either:

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

    or

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

    You're kind of using both, which gives an error. Remove the : from your line

    if (mysqli_num_rows($result)>0):
    

    and you should be good to go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?