0

i'm trying to make an insert query with php via ODBC to a 4D Database and i get the following error :

SQL error: Server rejected the connection: Failed to execute statement. , SQL state 08004 in SQLExecDirect

Here's my php code :

<?php
$user = 'Super_Utilisateur';
$pass = '123';
$server = '127.0.0.1';
$driver = '4D v16 ODBC Driver 64-bit';
$sql='INSERT INTO patient(nom) VALUES ("patient100")';

$connection_string='DRIVER={'.$driver.'};SERVER='.$server.';UID='.$user.';PWD='.$pass;
$conn = odbc_connect( $connection_string , $user , $pass);
if ($conn) {

    // connected
    echo 'Connection established.' . PHP_EOL;

   // working
    $result=odbc_exec($conn,$sql);
    //$Heure= odbc_result($result,'HeureT');
    odbc_close($conn);

} else{

    // could not connect
    die("Connection could not be established.");
}
// probably not needed
odbc_close_all();
?>

Thanks,

  • Seems like connection issue – Thamaraiselvam Jan 24 '18 at 16:44
  • Hi, can you be more specific please? the connection is established, when doing a Select query it works properly . – MrIlias Jan 24 '18 at 16:48
  • Does the code (`INSERT INTO patient(nom) VALUES ("patient100")`) work from inside of 4D? More details on this troubleshooting technique is found here: [http://kb.4d.com/assetid=76287](http://kb.4d.com/assetid=76287) – Tim Penner Jan 24 '18 at 17:29

2 Answers2

1

When experiencing problems querying a 4D database from PHP (via ODBC or PDO), it is a good test to try your SQL request from 4D itself to make sure the request is valid.

First, from within the targeted 4D database, you can test your SQL statement like this example:

Begin SQL
INSERT INTO patient(nom) VALUES ("patient100")
End SQL

You will then detect if the request is supported by the 4D SQL engine.

If the SQL statement works from within the targeted 4D database, then you should test from a remote 4D application using SQL LOGIN.

Next, from a remote 4D application, use "SQL Login" before "Begin SQL" like this example:

SQL Login($4D_ODBC_DSN; $login; $password)
Begin SQL
INSERT INTO patient(nom) VALUES ("patient100")
End SQL

Note: Make sure to use the correct DSN for your server.

Doing this will detect if the problems come from authentication or from the communication protocol.


Source: Tech Tip: Troubleshooting SQL Query Problems when connecting with PDO_4D

More sample code

Tim Penner
  • 3,551
  • 21
  • 36
0

The only thing you need to change are the quotes around the sql

$sql="INSERT INTO Test (Full_name) VALUES ('HansMuster');";

Then it works perfectly

Julian
  • 33,915
  • 22
  • 119
  • 174