Solved

PHP PDO

Posted on 2016-07-24
7
53 Views
Last Modified: 2016-07-27
Would somebody give me a good example of an insert statement  using PDO?
0
Comment
Question by:rgranlund
7 Comments
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 41726668
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
 
LVL 7

Author Comment

by:rgranlund
ID: 41726670
Isn't there a "try" statement?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 41726712
Doesn't make any sense to me but here is the page on PDO Exceptions:  http://php.net/manual/en/class.pdoexception.php
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 41726830
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
 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 250 total points
ID: 41726885
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 41727538
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
 
LVL 7

Author Closing Comment

by:rgranlund
ID: 41731591
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question