This question already has an answer here:
Please tell me whats wrong with this? im getting error in line 17 & 21, please help.. The purpose of this program is to fetch & display the details of the user from db.
<?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>";
}
?>
the displayed error messages are:
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
</div>
//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>
// 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>";
}
can u try this .. passing db object
Your query is broken. After the line $rs = mysql_query($strSQL);
add this: var_dump($rs)
to see what's going on and use mysql_error()
to debug it.