-9

请告诉我这有什么问题?我在第 17 行和第 21 行遇到错误,请帮忙。这个程序的目的是从数据库中获取并显示用户的详细信息。

<?php
// Connect to database server
mysql_connect("localhost", "root", "") or die (mysql_error ());

// Select database
mysql_select_db("lms") or die(mysql_error());

// (Line 17) Get data from the database depending on the value of the id in the URL
$strSQL = "SELECT * FROM login WHERE id=" . $_GET["id"];
$rs = mysql_query($strSQL);

// (Line 21) Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {

    // Write the data of the person
    echo "<dt>Name:</dt><dd>" . $row["name"] . "</dd>";
    echo "<dt>Username:</dt><dd>" . $row["username"] . "</dd>";
    echo "<dt>Rollno:</dt><dd>" . $row["rollno"] . "</dd>";

}
?>

显示的错误消息是:

Undefined index: id in C:\wamp\www\phploginsession\person.php on line 17

mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\phploginsession\person.php on line 21
4

3 回答 3

0

您的查询已损坏。在该行之后$rs = mysql_query($strSQL);添加:var_dump($rs)以查看发生了什么并用于mysql_error()调试它。

于 2013-04-11T11:26:51.003 回答
0
//list.php
<html>
    <head>
    <title>Retrieve data from the database</title>
    </head>
    <body>

    <ul>

    <?php
    // Connect to database server
    mysql_connect("mysql.myhost.com", "user", "sesame") or die (mysql_error ());

    // Select database
    mysql_select_db("mydatabase") or die(mysql_error());

    // SQL query
    $strSQL = "SELECT * FROM people ORDER BY FirstName DESC";

    // Execute the query (the recordset $rs contains the result)
    $rs = mysql_query($strSQL);

    // Loop the recordset $rs
    while($row = mysql_fetch_array($rs)) {

       // Name of the person
      $strName = $row['FirstName'] . " " . $row['LastName'];

       // Create a link to person.php with the id-value in the URL
       $strLink = "<a href = 'test1.php?id=".$row['id']."'>" . $strNavn.' '.$strName . "</a>";

        // List link
       echo "<li>" . $strLink . "</li>";

      }

    // Close the database connection
    mysql_close();
    ?>

    </ul>
    </body>
    </html>
于 2013-04-11T05:30:28.590 回答
0
// Connect to database server
$con=mysql_connect("localhost", "root", "") or die (mysql_error ());
// Select database
mysql_select_db("lms",$con) or die(mysql_error());

// (Line 17) Get data from the database depending on the value of the id in the URL
$strSQL = "SELECT * FROM login WHERE id= 1 ";
$rs = mysql_query($strSQL,$con);

// (Line 21) Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {
    // Write the data of the person
    echo "<dt>Name:</dt><dd>" . $row["name"] . "</dd>";
    echo "<dt>Username:</dt><dd>" . $row["username"] . "</dd>";
    echo "<dt>Rollno:</dt><dd>" . $row["rollno"] . "</dd>";

}

你可以试试这个..传递数据库对象

于 2013-04-11T05:39:15.200 回答