Solved

PHP PDO

Posted on 2016-07-24
7
46 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 82

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

758 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

22 Experts available now in Live!

Get 1:1 Help Now