A comment quote from the op (the last comment at the moment):
The primary problem is, it will not delete. even though the sql
statment is correct when tested directly in phpmyadmin. the secondary
problem is, it will also say true. even though it didn't delete.
Machavity solved the secondary problem. the primary remains. -
codenoob
TLDR: // Note: PDOStatement::execute .... Returns TRUE on success or FALSE on failure.
So that is why it returns 1 all the time for you. See the below for 2 files to test this with a change using rowCount()
off of a PDOStatement object.
You said in comments that the primary problem remains. I have no problem using the below after sort of hand crafting a database class, because you did not offer one (you mention one). Note the use of try/catch blocks.
Frankly we don't know if you had any exceptions or how you are handling them, or whether or not you have error reporting activated. The below should survive any testing, assuming the pdo object can successfully return the rowCount()
value.
Schema for testing:
create table product_area
( product_area_id int primary key,
theName varchar(100) not null
);
-- blockA begin
truncate product_area;
insert product_area (product_area_id,theName) values
(1,'Houston zone B'),(2,'Houston zone BH'),(20,'Houston zone Z');
-- blockA end
Test File:
For the test file, there are only a few lines of code near the top for testing in the section called "Mini test area"
<?php
// pdo_del_test_20160703.php
// related to answering http://stackoverflow.com/questions/38061597 at the moment
error_reporting(E_ALL);
ini_set("display_errors", 1);
include "myPDO_DB.php"; // Database class. The class name = myPDO_DB
// Mini test area BEGIN:
$a1=new aClass();
$ret=$a1->deleteArea(2);
echo "retValue=".$ret."<br>";
// Mini test area ... END
class aClass {
private $db=null;
public function __construct(){
echo "in constructor1<br>";
$this->db=new myPDO_DB();
echo "in constructor2<br>";
//$this->db=null;
echo "in constructor3<br>";
}
public function deleteArea($product_area_id){
$this->db->query("
DELETE
FROM product_area
WHERE product_area_id = :product_area_id
LIMIT 1
");
// Note: PDOStatement::execute .... Returns TRUE on success or FALSE on failure.
//
// so on a Delete call, it just says sure, OK, done with that (if no exception)
// It doesn't give feedback natively as to whether or not a row was actually deleted
//
//bind
$this->db->bind(':product_area_id', $product_area_id);
//Execute
// Don't forget to run schema `blockA` before testing (so that data is there)
if ($this->db->execute()) { // if this function returns anything other than TRUE you have exception problems
$pdo_rowcount=$this->db->stmt->rowCount(); // see http://php.net/manual/en/pdostatement.rowcount.php
// Depending on your systems ability to get pdo.rowCount() to work, consider the following
// Please see http://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_row-count
$this->db->query("select row_count()"); // from mysql "Information Functions". Based on prior mysql call.
$mysql_rowcount=$this->db->execute();
// but note, row_count() in mysql is not exactly a solution for the majority of systems out there.
// http://www.heidisql.com/forum.php?t=9791
// on mysql 5.5 and my 5.6.24, it returns 0
// so depending on your system, you will choose between $pdo_rowcount and $mysql_rowcount
return $pdo_rowcount; // your best bet, bust test it on your setup.
}
else {
// do something
return 0; // you would have already endured an exception though
}
}
}
?>
Database class:
<?php
// myPDO_DB.php
//
error_reporting(E_ALL);
ini_set("display_errors", 1);
include "db_connect_info.php"; // brings in the "defines" .. Shoot for a secure o/s vault in this file.
class myPDO_DB {
// will grab the stub from http://culttt.com/2012/10/01/roll-your-own-pdo-php-class/
//
// and then build your class into it (because you did not provide it)
//
// and then further improve it with try/catch blocks that were lacking
//
private $host = DB_HOST; // these were brought in with the include above. File not shown.
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $dbh;
public $stmt; // was made public to get into rowCount(); .... change this for your needs
private $error;
public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false
);
// Create a new PDO instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
echo "Connect Ok<br>";
}
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
public function bind($param, $value, $type = null) {
try {
if (is_null ( $type )) {
switch (true) {
case is_int ( $value ) :
$type = PDO::PARAM_INT;
break;
case is_bool ( $value ) :
$type = PDO::PARAM_BOOL;
break;
case is_null ( $value ) :
$type = PDO::PARAM_NULL;
break;
default :
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue ( $param, $value, $type );
}
catch(PDOException $e){
$this->error = $e->getMessage();
throw $e;
}
}
public function query($query) {
try {
$this->stmt = $this->dbh->prepare($query);
}
catch(PDOException $e){
$this->error = $e->getMessage();
throw $e;
}
}
public function execute(){
try {
return $this->stmt->execute();
}
catch(PDOException $e){
$this->error = $e->getMessage();
throw $e;
}
}
}