$name = 'root'; # user-supplied data
try {
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
#note: mysql:host=localhost(replace it by your hostname) , mysql:dbname:myDatabase (replace it by your database name), insert username and password for database at the end of this line
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data = $conn->query('SELECT * FROM myTable WHERE name = ' . $conn->quote($name));
foreach($data as $row) {
print_r($row);
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
Lets learn together .... feel free to participate and comment and provide your feedback .... Happy Coding
nirajanghimireyworkshop
Showing posts with label PDO. Show all posts
Showing posts with label PDO. Show all posts
Wednesday, December 24, 2014
MySQL Database Connection using PDO
Tuesday, December 23, 2014
PDO in PHP
PHP Data Object is what PDO stands for, PDO defines a lightweight, consistent interface that is used for accessing database in PHP. No data functions can be performed or brought in use only using PDO but a database specific PDO driver should be used instead to access a database server. PDO is latest and it is only available after PHP 5.1 version. PDO makes it easier and convenient and provides a data-access abstraction layer this helps using multiple database with same function query to fetch the data.
Databases supported by PDO :
Driver name
|
Supported databases
|
PDO_CUBRID
|
Cubrid
|
PDO_DBLIB
|
FreeTDS / Microsoft SQL Server / Sybase
|
PDO_FIREBIRD
|
Firebird
|
PDO_IBM
|
IBM DB2
|
PDO_INFORMIX
|
IBM Informix Dynamic Server
|
PDO_MYSQL
|
MySQL 3.x/4.x/5.x
|
PDO_OCI
|
Oracle Call Interface
|
PDO_ODBC
|
ODBC v3 (IBM DB2, unixODBC and win32 ODBC)
|
PDO_PGSQL
|
PostgreSQL
|
PDO_SQLITE
|
SQLite 3 and SQLite 2
|
PDO_SQLSRV
|
Microsoft SQL Server / SQL Azure
|
PDO_4D
|
4D
|
Example:
PHP Code(connection.php)
<?php
$dsn = 'mysql:dbname=ft;host=localhost;port=3306';
$username = 'root';
$password = '';
try {
$db = new PDO($dsn, $username, $password);
if ($db) {
echo "Successfully connected to database.";
}
} catch (PDOException $e) {
echo "Could not connect with MySql:" . $e->getMessage();
}
?>
PHP PDO Insert:
PHP Script (insert.php):
<?php
$dsn = 'mysql:dbname=try;host=localhost;port=3306';
$username = 'root';
$password = '';
try {
$db = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
echo "Could not connect with MySql:" . $e->getMessage();
}
//PDO Class
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
//PDO Statement Class
$sth = $db->prepare("INSERT INTO test(id, message) VALUES (:id, :mgs)");
$data = array(
':id' => 'first',
':msg' => 'this is first message'
);
echo $sth->execute($data);
?>
File to get or fetch records ( PHP PDO getting record):
PHP Script (records.php)
<?php
$dsn = 'mysql:dbname=try;host=localhost;port=3306';
$username = 'root';
$password = '';
try {
$db = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
echo "Could not connect with MySql:" . $e->getMessage();
}
//PDO Class
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
//PDO Statement Class
$sth = $db->query("SELECT * FROM test");
print_r($sth->fetch());
?>
Tuesday, September 23, 2014
Starting with Web Development
In today’s world internet has been so vital part of people’s daily life, such as web development has traveled a milestone and has become one of the most in-demand and professional skills.Companies around world are looking for web developers and programmers to build and maintain websites and web driven applications to meet their requirements and wants.
Web development can be learn without going to college or acquiring a college degree, all you need is a little courage to get involved and resources to get through. Resources for web development can be found over internet excessively and are even free to use and test these days. There are great free resources to begin within internet and I have tried to gather some of the best and easiest one that would guide you to be a newbie to beginner level web developer.
Though before going through these resources and logic you need to have set your mind up to learn and excellence the development part. While talking more about web development it’s of course necessary be get known to some of the programming languages used widely in web developmental tasks. Now learning how to code is as simple as learning a new language.
For tutorial purpose and starting purpose I would recommend to learn HTML/HTML5 for beginning with web development career. After getting a rookie in HTML5/HTML you need to get involved into the design stuff and go along with CSS, CSS3 would be the best for starting! You can and later on turn to CSS3 and Bootstrap.
This reading and study must not take more than 3 weeks, by three weeks you will be a rookie in HTML5/HTML and CSS/CSS3 or Bootstrap. Now, you can get yourself included in PHP. As said before PHP is the best and simplest programming language. It is easy to write and understand.
For the readers who already know about basics HTML and CSS may directly include themselves in PHP and start learn PHP. So far I searched internet and found some useful sites for study purpose and tutorial videos, I shall be listing them in end to my article.To expand this article and make you all have a better start and end i will write more on some practical topics like HTML5,Bootstrap and PHP, stay tuned. I shall be available soon with my next article by the end of this week.
Links for starting with php :
Web development can be learn without going to college or acquiring a college degree, all you need is a little courage to get involved and resources to get through. Resources for web development can be found over internet excessively and are even free to use and test these days. There are great free resources to begin within internet and I have tried to gather some of the best and easiest one that would guide you to be a newbie to beginner level web developer.
Though before going through these resources and logic you need to have set your mind up to learn and excellence the development part. While talking more about web development it’s of course necessary be get known to some of the programming languages used widely in web developmental tasks. Now learning how to code is as simple as learning a new language.
For tutorial purpose and starting purpose I would recommend to learn HTML/HTML5 for beginning with web development career. After getting a rookie in HTML5/HTML you need to get involved into the design stuff and go along with CSS, CSS3 would be the best for starting! You can and later on turn to CSS3 and Bootstrap.
This reading and study must not take more than 3 weeks, by three weeks you will be a rookie in HTML5/HTML and CSS/CSS3 or Bootstrap. Now, you can get yourself included in PHP. As said before PHP is the best and simplest programming language. It is easy to write and understand.
For the readers who already know about basics HTML and CSS may directly include themselves in PHP and start learn PHP. So far I searched internet and found some useful sites for study purpose and tutorial videos, I shall be listing them in end to my article.To expand this article and make you all have a better start and end i will write more on some practical topics like HTML5,Bootstrap and PHP, stay tuned. I shall be available soon with my next article by the end of this week.
Links for starting with php :
- Brief history of php
- PDO in PHP
- Working with captcha
- Connecting to database using PDO
- Uploading file
- Predefined variables in PHP
- PHP examples and tutorials
Labels:
PDO,
php,
webdesign,
webdevelopment
Location:
Southern Asia
Subscribe to:
Posts (Atom)