0

I am trying to copy an array into a table in the test database. I get this fatal error: Call to undefined function pg_insert() in C:\wamp\www\Lessons\GMS-DB.php on line 31. Can u please help me! Here is my code:

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$database = "test";


//                       Create server connection 
$con = mysql_connect ($servername, $username, $password, $database) or       die('cant connect server');

mysql_select_db('test',$con);

//                      Create database connection

$db_selected = mysql_select_db('test',$con);
if (!$db_selected) {
   die ('Can\'t use foo : ' . mysql_error());
}

   //-----------------------------------------------------------------------
   //                               Specify directory

$dir    = 'C:/Users/Desktop/data';
$files1 = scandir($dir);
$data = array_diff($files1, array('.', '..')); //remove the dots or periods
// print_r($data);
//                               put in a string
$matstring = implode("','",$data);
$matstring="".$matstring."";
$table_name = `test table 1`;

$res = pg_insert ($con ,  $table_name , $data);
if ($res) {
      echo "POST data is successfully logged\n";
  } else {
      echo "User must have sent wrong inputs\n";
  }


?>
Jeremy D
  • 4,787
  • 1
  • 31
  • 38
Momoyan
  • 1
  • 3
  • Where is your `pg_connect()` if you already use `pg_insert()` ? – Rizier123 Feb 19 '15 at 17:46
  • he's using mysql datase and using pg_insert to insert data – Xpleria Feb 19 '15 at 17:46
  • `mysql_connect` and `pg_insert` dont go together. But that error message actually means you dont have PostGreSQL extension installed/enabled. Since most of your code is mysql based, probably you wanted mysql_query there?` – Hanky Panky Feb 19 '15 at 17:47
  • Sidenote: `$con = mysql_connect ($servername, $username, $password, $database)` that is [`mysqli_`](http://php.net/manual/en/function.mysqli-connect.php) syntax using 4 parameters. http://php.net/manual/en/function.mysql-connect.php the syntax is "server, username, password", then pass the DB to `mysql_select_db` afterwards. – Funk Forty Niner Feb 19 '15 at 17:59

1 Answers1

1

You have two different kinds of databases there. One is MySQL and another is PostgreSQL. Your using a mysql database so use a mysql query to insert.

You need to use mysql query

mysql_query("INSERT INTO $table (column_name, column_name2) VALUES('$value', '$value2')");

Also, you are using mysqli syntax to connect to db. In mysql, the syntax is

$con = mysql_connect ($servername, $username, $password);
mysql_select_db('foo', $con);

And I strongly suggest you to use mysqli. Ancient mysql is no longer supported!

Here's the code in MySQLi

$db = new mysqli("host", "user", "password", "database");
$db->query("INSERT INTO $table (column_name, column_name2) VALUES('$value', '$value2')");

When should I use MySQLi instead of MySQL?

MySQLi Query

Community
  • 1
  • 1
Xpleria
  • 5,472
  • 5
  • 52
  • 66