I have 2 problems one not returning records and the bindValue not working.

this is the test code:

$config['db'] = array(
	'driver' => 'mysql', 
	'host' => 'localhost', 
	'dbname' => '***', 
	'username' => '***', 
	'password' => '***'
	);
function DB_ATRIBUTES($input) {
	$opt = array(
		/* any occurring errors wil be thrown as PDOException */
		//PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT,
		PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
		//PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
		/* an SQL command to execute when connecting */
		PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'",
		//PDO::ATTR_EMULATE_PREPARES => "false"
		);
	foreach ($opt as $key => $value) {
	echo'<hr>';
		echo $input->setAttribute($key, $value);
			echo'<hr>';
	}
}

$PDO_setting = $config['db']['driver'].':host='.$config['db']['host'].';dbname='.$config['db']['dbname'].'; '.$config['db']['username'].', '.$config['db']['password'].', ';

$their_ip_address = $_SERVER['REMOTE_ADDR'];
$their_host = gethostbyaddr($their_ip_address);
$their_user_agent = $_SERVER['HTTP_USER_AGENT'];
if (array_key_exists("HTTP_REFERER", $_SERVER)) {
	$their_referer = $_SERVER['HTTP_REFERER'];
} else {
	$their_referer = "No referer set";
}
/* See if they are in the database */
try {
	$database_handler = new PDO($PDO_setting);
	//DB_ATRIBUTES($database_handler);
	$stmt = $database_handler->prepare("
		SELECT * 
		FROM ip_blocker 
		WHERE ip = :their_ip_address 
		AND user_agent NOT LIKE '%google%' 
		AND user_agent NOT LIKE '%yahoo%' 
		AND user_agent NOT LIKE '%bing%' 
		AND user_agent NOT LIKE '%msn%' 
		AND user_agent not like '%slurp%'"
		);
		echo $their_ip_address;
	$stmt->bindValue(':their_ip_address', $their_ip_address, PDO::PARAM_INT);
	$stmt->execute();
	$rows = $stmt->fetchALL(PDO::FETCH_ASSOC);
	echo '<pre>'.print_r($stmt, true).'</pre>';
} catch(PDOException $e) {
	die("Execute query error, because: ". $e->getMessage());
}

exit;

I get this returned instead of the record.

127.0.0.1
PDOStatement Object
(
    [queryString] => 
		SELECT * 
		FROM ip_blocker 
		WHERE ip = :their_ip_address 
		AND user_agent NOT LIKE '%google%' 
		AND host NOT LIKE '%google%' 
		AND user_agent NOT LIKE '%yahoo%' 
		AND user_agent NOT LIKE '%bing%' 
		AND user_agent NOT LIKE '%msn%' 
		AND user_agent not like '%slurp%'
)


if I replace:
WHERE ip = :their_ip_address
with
WHERE ip = $their_ip_address

then I get this, but still no record returned.

127.0.0.1
PDOStatement Object
(
    [queryString] => 
		SELECT * 
		FROM ip_blocker 
		WHERE ip = 127.0.0.1 
		AND user_agent NOT LIKE '%google%' 
		AND host NOT LIKE '%google%' 
		AND user_agent NOT LIKE '%yahoo%' 
		AND user_agent NOT LIKE '%bing%' 
		AND user_agent NOT LIKE '%msn%' 
		AND user_agent not like '%slurp%'
)


I also tried PDO:😛ARAM_STR

Any Ideas why I'm not getting the bindValue, and not displaying the record, but I'm getting the [queryString] instead? it should be array() if it is empty.

trying to learn PDO...

Thanks,
Bob

    rbrown56 wrote:

    I get this returned instead of the record.

    That's because that's what you're asking for:

        $stmt->execute();
        $rows = $stmt->fetchALL(PDO::FETCH_ASSOC);
        echo '<pre>'.print_r($stmt, true).'</pre>'; // Don't you mean $rows?
    

    (And, just to be picky, you're not [man]return[/man]ing anything, just printing stuff for debugging.)

      oops...
      Duh...
      I was getting tired...
      Trying to follow a video tutorial and transpose it to what I was trying to do...
      Changed it and it's working...
      Thanks...

        Write a Reply...