0

I just tried to learn pdo by few examples and it is not going well, I have too many questions where I cannot find any answers I need...

First with mysqli I had $con = mysqli_connect() or die(); and then when I need I use $con with mysqli_real_escape_string as first parameter, when I Select from table, update etc..

But here as I see on This tutorial and W3Schools tutorial, I need to do this:

$servername = "localhost";
$username = "root";
$password = "";

try{
    $con = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}catch(PDOException $e){
    $error =  "Connection failed: " . $e->getMessage();
}

And under $con to add preparation for fetching data inserting etc. Can I get it to use like with mysqli somehow to have connect.php file where I connect to db and then use $con as variable where it is needed? Also I do not understand what are those "::" here PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION

Here is the code I tried but without adding $con to class method insertUser I can't access it by including connection in other file. And when I do add it I do get success message but nothing is inserted into table.

include_once('./c.php');
$p0 = $_POST['p0'];
$p1 = $_POST['p1'];
$p2 = $_POST['p2'];
$p3 = $_POST['p3'];
$p4 = $_POST['p4'];
class newUser{
        function insertUser($p1, $p2, $p3, $p4){
            $pid = md5($p3);
            $pw  = md5($p4);
            $servername = "localhost";
            $username = "root";
            $password = "";
            $con = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); 
//I have this into my c.php into try{}catch(){} but I cannot access it...
            $stmt = $con->prepare("INSERT INTO suu (nf, nl, me, wp, pin, rdate, ac) 
            VALUES (:firstname, :lastname, :email, :password, :profileid, now(), 0)");
            $stmt->bindParam(':firstname', $p1);
            $stmt->bindParam(':lastname', $p2);
            $stmt->bindParam(':email', $p3);
            $stmt->bindParam(':password', $pw);
            $stmt->bindParam(':profileid', $pid);
            $stmt->execute();

    echo "registerSuccessfully.html"; //I get this message but table is empty
        }
        function checkData($p1, $p2, $p3, $p4){
            if($p1!="" && $p1!=NULL && $p2!="" && $p2!=NULL && $p3!="" && $p3!=NULL && $p4!="" && $p4!=NULL){
                return true;
            }else return false;
        }
    }
   if($p0=="reg"){
    $newUser = new newUser($p1, $p2, $p3, $p4);
    if($newUser->checkData($p1, $p2, $p3, $p4)){
        try{
        $newUser->insertUser($p1, $p2, $p3, $p4);
        }catch(PDOException $e){
        echo "Error: " . $e->getMessage();
        }
    }
   }else{

   }
  • question is: did your code work with mysqli_ with that class and methods to start with, before moving over to PDO? the form's unknown and MD5 can't be trusted to store hashes with. – Funk Forty Niner Mar 22 '17 at 17:15
  • plus I see you're passing 4 values in the methods but 5 binded values in the query. – Funk Forty Niner Mar 22 '17 at 17:18
  • It worked, Yes because I am making fifth one inside method, it is md5 of email. –  Mar 22 '17 at 17:20
  • do not create a new connection, use existing one. do not use try and catch, get every single one from your code. Stay away from W3Fools – Your Common Sense Mar 22 '17 at 18:26
  • I found the answer, anyway i am wondering why is my question posted as duplicate when I cannot find same question? –  Mar 22 '17 at 20:23

0 Answers0