Search
 
SCRIPT & CODE EXAMPLE
 

PHP

use prepared statement in a where clause PDO

	$servername = "localhost";
	$username = "root";
	$password = "toor";
	$dbname = "testdb";
    $user = 'admin';
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT * FROM radcheck where username = :username ");
    $stmt->bindParam(':username', $user);
    $stmt->execute();

    // set the resulting array to associative
//    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $counter = $stmt->fetchAll();
    echo "Total rows returned" .count($counter);
Comment

PDO INSERT prepared statement

<?php
$servername = "localhost";
$username = "root";
$password = "";
$database_name = "database_name";

// CREATE DATABASE CONNECTION USING PDO METHOD
try {
    $database_connection = new PDO("mysql:host=$servername;dbname=$database_name", $username, $password);
    // Set the PDO error mode to exception
    $database_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "Connection Failed" . $e->getMessage();
}


// INSERT DATA INTO THE DATABASE
try {

    $sql = "INSERT INTO `users` (first_name, last_name, email) VALUES (?,?,?)";
    $stmt = $database_connection->prepare($sql);

    $stmt->bindParam(1, $first_name, PDO::PARAM_STR);
    $stmt->bindParam(2, $last_name, PDO::PARAM_STR);
    $stmt->bindParam(3, $email, PDO::PARAM_STR);

    // Assigning data into the variables
    $first_name = 'John';
    $last_name = 'Doe';
    $email = 'johndoe@gmail.com';
    $stmt->execute();

    echo "Data inserted successfully";

} catch (PDOException $e) {
    echo "data not inserted";
}
?>
Comment

PDO Prepared Statement php

//Using PDOStatement to protect db from sql injection
// text is the form field you are trying to protect
//We are using  $sql here as the object and joke_table is the
// name of the database table we will insert our jokes in.
//Text in here represents the database column that our users  
//input will be inserted in.

if (isset($_POST['text'])) {
   try {
        $pdo= new PDO('mysql:host=localhost;dbname=omo; chaerset=utf8','username','passwrd');
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = 'INSERT INTO `joke_table` SET   
        `text` = :text, 
        `date` = CURDATE()'; 
        $stmt = $pdo->prepare($sql); 
        $stmt->bindValue(':text', $_POST['text']); 
        $stmt->execute(); 
        header('location: thank.php'); 
      } catch (PDOException $e) {
            echo 'error in connecting to the database'.$e->getMessage().'in'.$e->getFile().':'. $e->getLine(). $e->getCode();
         
    } 
    } else {
    // use any logic that you would like to happen here

    }
Comment

PDO INSERT prepared statement

VALUES (:first_name, :last_name, :email)
$stmt->bindParam(':first_name', $first_name, PDO::PARAM_STR);
$stmt->bindParam(':last_name', $last_name, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
Comment

PREVIOUS NEXT
Code Example
Php :: laravel validation check foreign key exists 
Php :: how to loop by index in php 
Php :: error laravel 404 in server 
Php :: auto refresh extintion php 
Php :: make controller and model laravel 
Php :: defining route through controller 
Php :: laravel rate limit 
Php :: get diff array php 
Php :: php buffer 
Php :: php check for duplicates in array 
Php :: laravel eloquent multiple join 
Php :: download file using jquery php 
Php :: how to create a php website 
Php :: sha256 php cantidad caracteres 
Php :: Simple factory Design pattern in PHP 
Php :: @method overide form laravel 
Php :: php string variable 
Php :: no routes.php in http folder 
Php :: comment blade php 
Php :: mixed content laravel form target 
Php :: Add button next to "ADD TO CART" on product archive 
Php :: php file iterator 
Php :: laravel get data from model to controller 
Php :: Calculate Math Expression From A String Text With PHP 
Php :: symfony append to file 
Php :: laravel websockets 
Php :: php multiplei str 
Php :: check email veriy or not laravel 
Php :: drupal show php errors 
Php :: php get day of week number 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =