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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分