anton_1 Posted July 27, 2011 Share Posted July 27, 2011 ERROR MESSAGE Notice: Undefined index: id in C:\xampp\htdocs\Again\person.php on line 12 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Again\person.php on line 16 Return to the list <html> <head> <title>Retrieve data from database</title> </head> <body> <dl> <?php include 'dbconnect.php'; // Get data from the database depending on the value of the id in the URL $strSQL = "SELECT * FROM people WHERE id=" . $_GET["id"]; $rs = mysql_query($strSQL); // Loop the recordset $rs while($row = mysql_fetch_array($rs)) { // Write the data of the person echo "<dt>Name:</dt><dd>" . $row["FirstName"] . " " . $row["LastName"] . "</dd>"; echo "<dt>Phone:</dt><dd>" . $row["Phone"] . "</dd>"; echo "<dt>Birthdate:</dt><dd>" . $row["BirthDate"] . "</dd>"; } // Close the database connection mysql_close(); ?> </dl> <p><a href="list.php">Return to the list</a></p> </body> </html> Thank you in advance for any help! MOD EDIT: code tags added. Quote Link to comment https://forums.phpfreaks.com/topic/242965-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-i/ Share on other sites More sharing options...
Pikachu2000 Posted July 27, 2011 Share Posted July 27, 2011 When posting code, enclose it within the forum's . . . BBCode tags. The error means your query is failing and returning FALSE instead of a valid query result resource to mysql_fetch_array() . Quote Link to comment https://forums.phpfreaks.com/topic/242965-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-i/#findComment-1247933 Share on other sites More sharing options...
Muddy_Funster Posted July 27, 2011 Share Posted July 27, 2011 why does no-one ever read before they post? your query failed to return a dataset. because the value of GET['id'] wasn't found in the table, because the value of GET['id'] is not found using double quotes, it is GET['id'] with single quotes, although I expect it's probably a url malformation from the reffering page. Please post code that is from the page that links to this one. two things you need to learn - error capture and variable checking. Quote Link to comment https://forums.phpfreaks.com/topic/242965-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-i/#findComment-1247936 Share on other sites More sharing options...
Pikachu2000 Posted July 27, 2011 Share Posted July 27, 2011 $_GET['id'] is no different than $_GET["id"]. Quote Link to comment https://forums.phpfreaks.com/topic/242965-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-i/#findComment-1247942 Share on other sites More sharing options...
anton_1 Posted July 27, 2011 Author Share Posted July 27, 2011 previous page: <html> <head> <title>Retrieve data from the database</title> </head> <body> <ul> <?php include 'dbconnect.php'; // 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 = 'person.php?id = " . $row['id'] . "'>" . $strName . "</a>"; // List link echo "<li>" . $strLink . "</li>"; } // Close the database connection mysql_close(); ?> </ul> </body> </html> Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/242965-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-i/#findComment-1247945 Share on other sites More sharing options...
marcus Posted July 27, 2011 Share Posted July 27, 2011 $id = mysql_real_escape_string($_GET['id']); // or $id = (int) $_GET['id']; if($id){ // id has a value // perform query }else { // error: no id supplied } Quote Link to comment https://forums.phpfreaks.com/topic/242965-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-i/#findComment-1247949 Share on other sites More sharing options...
AyKay47 Posted July 27, 2011 Share Posted July 27, 2011 $id = mysql_real_escape_string($_GET['id']); // or $id = (int) $_GET['id']; if(!empty($id)){ // id has a value // perform query }else { // error: no id supplied } $id will return true if it contains an empty string Quote Link to comment https://forums.phpfreaks.com/topic/242965-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-i/#findComment-1247955 Share on other sites More sharing options...
Muddy_Funster Posted July 27, 2011 Share Posted July 27, 2011 try changing where you assign $strLink to this: $strLink = "<a href='person.php?id={$row['id']}'>$strName</a>"; Quote Link to comment https://forums.phpfreaks.com/topic/242965-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-i/#findComment-1247958 Share on other sites More sharing options...
anton_1 Posted July 27, 2011 Author Share Posted July 27, 2011 Thanks Muddy_Funster that was it. dont think it knew which array to look at correct? Thank you also guys for your help! Quote Link to comment https://forums.phpfreaks.com/topic/242965-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-i/#findComment-1247963 Share on other sites More sharing options...
marcus Posted July 27, 2011 Share Posted July 27, 2011 $id = mysql_real_escape_string($_GET['id']); // or $id = (int) $_GET['id']; if(!empty($id)){ // id has a value // perform query }else { // error: no id supplied } $id will return true if it contains an empty string My original code will return false if the URL contains just ?id= or ?id=0, our code is the same, you just made use of the empty function. I tested it using $id = (int) $_GET['id']; ?id= => false ?id=0 => false ?id=1 => true ?id=s => false Quote Link to comment https://forums.phpfreaks.com/topic/242965-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-i/#findComment-1247964 Share on other sites More sharing options...
Muddy_Funster Posted July 28, 2011 Share Posted July 28, 2011 Thanks Muddy_Funster that was it. dont think it knew which array to look at correct? Thank you also guys for your help! Nah, problem was that the link was malformed with the spaces inside the href quotes. Just got to be carefull when making concatonated strings on the fly (I suggest always echoing them during development to make sure they look like you expect them to). Glad it's working though, good luck. Quote Link to comment https://forums.phpfreaks.com/topic/242965-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-i/#findComment-1248359 Share on other sites More sharing options...
AyKay47 Posted July 28, 2011 Share Posted July 28, 2011 basically you were concatenating with double quotes when you should have used singles quotes, i myself prefer the complex syntax as muddy_funster showed you Quote Link to comment https://forums.phpfreaks.com/topic/242965-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-i/#findComment-1248505 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.