PHP with SQLite in TItanium - php

Hi all am using Titanium studio for Desktop Development in PHP,
when i tried to connect with sqlite3 it shows an error msg as
uncaught exception with message unable to open databasefile
where the code supplied is
$db = new SQLite3('sample.db');
and another trouble is I was unable to acess $_GET, $_POST, and $_REQUEST in php Desktop Development using Titanium

I was pulling my hair with this. Seems like you can use PDO connections for mysql and also use SQLite3 for SQLite. (at least for SKD 1.2.0.RC4 , not sure on any other previous versions)
Check what the phpinfo(); function returns, try to find this string "sqlite3 SQLite3 support => enabled". If you phpinfo returns this, it means you can use the SQLite3 class.
If it does work, take a look here http://www.php.net/manual/en/sqlite3.open.php and let us know how it goes for you. For me this worked just fine and I can open, insert and select without problems. Try to use at first a full path for your db just for testing.
As for the $_GET, $_POST, and $_REQUEST, you are right, they are not available unless you load php pages with get params in the same string ("login.php?user=me&pass=secret"), in that case, the $_GET variable does become available. Not the best way to go but its an option. You can also create a data class and use it for holding and passing data among php pages, during your execution time.

You should use Titanium.Database.open to open db:
var db = Titanium.Database.open("Path to your db");

Related

PHP runs a storing procedure on a database, how can I see concrete parameters passed into without altering source code?

Here is php code:
$query = Container::get('db_connection')
->prepare('sp_Util_SearchMemberByKeyWord');
$query->bind('szSort', $searchcol, DatabaseTypes::NVARCHAR(100))
->bind('szKeyWord', $searchstr, DatabaseTypes::NVARCHAR(100))
->bind('partnerid', $partnerid, DatabaseTypes::INT)
->bind('hideDisabled', $hideDisabled, DatabaseTypes::INT)
->bind('hideDemo', $hideDemo, DatabaseTypes::INT)
->bind('hideByLmtdAccountManagerUserID',
$hideByLimitedAccountManagerUserID, DatabaseTypes::NVARCHAR(100))
->execute();
I need to see exact values of the params passed into the storing procedure. There is no way I can debug or alter source code. Google Chrome (or any other browser) seems to be only one option. I also got access to the appropriate database. Can I capture those values somehow using built-in Chrome functionality or some advanced features of SQL Management Studio?
P.S. I am not php dev, not at all.
The simplest way to see variables in PHP is using the var_dump(); and exit;
Use it before your code.
For example:
var_dump($searchcol); exit;

Database connection script works in PHP 5.3 but not in 5.5

