Vertica and PHP connector

One of PHP's strengths is database connectivity. PHP supports a large number of databases and simplifies access via a unified ODBC interface. This provides a number of useful functions.

To see how to create a ODBC connection to Vertica check the following tutorial Vertica ODBC creation in Windows

  • odbc_fetch_row() - function is used to retrieve records from the result-set. This function returns true if it is able to return rows, otherwise false.
  • -this function can take two parameters (result identifier and an optional row number)

    odbc_fetch_row($rs)

    The code line below returns the value of the first field from the record:

    $name=odbc_result($rs,1);

    -the second parameter can be the field number or name of the column that needs to be dumped into the result set variable called name.

    Example of how to create a database connection with PHP on Vertica.

    $conn=odbc_connect('ODBC-name','user_name','user_password');
    if (!$conn)  {exit("Connection Failed: " . $conn);}
    $sql="select table_name from tables;";
    $rs=odbc_exec($conn,$sql);
    if (!$rs) {exit("Error in SQL");
    }while (odbc_fetch_row($rs)) { $name=odbc_result($rs,"table_name");
      echo $name; }odbc_close($conn);

    Where the odbc_connect function will receive 3 parameters:

  • ODBC-name : this will be the ODBC connection that was created using the Vertica driver.
  • user_name : this is the user that will have access to Vertica database.
  • user_password : is the user password.
  • Now you can create custom applications in PHP ,ODBC interface and using Vertica as database tool.