pdo

Using MySQL functions in PHP PDO prepared statements

你。 提交于 2019-12-25 03:59:19
问题 What's the right way of using a MySQL function while using PHP PDO? The function NOW() gets saved as a string instead of showing the time. $sth = $dbh->prepare("INSERT INTO pdo (namespace, count, teststring) VALUES (?, ?, ?)"); // these protect you from injection $sth->bindParam(1, $_a); $sth->bindParam(2, $_b); $sth->bindParam(3, $_c); $_a = 'Wishy-washy'; $_b = 123; $_c = 'NOW()'; // Doesn't work. Comes out as the string 'NOW()' (w/o the quotes) and not as a date 回答1: I would not pass

How to prevent duplicate inserts into a table

≡放荡痞女 提交于 2019-12-25 03:32:58
问题 I want to insert data into Mysql, and when the data is already in the table, it should not be inserted again. How to prevent the second insert? try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO MyGuests(firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')"; // use exec() because no results are returned $conn->exec($sql

MySQL PDO NOW() as assigned value - is it possible?

我与影子孤独终老i 提交于 2019-12-25 03:32:55
问题 I'm trying to pass a MySQL's NOW() function into PDO's assigned value through PHP and it's failing. I found that I must pass this directly into MySQL statement. But in my case, sometimes the datetime field can be empty. Is it even possible to pass NOW() as PHP's assigned value? Some code: I'm building query dynamically and the datetime is dependent on some other variable's value. if(isset($accountStatus)&&$accountStatus!=""){ $tmp[':account_status']=$accountStatus; if($accountStatus==0){ $tmp

IN() with string as parameter not working with PDO

谁说我不能喝 提交于 2019-12-25 03:26:34
问题 I'm trying to convert a mysql_query statement that had the following in it to PDO: $string = '2, 3, 4, 5, 7, 8'; mysql_query(" ... IN($string) ..."); This works fine with the mysql_query statement, but it will not work with PDO prepare and execute (no results are returned). What am I missing here? 回答1: If the query sql works fine with mysql_query , then it will work fine with pdo. What will not work is if you bind the whole string with just one placeholder. You need to bind for each values

PDO insert, foreach($_POST)

ぐ巨炮叔叔 提交于 2019-12-25 03:19:18
问题 My code below will not insert into my database. I do not know where my misstake is being made. (Thanks for the notifications regarding sql injections, will read about that laters <3) This is my php code so far: $sqlArray = array(); $nameArray = array(); $valueArray = array(); foreach($_POST as $name => $value) { //$sqlArray[] = "':".$name."'=>$".$name; $nameArray[] = $name; $valueArray[] = "'".$value."'"; } $names = implode(', ', $nameArray); $values = implode(', ', $valueArray); $sql =

Encrypting member data with keyfiles

独自空忆成欢 提交于 2019-12-25 03:17:16
问题 I am working on a website where I want members to be able to maintain a list of items in their account. In addition, they should be able to see/browse all items owned by others member (except without seeing any ownership information). I want to be able to offer some practical reassurance of security to members so that if they log into their account from a device that doesn’t have their keyfile, they will still be able to access and use their account but it will just be limited because it won

Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found

淺唱寂寞╮ 提交于 2019-12-25 03:14:18
问题 I tried to run the following code but it returned this erros: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column ''1'' in 'field list'' in /home/cardg/cards/jogar.php:59 Stack trace: #0 /home/cardg/cards/jogar.php(59): PDOStatement->execute() #1 {main} thrown in /home/cardg/cards/jogar.php on line 59 Why this is happening? <?php include('config.php'); $usuarion = $_SESSION['login']; $senhan = $_SESSION['senha']; // $attrs is

PDO queries works locally, not on server

老子叫甜甜 提交于 2019-12-25 03:12:15
问题 I'm executing an insert query to a MySQL database and the queries work perfectly fine on XAMPP locally, but not when I try the exact same code on the server. Here is the code: if($app['database']->insert('tickets', [ 'error' => $error, 'productid' => $productid, 'counterblack' => $counterblack, 'countercolor' => $countercolor, 'time' => $time, 'date' => $date, 'active' => 1 ])) { header("Location: ../Tasks.php"); } else { echo "Error"; } Insert function: public function insert($table,

Problems connecting to MySQL using PDO

折月煮酒 提交于 2019-12-25 03:12:04
问题 I've been trying to convert my application from using the depreciated mysql syntax to PDO for connecting to the database and performing queries, and it's been a pain so far. Right now I have a class, db_functions.php, in which I'm trying to create a PDO connection to the database, as well as perform all the CRUD operations inside of. Here is a sampling of the code: db_functions.php <?php class DB_Functions { private $db; // constructor function __construct() { require_once 'config.php'; //

mysql php pdo select a part of row value date record

点点圈 提交于 2019-12-25 02:59:43
问题 I want to select a part of a record in database. The record i want to select from is date. The record date is this format 07/02/2015 . I want to have a select option where i can put option of year and month to used as parameter for the search.I want to know a select query for ff. How can I select a specific year from the record(annual report) I want to select all record in database with the current year, or with the year 2014. How can i select a specific month from the record(monthly report)