0

Having difficulty finding info on how to rewrite this mySql line into PDO:

if ($_GET["action"] == "list")

In the code I am trying to convert, four if/else statements look similar to this:

if ($_GET["action"] == "create") {
        //Insert record into database
        $result = mysql_query("INSERT INTO people(Name, Age, RecordDate) VALUES('" . $_POST["Name"] . "', " . $_POST["Age"] . ",now());");

        //Get last inserted record (to return to jTable)
        $result = mysql_query("SELECT * FROM people WHERE PersonId = LAST_INSERT_ID();");
        $row = mysql_fetch_array($result);

        //Return result to jTable
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        $jTableResult['Record'] = $row;
        print json_encode($jTableResult);
    }
Nick
  • 138,499
  • 22
  • 57
  • 95
jefferp
  • 21
  • 2
  • Wow. That got totally reformatted and messed up. This is the line I am having trouble converting from MySql to PDO> if ($_GET["action"] == "create") { – jefferp Dec 24 '18 at 02:15
  • There is nothing MySQL specific in that line of code... – Nick Dec 24 '18 at 02:31
  • That `$_GET` condition does not change. Do you have general familiarity with how PDO should be written `prepare()/execute()`? The `INSERT` statement and subsequent retrieval of the new record are what you need to modify. – Michael Berkowski Dec 24 '18 at 02:35
  • My bad. Working on this 12 hours in a row makes me stupid. I never updated the "mysql_fetch_array", and my PHP version doesn't like mySql. Sorry to be a bother. – jefferp Dec 24 '18 at 02:55

1 Answers1

0

I have provided complete PDO code for your current scenario that is inserting data into table and then getting information of last inserted record. For better understandings you should have to look into PHP PDO Class. Its really simple, easy and you can find a lot of things on this topic.

<?php

    //Specifying database credentials

    $dbhost = "localhost";// Your host name
    $dbname = "test"; // Your database name
    $dbuser = "root"; // database user
    $dbpass = "";// Database password

    $Name = $_POST["Name"];
    $Age = $_POST["Age"];
    $RecordDate = date("d-M-Y h:i:s a");
    $jTableResult = array();

    //Establishing connection to the database
    try
    {

        $db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);

    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage();
        die();
    }

$sql = "INSERT INTO people(Name, Age, RecordDate) VALUES (:Name, :Age, :RecordDate)"; 
$statement = $db->prepare($sql);
//Bindging values to be added to People table
$statement->bindParam(":name", $firstname ); 
$statement->bindParam(":Age", $Age ); 
$statement->bindParam(":RecordDate", $RecordDate ); 


if($statement->execute()) // Check if insert statement executed successfully 
{

    $PersonId = $db->lastInsertId(); // Getting Last inserted id;

    $get_statement= $db->prepare("SELECT * FROM people WHERE PersonId=:PersonId");
    $get_statement->bindParam(":PersonId", $PersonId ); 

    $get_statement->execute(array(':Uemail'=>$email, ':Upassword'=>$password));
    if($row=$get_statement->fetch(PDO::FETCH_ASSOC)) // as we are getting only one record, therefore fetch() method is best. The fetchAll should be used if you are getting multiple records
    {

        $jTableResult['Result'] = "OK";
        $jTableResult['Record'] = $row;
        print json_encode($jTableResult);

    }
    else
    {
        echo json_encode("Error");
    }



}
else
{
        $jTableResult['Result'] = "FAIL";
        $jTableResult['Record'] = array();
        print json_encode($jTableResult);
}       
?>
Sajjad Ali
  • 304
  • 3
  • 12