1
I am trying to make an Internet with PDO. For this I use:
controller/con_clients.add.php
<?php
if($action == "new_client"){
    $search_cod_cliente = $pdo->prepare("SELECT MAX(cli_cod_cliente) AS cli_cod_cliente FROM clients"); 
    $search_cod_cliente->execute();
    while($result_cod_cliente = $search_cod_cliente->fetchObject()){
        $cli_cod_cliente = $result_cod_cliente->cli_cod_cliente + 1;
    }
    $conectar = new Con_Clients_Add;
    $conectar = $conectar->con_clients_add($cli_cod_cliente);
}
?>
classes/Con_clients_add.class.php
<?php
    class Con_Clients_Add{
        public function con_clients_add($cli_cod_cliente){
            try {
                $pdo = new PDO('mysql:host='.$host.';dbname='.$database, $login_db, $senha_db);
                $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch(PDOException $e) {
                echo 'ERROR: ' . $e->getMessage();
            }
            $client_insert = $pdo->prepare("INSERT INTO clients(cli_cod_cliente) VALUES (:cli_cod_cliente)");
            $client_insert->bindParam(':cli_cod_cliente', $cli_cod_cliente, PDO::PARAM_STR);
            $client_insert->execute(); 
        }
    }
?>
But if I use the traditional method, it works:
$db = mysql_connect ($host, $login_db, $senha_db);
$basedados = mysql_select_db($database);
$insere_cliente = mysql_query("INSERT INTO clients(cli_cod_cliente) VALUES ('$cli_cod_cliente')");
Thank you for the force. Hugs. Rafael
Any mistakes? What happens?
– Miguel
Yeah... Error appears 500
– Rafael Schaffer Gimenes
This
echo 'ERROR: ' . $e->getMessage();doesn’t happen?– Miguel
No... The funny thing is that the $cli_cod_client variable comes from the/con_clients_add.php... But if I force a value, for example: $cli_cod_client = '1900'; there it works...
– Rafael Schaffer Gimenes
I’m doubting it has anything to do with this... $client_insert->bindParam(':cli_cod_client', $cli_cod_client, PDO:PARAM_STR);
– Rafael Schaffer Gimenes
I just think it’s weird because it doesn’t seem to have anything to do with the connection, otherwise it would print
'ERROR: ' . $e->getMessage();– Miguel
Puts this at the beginning of your script, should show errors:
ini_set('display_errors', true);
error_reporting(E_ALL);– rray