Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 67
  • Last Modified:

PHP PDO

Would somebody give me a good example of an insert statement  using PDO?
0
rgranlund
Asked:
rgranlund
2 Solutions
 
Olaf DoschkeSoftware DeveloperCommented:
Would this help?
http://www.mustbebuilt.co.uk/php/insert-update-and-delete-with-pdo/

Or what other aspect of PDO is uncelar to you?
You don't have to use prepared statements, but it's a big advantage to do that.

Bye, Olaf.
0
 
rgranlundAuthor Commented:
Isn't there a "try" statement?
0
 
Dave BaldwinFixer of ProblemsCommented:
Doesn't make any sense to me but here is the page on PDO Exceptions:  http://php.net/manual/en/class.pdoexception.php
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Olaf DoschkeSoftware DeveloperCommented:
Well, try/catch only is necessary, if you run PDO in the PDO::ERRMODE_EXCEPTION mode. Default should be PDO::ERRMODE_SILENT: Just set error codes, so you only check, whether the PDOStatement::execute() returns FALSE and in that case check out PDO::errorCode and PDO::errorInfo.

If you have an error in your query, that'll happen in MySQL or whatever backend. It's your concern, certainly, but the stability of PHP is not in any danger, you just don't have a query result and should handle that. So since the error is happening in another process most likely even on a remote database server, it's not necessary to let that cause an exception in PHP.

It's a bit of taste, if your general error handling is based on exceptions, it's a simple setting and if you don't catch the exception at this level, it'll bubble up to the next higher level.

Bye, Olaf.
0
 
Scott Fell, EE MVEDeveloperCommented:
As noted, there are a lot of options. My example uses sql server but with pdo you can use essentially the same code for any db.  

if (!$db = new PDO("sqlsrv:Server=localhost;Database=$db_name", $db_user , $db_pass))
{
    die("No Database");
}

	$firstName = "John";
	$lastName -= "Smith";
	$total = 5;

    $stmt = $db->prepare('INSERT INTO dbo.myTable (FirstName, LastName, Total)

    VALUES(:first_name,:last_name,:total)');
    $stmt->bindValue(':first_name', $firstName, PDO::PARAM_STR);
    $stmt->bindValue(':last_name',   $lastName,  PDO::PARAM_STR);
    $stmt->bindValue(':total',    $total,  PDO::PARAM_INT);
   
    $stmt->execute();
	
	$arr = $stmt->errorInfo();
    print_r($arr);
	
	$stmt = null;

Open in new window


http://php.net/manual/en/book.pdo.php

What will make this a better thread is if your question was more specific as to what you are working on or what issues you are having.  But if you are looking for just a general idea, you have good answers above.
0
 
Ray PaseurCommented:
This is a common question, so we have good reference material available.

Bookmark (and read) these pages:
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
https://iconoun.com/mysql_mysqli_pdo_function_map.php
0
 
rgranlundAuthor Commented:
Thanks Ray.  I read that page a while back and at the time most of it went over my head.  This time around it really cleared a few this up.  Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now