Connect Laravel with vertica Database - php

I have problems linking my Project to my database. I want to select stuff from a vertica database into my project. In normal PHP it works, but the same code does not work in Laravel:
# Connect to the Database
$dsn = "VerticaDSN";
$conn = odbc_connect($dsn,'','') or die ("<br/>CONNECTION ERROR");
echo "<p>Connected with DSN: $dsn</p>";
# Get the data from the table and display it
$sql = "SELECT column FROM table";
if($result = errortrap_odbc($conn, $sql))
{
echo "<pre>";
while($row = odbc_fetch_array($result) )
{
echo "hi";
print_r($row);
}
echo "</pre>";
}
Is there a method that this code works inside my Laravel Controller?

What exact error have you? Is your $dns correct? It must look like:
$dsn = 'Driver=Vertica;Servername=xxx;Port=5433;Database=yyy';
odbc_connect($dsn,'usr','pwd');
In my Github repository there are detailed instructions for the use of Vertica in Laravel.

Related

I am not able to execute select records from a table in database in google sql cloud using php

<?php
define('DB_NAME', 'xp_database'); // DATABASE
define('DB_USER', 'valli'); // ROOT DEFAULT MYSQL
define('DB_PASSWORD', 'Valli123'); // PASSOWORD
define('DB_HOST', '127.0.0.1'); // LOCAL IF YOU USE LOCAL.
$data=array();
$i=1;
$link = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if($link === false)
{
die("ERROR: Could not connect. " . mysqli_connect_error());
}
else
{
echo "Connected to database";
}
// Attempt select query execution
$sql = "show databases";
if($result = mysqli_query($link, $sql))
{
if(mysqli_num_rows($result) > 0)
{
echo "<table>";
echo "<tr>";
echo "<th>userid</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['user_id'] . "</td>";
echo "</tr>";
// Free result
//set mysqli_free_result($result);
}
}
else
{
echo "No records matching your query were found.";
}
}
else
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
I have enabled the Admin API in google cloud.
Connection is successful. But retrieval using select query is not successful. Please tell me what is missing in this. Is there any other setting that I have to do. I have migrated the data from local database to mysql cloud database.
If you are using App Engine to connect to Cloud SQL, bear in mind that you will need to enable Cloud SQL proxy since Google App Engine connects from inside of Google servers to your Cloud SQL instance via proxy.
Nonetheless, if what you would like to do is connect from outside with pure PHP code, you can still do it without using the Cloud SQL proxy.
For connecting from outside using pure PHP code you will need to authorize your IP from your Cloud SQL instance, as shown here.
Then, you should modify your code in order to change localhost or 127.0.0.1 to your Cloud SQL instance public IP (taking into account the other variables like username, password and the database name that you would like to connect to).
For finding your Cloud SQL's public IP, you can refer to here.
Finally, if you would like to look around PHP on Google App Engine, you can do it by checking the documentation here.
I hope this helps.

Ajax Sqlite could not find driver./SQLSTATE[HY000]

i am trying to realize an exercice with Ajax and Sqlite database for which i use DB Browser for SQLite. The purpose of the exercice is to have two combobox fill each other in cascade. But first of all i need to fill the first one from data that i gather from the Database. But i am blocked on that first step.
Here is the code i made until now :
<?php
try
{
$db = new
PDO('sqlite3:\C:\xampp\htdocs\www\TPLigueDesChampions\ligueChampions.db');
$result = $db->query('SELECT pays FROM listepays');
echo ' <select name=\'Code\'><br>';
echo ' <option value=\'0\'>Choisissez</option>';
foreach($result as $row)
{
echo '<option value=\''.$row['Pays'].'<option><br>';
//echo "hello";
}
}
catch(PDOException $e)
{
print 'Exception : '.$e->getMessage();
}
?>
So when i make sqlite3 in pdo call i have the following error:
"Could not find driver"
And when i use sqlite normal it gives me out this: SQLSTATE[HY000] [14] unable to open database file
I searched in internet about a solution but everything i found until now is related to MySQL and i need SQLite. Can you please help?
for php
<?php
$bd = new SQLite3('ligueChampions.db');
$results = $bd->query('SELECT pays FROM listepays');
while ($row = $results->fetchArray()) {
var_dump($row);
}
?>
SQLite3 and Php

php local connection mysql database

I try to make a simple IOS app that can connect to mysql database and read one table. But my php code does't work and really have no idea why, it's seems correct to me. The database is in a raspberry phpmyadmin server and the server works great.
I will put my code here and please tell me what's wrong.
<?php
$host = "192.168.2.193";
$db = "produtos";
$user = "root";
$pass = "1234";
$connection = mysql_connect($host, $user, $pass);
if(!$connection)
{
die("Database server connection failed.");
}
else
{
//attempt to select the database
$dbconnect = mysql_select_db($db, $connection);
//check to see if we could select the database
if(!dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$query = "SELECT * FROM produtos";
$resultset = mysql_query($query, $connection);
$records = array();
//loop throught all our records and add them to our array
while ($r = mysql_fetch_assoc($resultset))
{
$records[] = $r;
}
echo json_ecode($records);
echo $resultset;
}
}
?>
Based on the question:
use mysqli_connect rather than mysql_connect because mysql_connect is deprecated and will not work someday. Also what is the the error you are getting? change your die() statement to something more helpful die(mysqli_error($connection));
Based on your comment:
That error would suggest that you either A) don't have the right IP address or B) there is a network issue between your host server and the SQL server, is this code running on the same server that is hosting the SQL database? if so then you can probably just use localhost for your $host

