0

Am using PDO method to get data from SQL But am unable to get data from SQL database since am new to PDO its little hard to understand

but i did the following code but Doesn't work can someone help me

CODE

<?php

$servername = "localhost";
$username = "sanoj";
$password = "123456";
$dbname = "test";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT * FROM members");
    $stmt->execute();
    // set the resulting array to associative
    $membid=($_GET['memberID']);
$email=($_GET['email']);
$state=($_GET['username']);
    }
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}



echo $membid;
echo $email;
echo $state;
$conn = null;
echo "</table>";
?>

ERROR I GET

Notice: Undefined index: memberID in C:\Users\sanoj\Documents\NetBeansProjects\godaddy optimized\data.php on line 14
Call Stack
#   Time    Memory  Function    Location
1   0.0020  132392  {main}( )   ..\data.php:0

( ! ) Notice: Undefined index: email in C:\Users\sanoj\Documents\NetBeansProjects\godaddy optimized\data.php on line 15
Call Stack
#   Time    Memory  Function    Location
1   0.0020  132392  {main}( )   ..\data.php:0

( ! ) Notice: Undefined index: username in C:\Users\sanoj\Documents\NetBeansProjects\godaddy optimized\data.php on line 16
Call Stack
#   Time    Memory  Function    Location
1   0.0020  132392  {main}( )   ..\data.php:0
romman
  • 201
  • 2
  • 4
  • 15

1 Answers1

2

Using $_GET variable won't make you get your data from your database using PDO. You were doing right up until $stmt->execute();. What you need to do next is:

while($result = $stmt->fetch(PDO::FETCH_ASSOC))
{ 
    foreach($result as $key => $value)
        echo $key.': '.$value.'<br/>';
    echo '<hr/>';
}

To get your data.

$_GET array is to fetch your data from the URL or from a GET Ajax call.

D4V1D
  • 5,805
  • 3
  • 30
  • 65