Solved

PHP PDO

Posted on 2016-07-24
7
48 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now