SQL Server Database Query with PHP

I need to get some data from a Microsoft SQL Server database at work. When I have the data I need, I need to make an Excel spreadsheet that can be saved locally on my computer.
I found PHPExcel which seems to do the job on the Excel part, but what about getting the data from the Database?
I can't seem to find anything that's recent. Only old tutorials.
Use this way to Fetch the Records :
<?php
$hostname = "192.168.3.50";
$username = "sa";
$password = "123456";
$dbName = "yourdb";
MSSQL_CONNECT($hostname,$username,$password) or DIE("DATABASE FAILED TO RESPOND.");
mssql_select_db($dbName) or DIE("Database unavailable");
$query = "SELECT * FROM dbo.table";
$result = mssql_query( $query );
for ($i = 0; $i < mssql_num_rows( $result ); ++$i)
{
$line = mssql_fetch_row($result);
print( "$line[0] - $line[1]\n");
}
?>
This will fetch each rows from the Data Retrieve and Print on the Page. Use your Required format into that. I mean, Use html Table to show the data in well format.
Use this code to get an data from Database.
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = '192.168.3.50';
// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'sa');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
else{
echo "connected ";
mssql_select_db('Matrix') or die("Wrong DATAbase");
//mssql_query("SELECT Seq_no from dbo.Trans_R WHERE Seq_no = 000001",$link) or die("cannot execute the query");
$query = mssql_query("SELECT Tr_Date,Tr_Time,Tr_Data from Matrix.dbo.Trans_R");
$f = mssql_fetch_array($query);
echo $f['Tr_Date'];
}
?>
Can i know why Negative Vote??
He asked me to :
" but what about getting the data from the Database?"

Insert data into MySql table strange exception

I am using this code to insert some values in MySql table:
<?php
mysql_connect("localhost","root","root");
mysql_select_db("bib");
$id = "12";
$titlu = "Joe";
$query = "INSERT INTO carte SET id='$id', titlu='$titlu'";
$result = mysql_query($query);
// Display an appropriate message
if ($result)
echo "<p>Product successfully inserted!</p>";
else
echo "<p>There was a problem inserting the Book!</p>";
mysql_close();
?>
After running it into browser, the following error occurs:
"Apache HTTP Server has encountered a problem and needs to close. We are sorry for the inconvenience."
It seems that mysql_select_db("bib") statement causes it. Database is create , also table...
I am running php 5.3 and mysql 5.1 on windows xp sp 2.
Please any ideas are welcomed...
Thanks...
Any of the mysql_* functions can fail for various reasons. You have to check the return values and if a function indicates an error (usually by returning FALSE) your script has to react appropriately.
mysql_error($link) and mysql_errno($link) can give you more detailed information about the cause. But you don't want to show all the details to just any arbitrary user, see CWE-209: Information Exposure Through an Error Message.
If you don't pass the connection resource returned by mysql_connect() to subsequent mysql_* functions calls, php assumes the last successfully established connection. You shouldn't rely on that; better pass the link resource to the functions. a) If you ever have more than one connection per page you must pass it anyway. b) If there is no valid db connection the php-mysql modules tries to establish the default connection which is usually not what you want; it only takes up more time to fail ..again.
<?php
define('DEBUGOUTPUT', 1);
$mysql = mysql_connect("localhost","root","root");
if ( !$mysql ) {
foo('query failed', mysql_error());
}
$rc = mysql_select_db("bib", $mysql);
if ( !$rc) {
foo('select db', mysql_error($mysql));
}
$id = "12";
$titlu = "Joe";
$query = "INSERT INTO carte SET id='$id', titlu='$titlu'";
$result = mysql_query($query, $mysql);
// Display an appropriate message
if ($result) {
echo "<p>Product successfully inserted!</p>";
}
else {
foo("There was a problem inserting the Book!", mysql_error($mysql), false);
}
mysql_close($mysql);
function foo($description, $detail, $die=false) {
echo '<pre>', htmlspecialchars($description), "</pre>\n";
if ( defined('DEBUGOUTPUT') && DEBUGOUTPUT ) {
echo '<pre>', htmlspecialchars($detail), "</pre>\n";
}
if ( $die ) {
die;
}
}
try this to connect to database:
$mysqlID = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD) or die("Unable to connect to database");
mysql_select_db(DB_DATABASE) or die("Unable to select database ".DB_DATABASE);
also, try this as your insert query:
$query = "INSERT INTO carte (id, title) values ('".$id."', '".addslashes($titlu)."')
$result = mysql_query($query) or die(mysql_error());
By using die(), it will tell you where it has failed and why

Categories