0

每个人都说从 MySQL 数据库打印数据很容易,但到目前为止我很难过。我是 PHP 新手,正在尝试从数据库中打印出个人数据。例如,Joe Bloggs 已经登录,然后他可以查看他的爱好等。所以我想要实现的是让用户登录并查看他们的信息。

基本上,用户可以登录,但是我尝试设置一个变量来存储 SQL 查询,然后在用户成功登录时将其打印出来,但是我现在收到以下错误消息 - 这不会消失。

( ! ) SCREAM: Error suppression ignored for
( ! ) Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in    C:\wamp\www\myfiles\login.inc.php on line 32
Call Stack
#   Time    Memory  Function    Location
1   0.0015  365584  {main}( )   ..\index.php:0
2   1.0144  387472  include( 'C:\wamp\www\myfiles\login.inc.php' )  ..\index.php:6
3   1.0427  390752  mysql_fetch_array ( )   ..\login.inc.php:32

    

像几乎所有初学者一样,我犯了一个错误,即创建了一个使尝试编码的内容过于复杂的大型文件,(这也是一个深夜,我很累,因此我可能看不到任何错误)。不过评论很好

我将不胜感激任何意见和建议!

// create variables username and password,if a user doesn't enter a UN and PW send error message
if (isset($_POST['username'])&& isset ($_POST['password'])){
    $username= $_POST['username'];
    $password= $_POST['password'];

    if (empty ($username)&&empty ($password)){
        echo 'supply username and password';
    }
// save  our MySQl queries as variables in order to reference them later on

    // for login
    $query = "SELECT * FROM `students` WHERE `username`='$username'AND `password`='$password'";
    // user login result
    $result= mysql_query($query);
    // display the courses that user is taking
    $user= "SELECT * FROM `students`";

// set conditions for login 
    if($result) {
    // if the data matches by rows send a message saying the user has logged in
        if(mysql_num_rows($result) > 0) {
            session_regenerate_id();
            $member = mysql_fetch_assoc($result);

            $_SESSION['SURNAME_NAME'] = $member['username'];
            $_SESSION['SESS_ID'] = $member['password'];
            session_write_close();
            //if logged on list course they are studying
            echo 'you are now logged in.<a href = "logout.inc.php">Log out</a>';
                while($row = mysql_fetch_array($user))
                {
                    echo $row['GivenName'],$row['username'], $row['password'],$row['Course1'],$row['Course2'], $row['Course2'];
                    echo "<br />";
                }
            exit();
        }else {
            // create boolean condition- if no match send error message
            $errflag = true;
            if($errflag) {
            session_write_close();
            echo 'invalid username/password combination';   
            exit();

            }
        }
    }else {
    // display errors
        echo mysql_errno() . ": " . mysql_error(); exit();

    }

}

?>
<form method="POST">
Username: <input type = "text" name="username"> Password: <input type ="password" name="password">
<input type="submit" value="Log in">
</form
4

3 回答 3

3

如果您是使用 php 和 mysql 的初学者取决于您适合使用的连接,以下是一些可能对您有所帮助的基本步骤。

MYSQL

首先:为数据库用户、密码、主机、数据库名设置配置

$conn = new mysqli('database_server','database_username','database_password','database_name');

第二:创建一个查询(您可以在此处插入您的查询)..

$result= $conn->query("SELECT SUM(Total_Steps) AS value_sum FROM users");

最后:使用 MYSQL 函数显示的记录,如..

while($row = $result->fetch_assoc()){
    echo $row['dabatase_columnname'];
    echo $row['database_columnname'];
}

PDO

连接到数据库 db = new PDO("...");

创建查询

$statement = $db->prepare("select id from some_table where name = :name");

使用绑定参数执行查询

$statement->execute(array(':name' => "Jimbo"));

获取查询返回的所有数据

$row = $statement->fetch(); // Use fetchAll() if you want all results, or just iterate over the statement, since it implements Iterator

MYSQL

首先:为数据库用户、密码、主机、数据库名设置配置

$conn = mysql_connect('database_server','database_username','database_password');
mysql_select_db($conn,'databasename')

第二:创建一个查询(您可以在此处插入您的查询)..

$result= mysql_query("SELECT SUM(Total_Steps) AS value_sum FROM users");

最后:使用 MYSQL 函数显示的记录,如..

while($row = $result->mysql_fetch_array($result)){
    echo $row['dabatase_columnname'];
    echo $row['database_columnname'];
}
于 2013-04-03T00:49:28.873 回答
0

如前所述,避免在新代码中使用 mysql_* 函数。它们不再被维护并被正式弃用。

这是您的代码示例,使用 PDO 和准备好的语句。您可能只需要更改连接信息。

<?php
// Get your connection sorted.
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
// Prepare a few statements for use later.
$stmtResult = $db->prepare("SELECT * FROM `students` WHERE `username`=?AND `password`=?");
$stmtUser = $db->prepare("SELECT * FROM `students`");

// create variables username and password,if a user doesn't enter a UN and PW send error message
if (isset($_POST['username'])&& isset ($_POST['password'])){
    $username= $_POST['username'];
    $password= $_POST['password'];

    if (empty ($username)&&empty ($password)){
        echo 'supply username and password';
    }

    // Use the try catch paradigm instead of if/else, because PDO will
    // throw an exception if things go sideways.
    try{
        // Execute the prepared statement, passing in the variables.
        $stmtResult->execute(array($username, $password));
        // Get the results in an array & then get the result count.
        $rows = $stmtResult->fetchAll(PDO::FETCH_ASSOC);
        $row_count = $stmt->rowCount()

        if($row_count > 0) {
            session_regenerate_id();
            $member = $rows[0];

            $_SESSION['SURNAME_NAME'] = $member['username'];
            $_SESSION['SESS_ID'] = $member['password'];
            session_write_close();
            //if logged on list course they are studying
            echo 'you are now logged in.<a href = "logout.inc.php">Log out</a>';
            $stmtUser->execute();

            // I moved this next call down to the loop because
            // there is no real reason to call it sooner than this
            while($row =  $stmtResult->fetchAll(PDO::FETCH_ASSOC))
            {
                echo $row['GivenName'],$row['username'], $row['password'],$row['Course1'],$row['Course2'], $row['Course2'];
                echo "<br />";
            }
            exit();
        }else {
            // create boolean condition- if no match send error message
            $errflag = true;
            if($errflag) {
            session_write_close();
            echo 'invalid username/password combination';   
            exit();

            }
        }
    }catch(PDOException $ex) {
        // This is what happens if something did go sideways.
        echo $ex->getCode() . ": " . $ex->getMessage(); 
        exit();
    }
}

?>
<form method="POST">
Username: <input type = "text" name="username"> Password: <input type ="password" name="password">
<input type="submit" value="Log in">
</form
于 2013-04-03T01:11:17.357 回答
0

问题是你没有这样做:

 mysql_query($user);

只需在mysql_fetch_arraylike之前添加:

$user_result = mysql_query($user);
while($row = mysql_fetch_array($user_result)){
     //...
}

但是,是的,正如另一个人所说,请使用MySqliorPDO代替。

于 2013-04-03T00:58:16.617 回答