Dealing with DDLs

In PHP, we have the pg_query() function that we use to execute all kinds of SQL statement. We can use this function to execute either DDL or DML statements. This function takes two arguments; one is the db connection resource and the other is an SQL statement. To track the SQL statement status, we have the pg_last_error() function, which takes the db connection as its argument.

Now, let's try to create a test table using the preceding getDBConnection()

<?php 
include'pgconnection.php'; 
 
$con=getDBConnection(); 
$sql =<<<EOF 
CREATE TABLE test(d char(1)); 
EOF; 
 
if (pg_query($con, $sql)) { 
  echo"test table is created successfully\n"; 
} 
else{ 
  echo"Failed in creating table\n"; 
  echopg_last_error($con); 
  echo"\n"; 
} 
?> 
 
$ phptest.php Successfully ...

Get PostgreSQL Development Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.