public function insert($tablename, $datafields = array(), $tablefields = array()) { //insert data $dbconfig = new DBconfig(); $host = $dbconfig->host(); $user = $dbconfig->username(); $pwd = $dbconfig->pwd(); $db = $dbconfig->db(); $datafld = DBmain::arraytostr2($datafields); //convert array to a single string separate with "," $tablefld = DBmain::arraytostr($tablefields); try { $conn = new PDO("mysql:host={$host};dbname={$db}", $user, $pwd); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO " . $tablename . " (" . $tablefld . ") VALUES (" . $datafld . ")"; // use exec() because no results are returned $conn->exec($sql); return true; } catch (PDOException $e) { echo $sql . "<br>" . $e->getMessage(); return false; } $conn = null; }
public function selectDataCommen($query) { //select Data $selectrowsstr = DBmain::arraytostr($selectrows); try { $stmt = $this->conn->prepare($query); $stmt->execute(); // set the resulting array to associative //$result = $stmt->setFetchMode(PDO::FETCH_ASSOC); $arr = array(); //create outer array if ($stmt->rowCount() > 0) { $count = 0; while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $arr[$count] = $row; //inner associative array ($row) is emberd to outer array $count++; } } return $arr; } catch (PDOException $e) { echo "Error: " . $e->getMessage(); } $conn = null; }