I have developed a web based member management tool for my sailing club few years ago and used some free script (a class) that handles database connection and operations on database. The class (connection.php) is invoked by the following code in a required file "settings.php":
$db = new db ( DBUSER, DBPASS, DATABASE, HOSTNAME );
and then is used throughout the site for handling database related requests. It is working fine in PHP 5.3 but due to some security concerns the site has to be upgraded to min 5.4. Unfortunately the script in PHP 5.4 fails to connect to database. I will include the script below (for brevity I show only couple of functions in the class that should give the idea):
// ==================================================================
// The Main Class
class db {
// ==================================================================
// DB Constructor - connects to the server and selects a database
function db($dbuser, $dbpassword, $dbname, $dbhost)
{
$this->dbh = #mysql_connect($dbhost,$dbuser,$dbpassword);
if ( ! $this->dbh )
{
$this->print_error("<ol><b>Error establishing a database connection!</b><li>Are you sure you have the correct user/password?<li>Are you sure that you have typed the correct hostname?<li>Are you sure that the database server is running?</ol>");
}
$this->select($dbname);
}
// ==================================================================
// Select a DB (if another one needs to be selected)
function select($db)
{
if ( !#mysql_select_db($db,$this->dbh))
{
$this->print_error("<ol><b>Error selecting database <u>$db</u>!</b><li>Are you sure it exists?<li>Are you sure there is a valid database connection?</ol>");
}
}
When trying to display a page in PHP 5.4 I get the following output:
dbh = #mysql_connect($dbhost,$dbuser,$dbpassword); if ( ! $this->dbh ) { $this->print_error("
Error establishing a database connection!
Are you sure you have the correct user/password?
Are you sure that you have typed the correct hostname?
Are you sure that the database server is running?
"); } $this->select($dbname); } // ================================================================== // Select a DB (if another one needs to be selected) function select($db) { if ( !#mysql_select_db($db,$this->dbh)) { $this->print_error("
Error selecting database $db!
Are you sure it exists?he rest of the pages
Are you sure there is a valid database connection?
My aim is to rewrite the class db without touching the rest of the pages.
I was trying to use mysqli instead of mysql but that made no difference. I'm using mainly procedural php in my programming and am not to good with object oriented.
My question is: what approach should I take to solve my problem. Could you put me in some direction...
This function appears to be deprecated as of PHP 5.5
http://php.net/manual/en/function.mysql-connect.php
If you have access to the php.ini file after the upgrade to 5.4 I would check to make sure the extension you are trying to use is unchecked in the windows extensions settings. If you pay for hosting, their tech support may be able to help if you have a lot of patience.
Thanks everybody for your contributions.
I've identified a simple (but major) problem with file connection.php containing Class db. It was using short version of php identifier: <? rather than <?php, which was causing fatal error of not finding the Class.
With this out of the way I am back to my original issue of deprecated functions not working correctly in php 5.5 but need to do more research before (eventually) asking another question.
Thanks again.
#Qchmqs Jan 8 at 7:28
you should be using PDO anyway
No!
You should be using PDO OR MySQLi
Balance please! there are plenty of well learned folk who advocate MySQLi for some situations, its a matter of judgement etc.

BIRT 4.5 - Disable saving connectstring inside report.rptdesign, PHP to assign connectstring

I am using Birt 4.5 and PHP/MYSQL.
I am able to run birt reports with php. I have enabled tomcat and copied 'birt-runtime-4_5_0/WebViewerExample' to tomcat/webapps and renamed it to birt.
So I can run birt viewer with php;
<?php
$fname = "report/test.rptdesign&__showtitle=false";
$dest = "http://localhost:8081/birt/frameset?__report=";
$dest .= $fname;
header("Location: $dest" );
?>
Above code is working fine. But report connectstring already saved in test.rptdesign file.
I want to remove DB login credentials from test.rptdesign file and assign it while report open with PHP.
I have tried with report parameters. But all the parameters will display on browser address-bar.
Is there any secure way to do this? This is very important when we need to change the database location. It is very hard to change the data source of each and every .rptdesign file.
Thank You,
Supun
I don't believe using report parameters to handle a database connection is the right way. In addition to the address-bar problem you mentionned, it will cause unexpected issues: for example you won't be able to use this database to feed the dataset of another report parameter.
With Tomcat the best approach is to externalize the database connection in a connection pool: easy, robust, and reports might run significantly faster.
Alternatively the datasource can be externalized in a BIRT library (.rptlibrary) and shared across all report-designs: thus only the library needs to be updated when the database location is changing.
I agree with Dominique that sending the database parameters via the query is most likely an inappropriate solution - and you've not given any explanation of whether this is a requirement of the system.
But it is quite trivial to proxy the request via PHP and decorate the URL with the required parameters, something like...
<?php
$_GET['__showtitle']=$_GET['__showtitle'] ? $_GET['__showtitle'] : 'false';
$_GET['__report']=$fname; // NB this should be NULL in your code!
$_GET['dbuser']='a_db_user';
$_GET['passwd']='s3cr3t';
$qry=http_build_query($_GET);
$url="http://localhost:8081/birt/frameset?" . $qry;
// if its simply returning HTML, then just....
$fin=fopen($url, 'r');
while ($l=fgets($fin)) {
print $l;
}
exit;
If the returned content contains relative links the you'll need to rewrite the output stream. If the content type is unusual or you want to project other headers (e.g. for caching) to the browser, then you'll need to use Curl, capture the headers and relay them.

Request parameters in PHP on HHVM

I'm running HHVM 3.2.0 and trying to get access to GET and POST request parameters. The problem is, HHVM doesn't support access to PHP superglobals ($_GET, $_POST, $_SERVER, etc).
The only other way I know of getting access to request parameters in PHP is via the filter_input function, but is that really best practice (if I'm just using the raw filter)? It seems as though HHVM should support something cleaner than that. (What about Hack?)
HHVM absolutely supports superglobals in PHP code -- they're a really key part of PHP! The docs page you've linked to is simply wrong, and I've filed a bug to get it fixed.
In strict mode Hack code, superglobals are not supported; this cookbook example shows how you can access them via partial mode.
Seems like they added HH\global_get() to get global variables.
https://docs.hhvm.com/hack/reference/function/HH.global_get/
To put everything together
use namespace \Facebook\TypeSpec;
// get $_GET using global_get
function global_get_get(): darray<arraykey, mixed> {
$spec = TypeSpec\darray(
TypeSpec\arraykey(),
TypeSpec\mixed(),
);
return $spec->assertType(\HH\global_get("_GET"));
}

SQL debugging in large PHP app

I am using CodeCharge Studio to finish a large PHP application. This question isn't really CCS related, but a bit more general. I have a web form that is supposed to allow CRUD capabilities with a certain SQL Server table, but the Inserts keep failing without throwing any errors. What would be the best way to debug this?
When I'm having trouble with dynamically generated SQL queries, I typically echo out the query and try running that query on the console for the DB. Or alternatively, you could write a simple PHP function that writes out strings to a file, and that way you don't have to display the query directly on your page, but instead in a log file.
See what the actually query is and then try doing that query directly on the DB. Then you know whether it's a PHP issue or a DB issue.
Then go from there, depending on the outcome.
If the query looks OK, double check that the user running the query has insert rights to the database.
I've been caught out by that before.
You can monitor all sql queries in mysql as shown in this site, once you enable logging, run the query manually and see why its failing..this should be good starting point.
In addition to what's mentioned before, I can add my recent discovery:
trigger_error(print_r($your_var,1),E_USER_ERROR);
So you can output and debug your variable, even if it's a complex script with redirects, where simple echo would not help.
Dmitri.
You should try using FirePHP and log all the SQL to your Firebug:
An Example would be:
$sql = "SELECT * FROM table"
if (!mysql_query($sql)) {
// In un successfull log to FireBug
FB::error($data, "SQL error: ".mysql_error());
}
You can also implement the FB::error call from your own function, so you can later deactivate this behaviour modifying your function:
function log_error($data, $msg){
//dont forget to deactivate in case the application goes live!
FB::error($data, $msg);
}
if (!mysql_query($sql)) {
// In un successfull log to FireBug
log_error($data, "SQL error: ".mysql_error());
}
Most of the database connection classes in CodeCharge have a 'debug' flag which will automatically write all the page's database commands at the top of the page.
For example, in an old PHP project of mine, 'Common Files' the file 'db_mysql.php' (line 27):
public $Debug = 0; ## Set to 1 for debugging messages.
Change to '1' and publish that file. Load the web page. Change back and re-publish when done.
I've used this in CCS for PHP and ASP projects, and is likely in the other languages (not sure if or where to find in .NET projects).

Categories