Jump to content

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given i


anton_1

Recommended Posts

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.

Link to comment
Share on other sites

why does no-one ever read before they post? :shrug:

 

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.