I can't find my mistake, I'm getting Execute fail error
$db = new PDO('mysql:host=localhost; dbname=xxxxxx', 'yyyyyy', 'zzzzzz', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")) or die ("fail");;
$query = "INSERT INTO multiTicker (mtgox,btcstamp,btce,btcchina,myDateTime) VALUES (:mtgox,:btcstamp,:btce,:btcchina,:myDateTime)";
$st = $db->prepare($query) or die ("Query fail");
$st->execute(array(':mtgox' => $mtgox,
':btcstamp' => $btcstamp,
':btce' => $btce,
':btcchina' => $btcchina,
':myDateTime' => $myDateTime)) or die ("Execute fail");
Looks like your DSN is incorrect (you have a space in it). Try this PDO constructor and stop using or die()!
$db = new PDO('mysql:host=localhost;dbname=xxxxxx;charset=utf8', 'yyyyyy', 'zzzzzz', array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
$query = "INSERT INTO multiTicker (mtgox,btcstamp,btce,btcchina,myDateTime) VALUES (:mtgox,:btcstamp,:btce,:btcchina,:myDateTime)";
$st = $db->prepare($query);
$st->execute(array(
':mtgox' => $mtgox,
':btcstamp' => $btcstamp,
':btce' => $btce,
':btcchina' => $btcchina,
':myDateTime' => $myDateTime
));
You don't have to set the default fetch mode to PDO::FETCH_ASSOC but I find it's handy.
Related
This is a test snippet from my application:
<?php
$dsn = "mysql:host=localhost;dbname=".$dbName.";charset=utf8mb4";
$options =
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::ATTR_AUTOCOMMIT => false,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_EMPTY_STRING
];
$conn = new PDO($dsn, $dbUser, $dbPass, $options);
var_dump($conn->inTransaction());
$sql = "SELECT
documentTypeID
FROM documentTypes";
$statement = $conn->prepare($sql);
var_dump($conn->inTransaction());
$result = $statement->execute();
var_dump($conn->inTransaction());
Output:
bool(false)
bool(false)
bool(true)
Why is the third var_dump true? I didn't intend to start a transaction. My understanding of transactions is that SELECT statements don't need or affect them. What am I missing?
After this happens in my app, I try to beginTransaction(), which fails because one has already started. Checking to see if inTransaction() is true before I begin one seems sloppy, but if that's how this is supposed to work, I guess I'll do it.
I have a form that has a hidden field (the date). I want this data to be inserted with the note that is submitted as well. I only have 3 columns in my table. ID,Note_id and Note.
So if a user submits a note the insert statement should add the date infront of the note.
My Current SQL Statement:
$pdoOptions = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false
);
$pdo = new PDO("mysql:host=" . MYSQL_HOST . ";dbname=" . MYSQL_DATABASE, MYSQL_USER,MYSQL_PASSWORD,$pdoOptions);
$data = [
'notes' => $_POST['notes'],
'id' => $_POST['rowID'],
'date2' => $_POST['date'],
];
$sql = "INSERT INTO notes (note_id, note) Values(:id, :notes)";
$stmt= $pdo->prepare($sql);
$stmt->execute($data);
My thought was I could add the :date2 to this but it does not seem to work
$pdoOptions = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false
);
$pdo = new PDO("mysql:host=" . MYSQL_HOST . ";dbname=" . MYSQL_DATABASE, MYSQL_USER,MYSQL_PASSWORD,$pdoOptions);
$data = [
'notes' => $_POST['notes'],
'id' => $_POST['rowID'],
'date2' => $_POST['date'],
];
$sql = "INSERT INTO notes (note_id, note) Values(:id, :date '-' :notes)";
$stmt= $pdo->prepare($sql);
$stmt->execute($data);
Whats the best method of doing this? I know I can have another column but I dont want that structure. The way it is displayed on the users side needs to be like this.
Thank you
Use CONCAT()
$sql = "INSERT INTO notes (note_id, note) Values(:id, CONCAT(:date,'-',:notes))";
This will work
This is an example of the database connection I'm using with my PDO queries:
$dsn = "mysql:host=localhost;dbname=some_db;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn,'Username','Password', $opt);
How would I modify it if my database is located on www.mysite.com and I want to access the database from a different website?
You need to open up port 3306 to accept connections.
$dsn = "mysql:host=mysite.com;dbname=some_db;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn,'Username','Password', $opt);
I cannot delete database from raw MySql query here. Is there a better way? Please suggest. Is there a way to write a query to drop database in Zend. I'm using Zend 1.11
// Delete db function
public function deleteDB($dbName){
// For database connection
$config = new Zend_Config(
array(
'database' => array(
'adapter' => 'PDO_MYSQL',
'params' => array(
'host' => 'localhost',
'dbname' => $dbName,
'username' => 'root',
'password' => '',
)
)
)
);
$db = Zend_Db::factory($config->database);
//Delete database
$sql = 'DROP DATABASE'. $dbName;
$db->query($sql);
If your configuration is correct then try to put space after DATABASE like
//Delete database
$sql = "DROP DATABASE `". $dbName."`";
$sql = 'DROP DATABASE'. $dbName;
I think you missed a space
$sql = 'DROP DATABASE '. $dbName;
This is an example of the database connection I'm using with my PDO queries:
$dsn = "mysql:host=localhost;dbname=some_db;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn,'Username','Password', $opt);
How would I modify it if my database is located on www.mysite.com and I want to access the database from a different website?
You need to open up port 3306 to accept connections.
$dsn = "mysql:host=mysite.com;dbname=some_db;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn,'Username','Password', $opt);