I have crateio set up and it's working fine using the PDO class.
I'm, trying to get a set of geopoints into the db using binds.
I have tried foreach but doesn't seem to work, I've tried this - which also doesn't work.
The geopoint column is set to geo_point_array.
$route="[[30.33333, -6.13336],[30.33333, -6.13336]]";
$db = new Database;
$db->Query("insert into geopoints (id, longlat, name) values ('33',?,'pat')");
$db->bind(1, $route);
$db->execute();
How do I add this set of cordinates to the db?
Thanks
GeoPoint is not supported as a native type in Crate's PDO driver yet, however you can use an double ARRAY.
From the Crate documentation:
Columns with the geo_point are represented and inserted using a double
array in the following format: [lon_value, lat_value]
I also strongly recommend to do parameter substitution for the other values.
use Crate\PDO\PDO;
$route = [[30.33333, -6.13336], [30.33333, -6.13336]];
$db = new PDO('crate:...');
$stmt = $db->query("insert into geopoints (id, longlat, name) values (?, ?, ?)");
$stmt->bind(1, 33, PDO::PARAM_INT);
$stmt->bind(2, $route, PDO::PARAM_ARRAY);
$stmt->bind(3, 'pat', PDO::PARAM_STR);
$stmt->execute();
PDO::query returns PDOStatement:
$route="[[30.33333, -6.13336],[30.33333, -6.13336]]";
//If Dateabase is a sublcass of PDO
//$db = new Database;
$db = new PDO(...);
$stmt = $db->query("insert into geopoints (id, longlat, name) values ('33',?,'pat')");
$stmt->bind(1, $route, PDO::PARAM_STR);
$stmt->execute();
Related
i'm trying to INSERT a sql query but it does not work - I got no errors, $pdo->errorInfo(); only returns Array and in the mysql is nothing to see!
Im 100% sure that $text, $file and $title is set (i've check that with echo) In every other php file this pdo connection works with include but not in the dev.php what should i do???
datenbank.php
<?php
$pdo = new PDO('mysql:host=localhost;dbname=db', 'user', 'password');
?>
dev.php
include("datenbank.php");
// Prepare an insert statement
$post = $pdo->prepare("INSERT INTO news (text, date, file, title) VALUES ($text, NOW(), $file, $title)");
$post->execute();
$help = $pdo->errorInfo();
You don't use the parameters markers in your prepare PDO stament. When you prepare a query using PDO extension, you need put markers in your query statement and indicate the value of those markers in the execute function like an associative array.
You can use markers like :marker or question marks ? and your query would be like that:
include("datenbank.php");
// Prepare an insert statement with marks params
$post = $pdo->prepare(INSERT INTO news (text, date, file, title) VALUES (:text, NOW(), :file, :title));
//execute statements with the marks values in prapare function params
$post->execute(array(':text' => $text, ':file' => $file, ':title' => $title));
Edit: PD: This prevents the SQL inyection.......
for string value you need quote
$post = $pdo->prepare("INSERT INTO news (text, date, file, title)
VALUES ('$text', NOW(),'$file', '$title')");
anyway you should not use php var in sql , you are at risk for sqlinjection .. use prepared statements and binding param instead
$stmt = $conn->prepare("INSERT INTO news (text, date, file, title)
VALUES (:text, NOW(), :file, :title)");
$stmt->bindParam(':text', $text);
$stmt->bindParam(':file', $file);
$stmt->bindParam(':title', $title);
$stmt->execute();
I am preparing mysql configuration settings using class. I am confused about it. I always use bindParam. It also possible to insert using array. I mean, what are the differences between array and bindparam.
eg array
$query = $db->prepare("INSERT INTO users SET
username = :uname,
password = :upass,
email = :umail");
$insert = $query->execute(array(
"upass" => "123456",
"umail" => "user#user.com",
"uname" => "username",
));
if ( $insert ){
$last_id = $db->lastInsertId();
}
eg
$stmt = $this -> db_conn -> prepare("INSERT into users(username, password) VALUES(:uname, :upass)");
$stmt -> bindParam(':uname', $username);
$stmt -> bindParam(':upass', $password);
$stmt -> execute();
Desconsidering the fact that with execute you can't choose the data type (it's always PDO::PARAM_STR) There's only a main difference between both (which is from core). PDOStatement::bindParam is by reference while PDOStatement::execute isn't. PDOStatement::execute do internnaly the same thing as PDOStatement::bindValue, but twice.
Internally (in C), bindValue calls the same method which execute calls. The method name which is called is really_register_bound_param.
On the other hand, bindParam calls other method called register_bound_param.
It means that bindValue calls the same method called by execute more than one time while bindParam calls a method to bind as a reference and only "really register" the param when execute is called.
Thinking about bind by reference, it's only possible using bindParam:
//fictional code
$stmt= $pdo->prepare("INSERT INTO table (column) VALUES (:value)");
$stmt->bindParam(":value", $randomValue);
for($i = 0 ; $i < 1000; $i))
{ $randomValue = rand(1000,1000000);
$stmt->execute();
}
Is it worthy? Perhaps while a while with a complex insert with multiples parameters could reduce the overhead with rebinding a new parameter or a big amount of them.
I have something like this:
$stmt = $this->getDoctrine()->getConnection()->prepare(
'insert into someTable (columnList) values (parameters);');
/*
bind parameters
*/
$stmt->execute();
How do I get the last inserted ID?
Thanks,
Scott
You'll want to use lastInsertId().
Example:
$dbh = Doctrine_Manager::getInstance()->getCurrentConnection();
$sth = $dbh->prepare("INSERT INTO continent (created_at, updated_at) VALUES ( NOW(), NOW() )");
$sth->execute();
$conn = Doctrine_Manager::getInstance()->getCurrentConnection();
$conn->lastInsertId('continent_id');
The code below answers the question correctly:
//This call will get the doctrine connection from inside a symfony2 controller
$conn = $this->getDoctrine()->getConnection();
$stmt = $conn->prepare(
'insert into someTable (columnList) values (parameters);');
/*
bind parameters
*/
$stmt->execute();
$id = $conn->lastInsertId();
To get the connection from inside a doctrine repository class that you have created:
$conn = $this->getEntityManager()->getConnection();
The current error when running this from the command line is "Call to a member function bindParam() on a non-object" which I've worked out to being a problem with the variable $orderPO. Something does not like non-numeric characters which led me to the bindParam PARAM_STR business which does not work either. The database fields are both varchar 50.
My search skills are failing me. I know this must be posted somewhere about a million times but I can't seem to find it. I am completely open to doing this another way if someone has a better idea.
Current attempt code:
try
{
$orderNum = '123456';
$orderPO = '123456-A';
$dbh = new PDO("mysql:host=localhost;dbname=dbname", 'someuser', 'somepass');
$stm = $dbh->prepare("insert into some_table (order_number, order_po)");
$stm->bindParam(':order_number', $orderNum, PDO::PARAM_STR);
$stm->bindParam(':order_po', $orderPO, PDO::PARAM_STR);
$stm->execute();
print_r($stm);
print_r($dbh);
$arr = $stm->errorInfo();
print_r($arr);
$stm->closeCursor();
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
In order to bind parameters using PDO, you will need to use placeholders, like this:
$stm = $dbh->prepare("
INSERT INTO `some_table` SET
`order_number` = :order_number,
`order_po` = :order_po
");
$stm->bindParam(':order_number', $orderNum, PDO::PARAM_STR);
$stm->bindParam(':order_po', $orderPO, PDO::PARAM_STR);
Notice the inclusion of the : character before the named placeholder. I also added column names to your query.
Read further and see examples: PDO bindParam
The correct syntax is
$stm = $dbh->prepare("insert into some_table (order_number, order_po) VALUES (?, ?)");
$stm->bindParam(1,$orderNum);
$stm->bindParam(2,$orderPO);
include the questions marks, the numbers in the bindParam call refer to which question mark you're binding the parameter to
You are trying to use bindparam, but bind param matches ? not cursors :. You have not included any parameters or values.
Also, you are missing your VALUES statement within the query, which is causing the query to fail. This is why you get the "Call to a member function bindParam() on a non-object"
To use the :value syntax, use bindValue, not bindParam. to use bindParam, switch the :value to ? in your query and number them in order is your execute array.
try
{
$orderNum = '123456';
$orderPO = '123456-A';
$dbh = new PDO("mysql:host=localhost;dbname=dbname", 'someuser', 'somepass');
$stm = $dbh->prepare("insert into some_table (order_number, order_po) VALUES (:order_number, :order_po)");
$stm->bindvalue(':order_number', $orderNum, PDO::PARAM_STR);
$stm->bindvalue(':order_po', $orderPO, PDO::PARAM_STR);
$stm->execute();
print_r($stm);
print_r($dbh);
$arr = $stm->errorInfo();
print_r($arr);
$stm->closeCursor();
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
I am using named placeholders like so:
$job['services_flag'] = 0;
$SQL = "INSERT INTO jobs (
services_flag
)
VALUES (
:services_flag
)";
$STH = $DBH->prepare($SQL);
$STH->execute($job);
However, this insists on inserting 1.
If I don't use named placeholders:
$SQL = "INSERT INTO jobs (
services_flag
)
VALUES (
0
)";
$STH = $DBH->prepare($SQL);
$STH->execute();
Then it inserts 0. Eh?
Update:
The data type of the services_flag field in my SQL database is BIT. I don't know if that makes any difference.
This is a known bug with BIT type, you should use bindParam for it
You need to bind a value to it:
$STH->bindValue(':services_flag', $job['services_flag']);
$STH->execute();
That's one way to do it. I've seen an array with values mapped to placeholders passed in the execute() as well.