【问题标题】:How to echo an item from a database如何回显数据库中的项目
【发布时间】:2020-06-09 14:19:14
【问题描述】:

我还在学习 PHP,所以请原谅我糟糕的代码。 我正在尝试在用户登录后输出他们的名字,但是没有任何返回,请我帮忙。

   <?php

session_start();

$DATABASE_HOST="localhost";
$DATABASE_USER="root";
$DATABASE_PWORD="";
$DATABASE_NAME="registration";

$connection=mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PWORD, $DATABASE_NAME);
if (mysqli_connect_errno()){
    //if there is an issue with connecting to the database, ends code and displays the error
    die("failed to connect to server: " .mysqli_connect_error()); //kills program
}

if (!isset($_POST['email'], $_POST['pswd'])){ //checking if both fields were inputted into on the form, isset()checks if data exists
    //unable to get data
    die("please fill in both email and password"); //kills program
}

$email = mysqli_real_escape_string($connection, $_POST['email']); //saves input as string, preventing misinterpretation
$password = mysqli_real_escape_string($connection, $_POST['pswd']);//saves input as string, preventing misinterpretation

$SQLstatement = "SELECT * FROM users WHERE email='$email' and password='$password'"; //querys the database for match

$Queryresult = mysqli_query($connection, $SQLstatement) or die(mysqli_error($connection)); //runs the query 

$rowsQueryResult = mysqli_num_rows($Queryresult);//number of 'emails' in database where the emails match
$dbFirstName=$rowsQueryResult ['firstName'];
    if ($rowsQueryResult==1){//if the number of emails where a match is made, is 1 
        echo "Welcome $dbFirstName <br/> ";
        echo "successful login. <a href='accountPage.php'>Click</a> here to access the accounts page";      //successful login, links to accounts page
        $_SESSION['firstName']=$dbFirstName;

    }else{ //if matches are 0 or >=2 
        die ('unsuccessful login'); //kills program
    }

?>

感谢您的宝贵时间和帮助

【问题讨论】:

  • mysqli_num_rows 应该是mysqli_fetch_assoc
  • 非常感谢,问题已解决
  • 我强烈建议切换到PDO,而不是使用简单的mysqli

标签: php html sql xampp


【解决方案1】:

这个问题可以通过使用mysqli_fetch_assoc()函数代替mysqli_num_rows()来解决。但是,我建议您使用 PDO,因为它更易于实现且更具可读性。

【讨论】:

    【解决方案2】:

    mysqli_num_rows() 函数返回结果集中的行数。

    $rowsQueryResult = mysqli_num_rows($Queryresult);`
    

    将在电子邮件匹配的数据库中提供“电子邮件”的数量。

    你需要使用mysqli_fetch_assoc()作为

    $row = mysqli_fetch_assoc($Queryresult);
    $dbFirstName=$row['firstName'];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 2018-05-14
      • 1970-01-01
      相关资源
      最近更新 更多