Firebird:How to show table structure in html page? - php

I try to use the code below to show table structure in html page:
<HTML>
<HEAD>
<TITLE>PHP + Firebird / Interbase test (connection)</TITLE>
</HEAD>
<BODY>
<H3>FB Connect test.</H3>
<?php
// DB definition of account
define("DBNAME","xx.xxx.xx.xxx:D:\DATABASE\OCS DATA.FDB"); // data bsse name
define("DBUSER","USER"); // user name
define("DBPASS","USER"); // password
// DB connection
$dbh = ibase_connect(DBNAME,DBUSER,DBPASS);
echo ibase_errmsg();
if ($dbh == FALSE) {
echo 'could not connect to DB<BR>';
} else {
echo 'success to connect to DB<BR>';
}
$ibsql = "SHOW TABLE DOC_TO";
echo ibase_errmsg();
$result=ibase_query($ibsql);
echo $result;
?>
</BODY>
</HTML>
but why it just show the result as "success to connect to DB"?

Firebird does not seem to have a SHOW TABLE or SHOW TABLES command which are specific to MySQL.
The documentation on the IBphoenix web site states that the SHOW TABLES command is only available in the isql command line tool and nowhere else.
In MySQL, you can do SHOW TABLES. You
can use the same in Firebird's isql
command-line tool, but nowhere else.
It goes on and provides the following SQL code as a roughly equivalent solution:
SELECT RDB$RELATION_NAME FROM RDB$RELATIONS;
This query will show you both system
and user tables. To select user tables
only, use this:
`SELECT RDB$RELATION_NAME FROM RDB$RELATIONS WHERE RDB$SYSTEM_FLAG = 0;
Maybe you can get something equivalent from what you were trying to accomplish by querying the RDB$RELATIONS table (or maybe some other runtime information tables provided by Firebird). See also Lorenzo Alberton's post on extracting META information from a Firebird database.

Note than in PHP you should use single quotes $reqest = 'select ...'; otherwise the $ symbol will be translated as PHP variable sign.

Related

Pass variable data from PHP to shell_exec command

I have a script that clones tables on to uniquely named MySQL databases from a master sql dump file . Each account has their own database, but the table structure is the same for all accounts. My solution was to dump the master database table and then through PHP shell_exec, run the MySQL controller cmd (mysql) to populate the newly created database with default tables.
At Issue: The process works but only when I hard code the accounts unique database name in the master sql dump file.
However, "USE acct_dbID" line inside the master sql file needs to be dynamically set at runtime.
Here is the code:
include('.dbase_credentials'); //constants for connection object
//using PHP built in connection class mysqli
$mysqli = new mysqli(DB_HOST,DB_UNAME,DB_UPWORD,DB_NAME);
if ($mysqli->connect_errno){
echo "Failed to open database connection: ".$mysqli->connect_error;
exit();
}
$dbID=$varNum; //variable, number ID generated earlier in the account setup process
//create database, doesnt return a resultset, no need for object var here
if ($mysqli->query("CREATE DATABASE acct_".$dbID) === TRUE){
//if dbase was created, clone the tables
$res = shell_exec('mysql -u dbaseUser --password=`cat /path/to/pass` -e "source /path/to/master_tables.sql" acct_'.$dbID);
//provide some UI feedback, shell_exec returns null on failure
if ($res!=null){
echo "The tables were cloned!";
}else{
echo "The cloning process failed!";
}
}else{
echo "no database created.";
}
So again, master_tables.sql needs variable data passed it at runtime so the "USE acct_dbID" can be specific to each new account.
Any thoughts are appreciated.
rwhite35
knittl had the right direction for what I need to do. See the comment conversation above. Thanks knittl.
Here is the new code block using multi_query and file_get_contents.
if ($mysqli->query("CREATE DATABASE acct_".$dbID) === TRUE){
$acct="acct_".$dbID;
$query = "USE ".$acct.";";
$query .= file_get_contents('/path/to/master_table.sql');
$result=$mysqli->multi_query($query);
}
Now account can be variable and multi_query will run each query statement in the string. I probably lose some efficiency but gain the ability to clone tables for each new account. The only additional edit made was to the master_table.sql. I removed all the comments that the dump process adds.
Thanks,
rwhite35

Can't access the SQLite database with MAMP and PHP

I have been learning how to program websites lately and the time has come for me to add a database. I have in fact already successfully created a MySQL database and interacted with it with PHP.
My problem is I can't seem to access a SQLite database file with it. I am using MAMP to host locally for now. Here is a snippet of the code I am using to access the db and find and print out a value stored on it.
<?php
$dbhandle = sqlite_open('/Applications/MAMP/db/sqlite/Users');
if ($dbhandle == false) die ('Unable to open database');
$dbquery = "SELECT * FROM usernames WHERE username=trevor";
$dbresult = sqlite_query($dbhandle, $dbquery);
echo sqlite_fetch_single($dbresult);
sqlite_close($dbhandle);
?>
As you have access to the database (your code doesn't die), I'd say there's got to be an error later ;-)
Looking at your SQL query, I see this :
SELECT * FROM usernames WHERE username=trevor
Are you sure you don't need to put quotes arround that string ?
Like this :
SELECT * FROM usernames WHERE username='trevor'
Also, notice that sqlite_fetch_single will only fetch the first row of your data -- which means you might need to use sqlite_fetch_array or sqlite_fetch_object if you want to access all the fields of your resulset.

how to access firebird from Mysql?

dear all..
i have a DB which built use Mysql.
for this case, i want to access another server PC which have DB in firebird. i want to take some data inside that. Both of DB always connected, because i must get data every time. This is a scheduling data, so i must always connected to firebird from mysql.
But i have no experience to make connection between Mysql and firebird.
can you tell me how to do that?
what is the best way that must i choose, do some migration or convert or may make sync.?
any advance will be appreciate.thanks.
could I call the firebird 1st(use ibase_connect) then insert the data to mysql? i dont know how to insert data to Mysql DB after i get from firebird DB.
At this MySQL server can only connect to other MySQL servers. Not sure about Firebird, but if it's similar, then you need to do all you data handling in external application.
Take a look at MySQL Migration Toolkit. According to this post firebird is supported.
Cheers!
I suggest migrating firebird data to MySQL, this would help you avoid some pain in the ass :)
Unfortunately I have a project which has to be synced with some external firebird db...
may be use php can work.
<HTML>
<HEAD>
<TITLE>PHP + Firebird / Interbase test (connection)</TITLE>
</HEAD>
<BODY>
<H3>FB Connect test.</H3>
<?php
// DB definition of account
define("DBNAME","xx.xxx.xx.xxx:D:\DATABASE\OCS DATA.FDB"); // data bsse name
define("DBUSER","SYSDBA"); // user name
define("DBPASS","masterkey"); // password
// DB connection
$dbh = ibase_connect(DBNAME,DBUSER,DBPASS);
if ($dbh == FALSE) {
echo 'could not connect to DB<BR>';
} else {
echo 'success to connect to DB<BR>';
// DB dis connection
ibase_close($dbh);
}
?>
</BODY>
</HTML>

Problem using php ODBC functions from within a Joomla article

I am new to Joomla and new to php (wish I was so new in age too). I have installed joomla on a local apache webserver. I am trying to use some php code in a joomla article in order to fetch some data from a Sybase ASE 12.5 database. I installed sourcerer and started to try an ODBC connection using a system DSN (which I verified it is working):
{source}
<?php
echo 'This text is placed through <b>PHP</b>!';
echo '<p>';
echo '</p>';
$conn = odbc_connect('myDSN', 'sa', 'myPassword') or die('Could not connect !');
echo 'Connected successfully';
$sql = 'SELECT day, name FROM my_table where month = 1';
odbc_close($conn);
?>
{/source}
The above code doesn't do much, but this is how far I can get without problems. I refresh the joomla page and I see inside the article's text:
...
This text is placed through PHP!
Connected successfully
...
Seems ok, the connection obviously established (I verified this by stopping the sybase service and getting the "Could not connect" message). Then I added one more line, just below the $sql assignment.
$rs = odbc_exec($conn,$sql);
I refresh and ...I see nothing coming from the script (not even the "This text is placed through PHP!").
Obviously, I see nothing if I include code to echo the contents of $rs. I also tried this but in vain.
if (!$rs)
{exit("Error in SQL");}
Once I add the odbc_exec command, the entire script ceases working.
In php.ini I read:
; Windows Extensions
; Note that ODBC support is built in, so no dll is needed for it.
Do you have any idea what is going wrong?
UPDATE
Connecting to a MySQL database using code like this, works like a charm.
To answer your question
Is this the correct way to get data
from another database in to an article
or should I use some plug-in to do
that?
The correct way is to use JDatabase object which you get by JFactory::getDBO() or JDatabase::getInstance(), see Joomla JDatabase documentation.
$db = JDatabase::getInstance( $databasConfigArray );
$db->setQuery('your query');
$data = $db->loadObjectList();
Here is a good tutorial showing how to connect to multiple databases in Joomla, there is even source code for helper class.
Also look at this thread Connecting to 3rd party databse in Joomla!?

ms-access database to mysql database by php

How we can convert a ms-access database to mysql database by php…
or
How we can access a ms-access database by php..
For accessing a MS Access database on Windows through ODBC, see here.
How we can convert a ms-access
database to mysql database by php
You can export via an ODBC connector or (if you don't have too many tables) you can export your data to a text file and then import it into MySQL (after creating the tables manually) via LOAD DATA. Right-click on the tables and choose Export for available options.
For more detailed info on migrating from MS Access to MySQL, check out this great article from the MySQL Dev Team:
http://dev.mysql.com/doc/mysql/en/LOAD_DATA.html
How we can access a ms-access database
by php
You can do this easily right through PDO.
You can use this simple code to connect to Access database. I tried this code in PHP,working on Windows XP with XAMPP's Apache server, and using Access 2007 file as a database. Just create your access file and try this :
First go to Start menu > Control Panel > Administrative Tools > Data Sources(ODBC) > System DSN > Add.. > Microsoft Access Driver(.mdb,.accdb) and show your access file. Give name to the connection.
Then write in your *.php file this code:
`
<?php
$host= "host_name";
$user= "user_name";
$pass= "password";
$db_connect=odbc_connect($host,$user,$pass); //connect to access file as database
if (!$db) //In case if you didn't connect , you'll get this error message
{
echo "Can't connect";
exit;
}
$query = "SELECT * FROM table_name"; //pulling data form Access file
$row = odbc_exec($db, $query);
while(odbc_fetch_row($row)
{
$row1 = odbc_result($row,1);
$row2 = odbc_result($row,2);
$row3 = odbc_result($row,3);
echo $row1." ".$row2." ".$row3."<br>"; //watching if data is taken correctly
}
?>
And then you can insert that rows into sql database by adding this code into the while loop :
<?php
$db="MySQLdatabaseName";
$db_connect= mysql_connect($host,$user,$pass);
mysql_select_db($db, $db_connect);
$insert_into_MySQL = "INSERT INTO table_name($row1,$row2,$row3)
VALUES('".$row1."', '".$row2."', '".$row3."'); ";\\These are 2 lines to be
mysql_query($insert_into_MySQL ); \\added to the while loop
?>

Categories