I'm new to SQLite3 and PHP and was wondering whether and how I could connect to a SQLite3 database with PHP.
How would I get the data from the db and would it be possible to output them on a browser screen?
I've been searching the web for a while now, but couldn't find anything.
Thank you.
<?php
$db = new SQLite3('mysqlitedb.db');
$results = $db->query('SELECT bar FROM foo');
while ($row = $results->fetchArray()) {
var_dump($row);
}
?>
Taken from here: PHP: SQLite3::query - Manual
SQLite is enabled by default with PHP. You have to use the built-in class SQLite3 (you will find some examples on this page).
This is the best way I have found and I got it directly from the sqlite website:
<?php
$db = new SQLite3('sqlite3db.db');
$results = $db->query('select * from db');
while ($row = $results->fetchArray()) {
var_dump($row);
}
?>
Also if you're looking to include the php results into html like a table cell or something, go as such:
$results = $db->query('select * from db');
?>
<?php while ($row = $results2->fetchArray()) {?>
<td><h4><?php echo $row['id'];?></h4></td>
<?php } ?>
Related
I need some help with my PHP. I have a trouble with fetching the data from the database. I have hired a PHP developer who did not do his job properly that he have messed up the code which make it don't work so I need some help to fix the issue to get it working again.
When I try this:
//open the database File
$db = new SQLite3('myChannel.db');
if(!$db)
{
echo $db->lastErrorMsg();
}
else
{
$channel_name = $_GET['channels'];
$sql ="SELECT channel, title, start_date, stop_date, description FROM programs WHERE channel='$channel_name'";
$results = $db->query($sql);
while ($row = $results->fetchArray())
{
print_r($row);
}
What happen with the code is it will not fetching the matched data from the database as it will not do anything. I think there is something wrong with the $sql variable.
What I'm expecting to do is I want to look for data in the database where I use the variable called $channel_name, then I want to fetch the matched data to output them in my PHP.
Can you please help me how I can fetch the matched data in the database?
Try this code based on the SQLite PHP docs
class MyDB extends SQLite3 {
function __construct() {
$this->open('myChannel.db');
}
}
$db = new MyDB();
if (!$db) {
echo $db->lastErrorMsg();
} else {
$channel_name = $_GET['channels'];
$sql = "SELECT channel, title, start_date, stop_date, description FROM programs WHERE channel='{$channel_name}'";
$results = $db->query($sql);
while($row = $results->fetchArray(SQLITE3_ASSOC) ) {
print_r($row);
}
}
I changed a few things. I turned your database connection into a class, and I changed your while to include SQLITE3_ASSOC.
Warning: OP's code and as a result this answer has code that is
vulnerable to SQL Injection!
I'm working on a symfony2 project. when i insert data with french accents like (é), it is inserted as shown in this image:
when i try to retrieve this data using a php file to use it in android app, i use this code:
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','adproject');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('unable to connect to db');
$sql = "select * from actualite";
$result = array();
$res = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($res)){
array_push($result,
array('id'=>$row[0],
'login'=>$row[1],
'password'=>$row[2]
));
}
echo json_encode($result);
mysqli_close($con);
?>
I get nothing because of the accents in the database. Any suggestion?
I suspect you'll need to go through this Configuring the Database section and setting up for UTF8:
http://symfony.com/doc/current/doctrine.html#configuring-the-database
Let us know if you've already done that.
Hey I am iOS developer I am trying to create simple JSON output from my website. I found good start link and here is some explanation how to do it.
So I've created accounts.php file and put it to my public_html folder
<?php
include_once("JSON.php");
$json = new Services_JSON();
$link = mysql_pconnect("localhost", "user", "pass") or die("Could not connect");
mysql_select_db("iglobe") or die("Could not select database");
$arr = array();
$rs = mysql_query("SELECT * FROM users");
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
Echo $json->encode($arr);
?>
Of course I use my user and password and I pointed my just created database ob my end.
so when I try to request my file so http//mywebsite.com/accounts.php there is no data.
I tried to use google chrome and Postman so it says No response received when I switch to JSON. For HTML there is no info in Postman.
My question how can I test it? even if I use Echo(123) before include_once("JSON.php"); line there is no 123 on html page.
I tried to test PHP with only this code:
<?php
phpinfo();
?>
and it works. I have PHP Version 5.4.32
First of all, simply use PHP's function json_encode($arr). It does exactly what you are asking for and is pretty much included in every version of PHP that I can think of.
Documentation
Also, I am not sure if this is the issue, but you may want to change Echo ==> echo. This is generally convention at the very least.
SUPER IMPORTANT
Finally, DO NOT USE mysql extension. Its is dangerous, may not work correctly, and has security vulnerabilities. Use mysqli or PDO.
Matrosov -
You are very close. Use the json_encode function to output your code via the PHP manual. Also consider using mysqli instead of mysql for your database connection as it has been better support for modern MySQL servers.
http://php.net/manual/en/function.json-encode.php
http://php.net/manual/en/book.mysqli.php
<?php
include_once("JSON.php");
$link = mysqli_connect("localhost", "user", "pass") or die("Could not connect");
$link->mysql_select_db("iglobe") or die("Could not select database");
$arr = array();
$rs = mysql_query("SELECT * FROM users");
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
echo json_encode($arr);
?>
Lately I've been struggling with a problem and I have no idea what I'm doing wrong.
I want to add information to a database in SQLite3 from a PHP script. To achieve that since I am new to this I created a database, put some info in there and with the following script I am able to read the data:
<?php
$db = new SQLite3('mydatabase.db');
$results = $db->query('SELECT * FROM temps');
while ($row = $results->fetchArray()) {
var_dump($row);
}
?>
Next step I tried to ADD info to the database from PHP with this script:
<?php
$db = new SQLite3('mydatabase.db');
$db->exec("INSERT INTO temps (zone,temperature) VALUES ('maia',66)");
echo "something";
}
?>
This script does NOT add anything to the database and the echo is displayed.
After reading a bit I changed the last script to something like this:
<?php
try
{
$db = new PDO('sqlite:mydatabase.db');
$db->exec("INSERT INTO temps (zone,temperature) VALUES ('maia',66)");
echo "Row Inserted\n";
}
catch(PDOException $e)
{
print $e->getMessage();
}
?>
There is no exception displayed, the echo "Row Inserted" is displayed and nothing is added to the database.
Can someone give me an hint about what am I missing please?
Pretty much appreciated.
So I've been learning for about 3 months now and am currently using very old procedural techniques and the deprecated mysql extension in my code. So time to take a step forward, ditch my procedural ways and get into object oriented / prepared statements...
This is very basic but I guess everyone has to learn some day. I'm trying to get retrieve and simple dataset from mysql database..
so far I have my connection:
$useri = new mysqli('localhost', 'useri', 'xxx','yyy');
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
I get no errors so I assume this works, and I have my query:
$test_query = "SELECT * FROM t";
$test_query = $useri->real_escape_string($test_query);
echo $test_query;
if($result = $useri->query($test_query)){
while($row = $useri->fetch_object($result)){
echo $row->id;
}
$result->close();
}
$useri->close();
However I get no results... so, 2 questions:
a. what am I doing wrong? and
b. anyone recommend any good tutorials apart from the php manual for this stuff?
Thanks :)
This works for one of the table i have in my db:
$useri = new mysqli('localhost', 'useri', 'xxx','yyy');
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
$test_query = "SELECT * FROM t";
$test_query = $useri->real_escape_string($test_query);
if($result = $useri->query($test_query)){
while ($row = $result->fetch_object()) { //only this is changed
echo $row->id;
}
$result->close();
}else{ //check for error if query was wrong
echo $useri->error;
}
$useri->close();
make sure that you have a space after *
$test_query = "SELECT * FROM t";
check this tutorial
http://net.tutsplus.com/tutorials/php/php-database-access-are-you-doing-it-correctly/
http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/