Solved

PHP PDO

Posted on 2016-07-24
7
62 Views
Last Modified: 2016-07-27
Would somebody give me a good example of an insert statement  using PDO?
0
Comment
Question by:rgranlund
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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 53

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 110

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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 i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 dynamically set the form action using jQuery.

623 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