php help please

hello i am in need of some dire php help and i was wondering what was wrong with my code? at first it didnt show the middle statement then later i got some Fatal error: Call to a member function fetch() on a non-object? i dont know what could be wrong

[php]<?php
function displayRequired($fieldName) {
echo “Pls fill in “$fieldName”.
\n”;
}

function validateInput($data, $fieldName) {
global $errorCount;
if (empty($data)) {
displayRequired($fieldName);
++$errorCount;
$retval = “”;
}
else {
$retval = trim($data);
$retval = stripslashes($retval);
}
return($retval);
}
function validateDB($id, $password){
$dbh=new PDO(‘mysql:host=localhost;dbname=db1’,‘root’,‘student’);
$sql= “SELECT * FROM members WHERE id=’$id’ AND pw= ‘$password’”;
$result = $dbh->query($sql);
$row = $result->fetch();
if (!empty($row)){
return true;
}
else{
return false;
}
}

function redisplayForm($id, $password) {
$id=$_COOKIE[‘id’];
if ($id==null){$id="";}
?>

<form action = "6-3aa.php" method="post">
ID:<input type="text" name="id" value="<?php echo $id; ?>" /><br />
Password: <input type="password" name="pwd" value="<?php echo $password; ?>" /><br />
Remember Me:<input type="checkbox" name="rm" value="rm" /><br />
<input type="submit" name="Submit"value="Login" />
</form>
<?php

}

if(isset($_POST[“Submit”])){
$errorCount=0;
$id = validateInput($_POST[‘id’], “User name”);
$password = validateInput($_POST[‘pwd’], “Password”);

if($errorCount > 0){
	echo "Please re-enter the information";
	redisplayform($id, $password );
}else{
	validateDB($id, $password);
	if($_POST['rm']){
	setcookie('id', $id, time()+3600); 
	   echo "Login Valid and Cookie Set. <a href='6-3aa.php'>Back to 	login page</a>";

	}elseif(validateDB($id, $password) && !$_POST['login']){
		echo "Login Valid and No Cookie Saved <a href='6-3aa.php'>Back to login page</a>";

	}else{
	      echo "Login Failed and No cookie. <a href='6-3aa.php'>Back to login page</a>";

	}

}

}
else {
redisplayform($id, $password);
}
?>[/php]

PDO::query will return false on failure. Which means if $result = false then $result is not an object.

See: http://php.net/manual/en/pdo.query.php

Try enclosing with a try/catch and see if you get an error:

[php]
function validateDB($id, $password){
$dbh=new PDO(‘mysql:host=localhost;dbname=db1’,‘root’,‘student’);
$sql= “SELECT * FROM members WHERE id=’$id’ AND pw= ‘$password’”;

try {
	$result = $dbh->query($sql);
	$row = $result->fetch();
	if (!empty($row)){
		return true;
	}
	return false;
}
catch(PDOException $e) {
	exit($e->getMessage());
}

}
[/php]

hmm thanks but i still got that fetch error i seriously going crazy about this haha

Let’s try this. Move the try above the connection to catch any exceptions there. Then, instead of using fetch (since it doesn’t appear you need to actually fetch the row), we can try rowCount

[php]
function validateDB($id, $password){
try {
$dbh=new PDO(‘mysql:host=localhost;dbname=db1’,‘root’,‘student’);
$sql= “SELECT * FROM members WHERE id=’$id’ AND pw= ‘$password’”;

	$result = $dbh->query($sql);
	if ($result->rowCount() > 0) {
		return true;
	}
	return false;
}
catch(PDOException $e) {
	exit($e->getMessage());
}

}[/php]

OR, you can simply check to see if $result = false

[php]
function validateDB($id, $password){
try {
$dbh=new PDO(‘mysql:host=localhost;dbname=db1’,‘root’,‘student’);
$sql= “SELECT * FROM members WHERE id=’$id’ AND pw= ‘$password’”;

	$result = $dbh->query($sql);
	if ($result) {
		return true;
	}
	return false;
}
catch(PDOException $e) {
	exit($e->getMessage());
}

}
[/php]

well it gives me Fatal error: Call to a member function rowCount() on a non-object in /var/www/6-3a.php on line 25

Hmm… yes that would also happen if $result = false. If you try my second option does it ever return true?

thank you i no longer have that fetch error but my orginal thing still exists where i cant get the second echo to show
which is [php]echo “Login Valid and No Cookie Saved Back to login page”;[/php]

You still have to deal with the first problem. If $result is returning false then something is wrong. Even an empty result would not return false.

Unfortunately if you are not getting any exceptions then I have no idea what the problem could be. You should double check that your localhost has PDO enabled and that error_reporting = E_ALL

i dont know how but thanks for your help man youve helped alot

What are you using to run your localhost? Is this on a server or on your personal computer?

Basically, in your php.ini file you would look for the following line:

;extension=php_pdo_mysql.dll

If it has the ; before it, you want to remove it and restart the server.

its on a virtual machine running ubuntu on opera browser

See my edit. Also, in the php.ini file you want to search for a line like this

error_reporting = E_ALL

If you do not have access to this file, you can try this. Create a new file called phpinfo.php and paste this inside and run it.

[php]<?php phpinfo(); ?>[/php]

If you have access, it should tell you if PDO is installed. You will see a box like this:

PDO
PDO support enabled
PDO drivers sqlite, mysql, sqlite2

sorry gotta go thanks for your help tho

Sponsor our Newsletter | Privacy Policy | Terms of Service