0

I'm trying to put my database online put in my code i have PHP files that are only PHP and others PDO .

I added 2 different connections to each :

database.php(PDO)

$user='';
$pass='';
$db='';
$dbserver='';


try {
$conn = new PDO("mysql:host=$dbserver;dbname=$db", $user, $pass);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully"; 
} catch(PDOException $e) {    
echo "Connection failed: " . $e->getMessage();
}

?> 

One of the PDO files I started with :

<?php
header('Access-Control-Allow-Origin: *');

include('database.php');

  try {
$conn = new PDO("mysql:host=$dbserver;dbname=$db", $user, $pass);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully"; 
} catch(PDOException $e) {    
echo "Connection failed: " . $e->getMessage();
}

 $data = array();



     $a_username         = filter_var($_POST['a_username'], FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
     $p_username         = filter_var($_POST['p_username'], FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
     $password           = filter_var($_POST['password'], FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
     $vpassword          = filter_var($_POST['vpassword'], FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);

try{

        $sql  = "INSERT INTO account_info (a_username,p_username, password, vpassword) 
            VALUES(:a_username, :p_username, :password, :vpassword)";

        $stmt    = $conn->prepare($sql);    

        $stmt->bindParam(':a_username', $a_username, PDO::PARAM_STR);
        $stmt->bindParam(':p_username', $p_username, PDO::PARAM_STR);
        $stmt->bindParam(':password', $password, PDO::PARAM_STR);
        $stmt->bindParam(':vpassword', $vpassword, PDO::PARAM_STR);

        $stmt->execute();

        echo json_encode(array('message' => 'Congratulations the record was added to the database'));

}
     catch(PDOException $e)
     {
        echo $e->getMessage();
     }      
 ?>

No more errors but I still can't log in from my phone

Voula16
  • 55
  • 8
  • Do not mix mysql APIs. You'll just give yourself a headache. Why would you want to? – aynber Dec 12 '17 at 18:40
  • I originally had my code in` PDO` but i couldn't do some stuff with it and I found the solution with `mysqli` i know it's not the optimal solution but is there a way it might work ? – Voula16 Dec 12 '17 at 18:41
  • is there a way i can maybe link the `PDOs` together in `database1` and the `mysqli`s in `database2` together and then link them ? – Voula16 Dec 12 '17 at 18:51
  • Or maybe put both in one `database.php` and put an if statement ? – Voula16 Dec 12 '17 at 18:52
  • That can work. You won't be able to use them in the same call, but you can get the results from each one, and then manipulate them yourself. – aynber Dec 12 '17 at 18:52
  • I created 2 separate but this error occurs : `Fatal error: Uncaught Error: Call to a member function prepare() on null in...` on : `$stmt = $pdo->prepare($sql);` – Voula16 Dec 12 '17 at 21:00
  • Then your PDO connection failed. Check for PDO errors to see what the problem might be. – aynber Dec 13 '17 at 00:21
  • when I add `include('database.php');` should I keep or remove `$dsn = "mysql:host=" . $hn . ";port=3306;dbname=" . $db . ";charset=" . $cs; $opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_EMULATE_PREPARES => false, ); $pdo = new PDO($dsn, $un, $pwd, $opt);` from my pdo files – Voula16 Dec 13 '17 at 09:29
  • It should all be in one place. – aynber Dec 13 '17 at 13:32
  • fixed I got no more errors but i still can't log in – Voula16 Dec 13 '17 at 14:14

0 Answers0