Can't solving this error

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\update.php on line 32
Error: Data not found…

This is very hard to diagnose without seeing your code.
At very least line 32 of update.php would be helpful.

Also worth noting that mysql functions are now obsolete, so should no longer be used.

PHP

$id="";
$name="";
$fathername="";
$mothername="";
$address="";
$birthday="";
$age="";
$email="";
$religion="";
$nationality="";
$gender="";



$servername = "localhost";
$username = "root";
$password = "";
$dbname = "registrationform";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


$id =$_REQUEST['id'];
$result = mysql_query("SELECT * FROM registrationdb WHERE id='$id'");
$row = mysql_fetch_array($result);
if (!$result) 
		{
		die("Error: Data not found..");
		}
	$name=$row["name"];
	$fathername=$row["fathername"];
	$mothername=$row["mothername"];	
   	$address=$row["address"];
	$email=$row["email"];
	$birthday=$row["birthday"];
	$religion=$row["religion"];
    $nationality=$row["nationality"];
    $gender=$row["gender"];	
	
if(isset($_POST["update"])){	
	$name=$_POST["name"];
	$fathername=$_POST["fathername"];
	$mothername=$_POST["mothername"];	
   	$address=$_POST["address"];
	$email=$_POST["email"];
	$birthday=$_POST["birthday"];
	$religion=$_POST["religion"];
    $nationality=$_POST["nationality"];
    $gender=$_POST["gender"];	

	mysql_query("UPDATE registrationdb SET name ='$name', fathername ='$fathername',mothername ='$mothername',address ='$address',email ='$email',birthday ='$birthday',religion ='$religion',nationality ='$nationality',
		 gender ='$gender' WHERE id = '$id'")
				or die(mysql_error()); 
	echo "Saved!";
	
	header("Location: insert.php");			
}
mysql_close($conn);

?>

HTML


<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="raw">
<div class="col-md-8">
  <div class="panel panel-info">
  <div class="panel heading" class="panel panel-primary">Update data</div>
   <div class="panel-body" ></div>
  <form method="post" action="update.php"> 
    
	<div class="form-group">
      <label for="name">Name:</label>
      <input type="text" class="form-control" value=<?php echo $name;?>/>
    </div>
  
  
    <div class="form-group">
      <label for="fathername">Father Name:</label>
      <input type="text" class="form-control" name="fathername" value=<?php echo $fathername;?>>
    </div>
  
  
  
    <div class="form-group">
      <label for="mothername">Mother Name:</label>
      <input type="text" class="form-control" name="mothername" value=<?php echo $mothername;?>>
    </div>
  
    <div class="form-group">
      <label for="address">Address:</label>
      <input type="text" class="form-control" name="address" value=<?php echo $address;?>>
    </div>
  
  
    <div class="form-group">
      <label for="email">Email:</label>
      <input type="email" class="form-control" name="email" value=<?php echo $email;?>>
    </div>
  
  
  
    <div class="form-group">
      <label for="birthday">Birthday:</label>
      <input type="date" class="form-control" name="birthday" value=<?php echo $birthday;?>>
    </div>
  
  
  
  
  <div class="form-group">
    <label for="religion">Raligion:</label>
      <select class="form-control" name="religion" value=<?php echo $religion;?>>
        <option>Muslim</option>
        <option>Hindu</option>
        <option>Chiritian</option>
        <option>Buddha</option>
      </select>
	  </div>
	
	<div class="form-group">
    <label for="Nationalilty">Nationality:</label>
      <select class="form-control" name="nationality" value=<?php echo $nationality;?>>
        <option>Bangladeshi</option>
        <option>Indian</option>
        <option>Nepali</option>
        <option>Pakisthan</option>
      </select>
	  </div>
	
  
	
	<div class="form-group">
    <label for="gender">Gender:</label>
      <select class="form-control" name="gender" value=<?php echo $gender;?>>
        <option>Male</option>
        <option>Female</option>
        <option>Others</option>     
      </select>
	  </div>
	  <button type="submit" name="update" class="btn btn-primary">Update</button>
	  
	  
	  
 </body>
 
 
 
</html>

I think the error indicates that the query returned no results, hence $result is a Boolean false value. You need to check whether you’ve had results before using them, not afterwards are you are here. But switch back to mysqli as you had in your other post, don’t spend time writing new code with the old functions.

1 Like

You are using the variable $id in the query, a value coming from $_REQUEST['id']
But I don’t see an input named “id” in the form.

after modifying my code i also deeing this error.plz see it.

<?php


$name="";
$fathername="";
$mothername="";
$address="";
$birthday="";
$age="";
$email="";
$religion="";
$nationality="";
$gender="";



$servername = "localhost";
$username = "root";
$password = "";
$dbname = "registrationform";
//$id =$_REQUEST['id'];
// Create connection
$conn = new mysql($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


//$id =$_REQUEST['id'];
$result = mysql_query("SELECT * FROM registrationdb WHERE id='$id'");
$test = mysql_fetch_array($result);
if (!$result) 
		{
		die("Error: Data not found..");
		}
	$name=$test["name"];
	$fathername=$test["fathername"];
	$mothername=$test["mothername"];	
   	$address=$test["address"];
	$email=$test["email"];
	$birthday=$test["birthday"];
	$religion=$test["religion"];
    $nationality=$test["nationality"];
    $gender=$test["gender"];	
	
if(isset($_POST["update"])){	
	$name=$_POST["name"];
	$fathername=$_POST["fathername"];
	$mothername=$_POST["mothername"];	
   	$address=$_POST["address"];
	$email=$_POST["email"];
	$birthday=$_POST["birthday"];
	$religion=$_POST["religion"];
    $nationality=$_POST["nationality"];
    $gender=$_POST["gender"];	

	mysql_query("UPDATE registrationdb SET name ='$name', fathername ='$fathername',mothername ='$mothername',address ='$address',email ='$email',birthday ='$birthday',religion ='$religion',nationality ='$nationality',
		 gender ='$gender' WHERE id = '$id'")
				or die(mysql_error()); 
	echo "Saved!";
	
	header("Location: insert.php");			
}
mysql_close($conn);

?>
<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="raw">
<div class="col-md-8">
  <div class="panel panel-info">
  <div class="panel heading" class="panel panel-primary">Update data</div>
   <div class="panel-body" ></div>
  <form method="post" action="update.php"> 
    
	<div class="form-group">
      <label for="name">Name:</label>
      <input type="text" class="form-control" value=<?php echo $name;?>/>
    </div>
  
  
    <div class="form-group">
      <label for="fathername">Father Name:</label>
      <input type="text" class="form-control" name="fathername" value=<?php echo $fathername;?>>
    </div>
  
  
  
    <div class="form-group">
      <label for="mothername">Mother Name:</label>
      <input type="text" class="form-control" name="mothername" value=<?php echo $mothername;?>>
    </div>
  
    <div class="form-group">
      <label for="address">Address:</label>
      <input type="text" class="form-control" name="address" value=<?php echo $address;?>>
    </div>
  
  
    <div class="form-group">
      <label for="email">Email:</label>
      <input type="email" class="form-control" name="email" value=<?php echo $email;?>>
    </div>
  
  
  
    <div class="form-group">
      <label for="birthday">Birthday:</label>
      <input type="date" class="form-control" name="birthday" value=<?php echo $birthday;?>>
    </div>
  
  
  
  
  <div class="form-group">
    <label for="religion">Raligion:</label>
      <select class="form-control" name="religion" value=<?php echo $religion;?>>
        <option>Muslim</option>
        <option>Hindu</option>
        <option>Chiritian</option>
        <option>Buddha</option>
      </select>
	  </div>
	
	<div class="form-group">
    <label for="Nationalilty">Nationality:</label>
      <select class="form-control" name="nationality" value=<?php echo $nationality;?>>
        <option>Bangladeshi</option>
        <option>Indian</option>
        <option>Nepali</option>
        <option>Pakisthan</option>
      </select>
	  </div>
	
  
	
	<div class="form-group">
    <label for="gender">Gender:</label>
      <select class="form-control" name="gender" value=<?php echo $gender;?>>
        <option>Male</option>
        <option>Female</option>
        <option>Others</option>     
      </select>
	  </div>
	  <button type="submit" name="update" class="btn btn-primary">Update</button>
	  
	  
	  
 </body>
 
 
 
</html>

Fatal error: Class ‘mysql’ not found in E:\xampp\htdocs\update.php on line 23

Now you don’t even attempt to define $id, but still use it in the query.

Can you confirm the database name and table name are correct. It appears as though they are strangely named or should be reversed.

yes its correct.
database name:registrationform
table name:registrationdb

1 Like

That’s a product of this kind of course: https://www.kickstarter.com/projects/1311831077/the-full-stack-developer-bundle-learn-by-building

First they watch some video, then consider themselves developers, then they take some gig, then put in a code they got from a video, and then, when it doesn’t work, they just come to a forum, bringing the code in and asking you to fix it.

The issue here is not from id actually, as no absent $id can break this query. But from the struggle that the OP is having with database API.

And this situation, in my opinion, is a good example why this kind of questions is rotten. Given a blob of code, a human can spot a mistake or two, but if the author have no idea what are they doing at all, no complete answer is ever possible, unless you just sit, take their code, debug and rewrite it completely.

1 Like

try this… it might help

$query = “SELECT * FROM registrationdb WHERE id=‘$id’”;
echo $query;
$result = mysql_query($query);

if($result === FALSE) {
die(mysql_error(“error message for the user”));
}

while($row = mysql_fetch_array($result))
{
echo $row[‘name’];
}

It might, but it’s still using the old no-longer-supported mysql functions that the OP didn’t use in their other post. The error message about “class not found” is because they’re attempting to instantiate the ‘mysql’ class, a kind of mixture of that an mysqli code.

Please read this function’s description in the PHP manual. I am quite surprised to see such a wrong belief expressed here.
An empty result is a legitimate result all the same. You can always get the data from it.

Yes, you’re quite correct, it returns FALSE on error, rather than on no results. I figured I’d depart from the “don’t use the old functions” mantra and just try to answer the question, and it backfired spectacularly on me. So the fact the OP is connecting to the database with mysqli_connect() and then trying to run a connection with the old function is possibly more likely.

Well that’s the first time I actually agree with this person despite what he had said in the past.

@droopsnoot OP is actually not using mysqli_connect. They are using the OOP version - new mysqli. Preferably, it should never be mixed matched. Especially with the old mysql_ functions. I was actually thinking about creating a library that remapped all old mysql_ calls to the mysqli_. But the late @felgall in a PM said that it should of never happened in the first place with having mysql_ around. So I decided to trash it.

It is clear now that if he OP is seeing Cannot find 'mysql' class', they are using PHP 7. Since if that is the case, they need to update their code to a more modern style.

I failed to notice that mix up in the first set of code.

I know that gets really boring, like a broken record. But in this case I think it’s justified as the OP seems to be reverting back to mysql from mysqli. Presumably due to the failure to make the code work with mysqli, but as a result is making an even bigger mess.

OK, so are you saying that you can connect to the database using new mysqli but then use the old mysql_ functions on that same connection? If that’s the case, I think I have another post to go back and change somewhere. Has it ever been possible to initialise a mysql connection in that way, though? OP goes from “new mysqli()” in post #3 to “new mysql()” in post #6 where they get the “cannot find class” message.

My old signature used to point out that I come here more to learn than anything, so I’m trying to come across as interested rather than argumentative, hope it’s working.

I don’t recall much of it because it was a while back, but I did see an example some where on here. I believe @felgall was also on that post. We were talking about something and he pointed out that mix matching is possible, but not ideal. Or maybe it was the other way around. *Scratches head* Any who, I wouldn’t recommend mix matching anything because that in its own place is already bad practice.

I can test that theory out later when I get home. I can roll back to any version as I see fit so rolling back to PHP 2 wouldn’t be a problem. This is all in theory so after it is confirmed, I’ll roll back to PHP 7RC3.

I wasn’t aware that mixing and matching mysql and mysqli was possible either.
I think we can all agree that it should not be done and the OP must choose between the two and mysqli is the better option of the two (though I prefer PDO myself).

And then, $id should have a valid value assigned.

I suspect mixing mysql, mysqli and PDO would not work when within the same code sequence.

IIRC what was discussed was that it is possible to have different scripts use different API extensions to work with the same MySQL database.

That is, All of the obsolete mysql code does not need to be rewritten all at the same time (well, unless ver 7 happens to you), and can be done script by script.

2 Likes