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
Related
I am trying to establish a connection to my works Pervasive SQL database. I've tried using odbc_connect (didn't work) but I was told that PDO is easier and better (HA, also didn't work). This is my connection string
$dbh = new PDO("odbc:Driver={Pervasive ODBC Client Interface};ServerName=192.168.43.19;dbq=GLOBALTST");
I've tried odbc:DSNname (https://www.php.net/manual/en/ref.pdo-odbc.connection.php), ODBC:servername (ip and hostname), and odbc:databasename. Nothing has worked. This is the error I am getting:
Fatal error: Uncaught PDOException: SQLSTATE[IM003] SQLDriverConnect: 160 Specified driver could not be loaded due to system error 1114: A dynamic link library (DLL) initialization routine failed. (Pervasive ODBC Client Interface, C:\PSQL\bin\w3odbcci.dll). in C:\inetpub\wwwroot\default.php:4 Stack trace: #0 C:\inetpub\wwwroot\default.php(4): PDO->__construct() #1 {main} thrown in C:\inetpub\wwwroot\default.php on line 4
The DLL it is looking for is actually located in the spot it's referencing. as far as I can tell there are no issues with it. THe ODBC is configured correctly on the server and I can connect to the DB in the PSQL control center. can anyone help in identifying my issue or pointing to a connection string that works either odbc_connect or PDO?
This code worked for me using PSQL v11 64 bit ODBC, PHP 7.2 64 bit on a Windows machine.
<?php
try {
// Connect to the data source
//$dbh = new PDO($dsn);
$dbh = new PDO("odbc:Driver={Pervasive ODBC Interface};ServerName=192.168.43.19;dbq=demodata");
$stmt = $dbh->prepare('SELECT * FROM class');
// Execute the prepared statement for each name in the array
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$numResults = count($result);
echo ('<b>Total Results: </b> ' . $numResults . '<br>');
if ($numResults > 0) {
// Output the table header
echo '<table><tr>';
foreach ($result[0] as $fieldName=>$value) {
echo '<th>' . htmlspecialchars($fieldName) . '</th>';
}
echo '</tr>';
// Now output all the results
foreach($result as $row) {
echo '<tr>';
foreach ($row as $fieldName=>$value) {
echo '<td>' . htmlspecialchars($value) . '</td>';
}
echo '</tr>';
}
// Close the table
echo '</table>';
} else {
echo 'No results';
}
// Close statement and data base connection
$stmt = NULL;
$dbh = NULL;
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
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.
I have a sqlite db file which is definitely not corrupt since I can open it with SQLiteStudio.
However, when I try to open it dynamically with PHP with the following code I found in some tutorial:
class MyDB extends SQLite3
{
function __construct()
{
$this->open('../testDB');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
$sql ="SELECT * from testTable";
$ret = $db->query($sql);
while($row = $ret->fetchArray(SQLITE3_ASSOC) )
{
echo "ID = ". $row['id'] . "\n";
echo "NAME = ". $row['name'] ."\n\n";
}
echo "Operation done successfully\n";
$db->close();
I get the following result:
Opened database successfully
Warning: SQLite3::query() [sqlite3.query]: Unable to prepare statement: 11, database disk image is malformed in test.php on line 52
Fatal error: Call to a member function fetchArray() on a non-object in test.php on line 53
I found some threads like that one, but none of them had a definite answer.
Can somebody help me out here? Thanks in advance!
I had this same "database disk image is malformed" error and i solved it using SQLiteStudio.
Since you already have it, open the file, right click on the database and try the Vacuum option. After doing this, try the integrity check. If the result is 'OK' then your problem is solved. At least that's how i solved it. Vacuum rebuilds the entire database.
I've already the same problem with small SQLITE db.
Try to use:
$sql = "VACUUM";
$db->query($sql);
I am trying to list data from two specific columns in a table. Whenever I go to the file, it returns a server error. When I remove the while loop, it executes perfectly, so I have no idea what I am doing wrong.
Here's the error:
Server error The website encountered an error while retrieving
http://dayzlistings.com/reg-whitelist.php. It may be down for
maintenance or configured incorrectly. Here are some suggestions:
Reload this webpage later. HTTP Error 500 (Internal Server Error): An
unexpected condition was encountered while the server was attempting
to fulfill the request.
try {
$dbh = new PDO('mysql:host=localhost;dbname=dbname','dbuser','dbpass');
$sql = "SELECT * from wp_cimy_uef_data";
$q = $dbh->prepar($sql);
$q->execute();
while($row = $q->fetchall() {
echo $row['USER_ID'];
echo $row['VALUE'];
}
}
$dbh = null;
} catch (PDOException $e) {
print "Error from Dedicated Database!: " . $e->getMessage() . "<br/>";
die();
}
500 means something wrong when you interact with the server, e.g. access db.
And $row['USER_ID'] will never work, instead you should use $row[0]['USER_ID'].
isn't fetchAll() returns the whole table as an array?..
you don't need to while loop $row.. just do $row = $q->fetchAll() without while and print_r the whole array and see what you get..
and if you still wanna do while I think you may use
while($row = $q->fetch()){
// rest of the code here
}
Also you cannot put annything between try catch..
try{
//code
}
$dbh = null; //**This is not allowed by the way...**
catch(PDOException $e){
//code
}
Dins
I am starting to use PDO and I successfully connected to MySQL using PDO. However, when I try to SELECT stuff from my DB, nothing happens. Nothing is echoed. (even though I have records in that table, and the column username exists) No error in my PHP log.
I am using MAMP and all PDO components seem to be working in phpinfo() (since I was able to connect to db in the first place)
Please let me know what could have gone wrong. Thanks a lot
<?php
try
{
$connection = new PDO('mysql:host=localhost;dbname:my_db','my_username',
'xxxxxxx');
$stmt=$connection->prepare("SELECT * FROM users");
$stmt->execute();
while ($row=$stmt->fetch(PDO::FETCH_OBJ)){
echo $row->username;
}
}
catch(Exception $e)
{
echo "There was an error connecting to the database";
}
?>
You need to tell PDO that you want it to throw exceptions:
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Following your comment below, it is apparent that your DSN is incorrect. It should be:
$connection = new PDO('mysql:host=localhost;dbname=my_db','my_username','xxxxxxx');
Note that the syntax is dbname= rather than dbname: (which you had originally).
First, get your query out of your PDO connection segment...
<?php
try
{
$connection = new PDO('mysql:host=localhost;dbname:my_db','my_username',
'xxxxxxx');
}
catch(Exception $e)
{
echo "There was an error connecting to the database";
}
?>
Then, do it.
<?php
$SQL = 'SELECT * FROM users';
foreach($connection->query($SQL) as $row){
print $row['username'] . "\n".'<br />';
}
?>
Why not ask PHP?
catch(Exception $e)
{
die($e);
}
Looks like your either don't have data in that table or have an error:
Try to add this code after $stmt->execute();:
$arr = $sth->errorInfo();
print_r($arr);