Possible to have drupal read from a sql database? - php

I have a site under drupal (using mysql) but need it to read from a external sql database and make quires/reports, maybe using views2?
Is is at all possible? I been looking for a solution to just read (not import, its thousands of entries...) from an external database for hours and not sure if its even possible.

Drupal can access external databases, assuming that they are one of the types Drupal supports.
I will refer you to this handbook page: How to connect to multiple databases within Drupal.
However, to actually access data in the external database, you will need to write custom code, probably in a custom module. Essentially, you will need to do something like:
// Set Database API to use the other database.
db_set_active('external_db');
// Query the database.
db_query("SELECT * FROM {your_table} WHERE condition = 'value'");
// Set the Database API back to the default db.
db_set_active('default');
Essentially, point the database to the external database, make your reads and writes, and switch back. If you forget to switch back, Drupal will crash as it's core functions will try to work with the non-Drupal database.

It's possible to define several database connections in your settings.php and use db_set_active to select which database to use.
If you want to do this with views, you might need to do some extra work and embed the view after setting the active database to the external one.

Without writing code, you should be able to create views of the secondary database using the Table Wizard module. However, given how Drupal 6 handles its database abstraction, this will require that the secondary database be the same type as the main Drupal database (eg, either both are MySQL or both are Postgresql).

Related

Wordpress - Using a database to update custom posts

I have a Wordpress site that utilizes a custom post type, call it CPT-1, that I created using JetEngine. Inside of CPT-1 are meta fields. Once that was setup, I did a bulk insert of data using Ultimate CSV Importer Pro and it put this information into CPT-1 and I could put each column of data into the meta fields I wanted to use. These fields are then used later in tables.
Is there a way to go around the CSV Importer part of this process and just pull from a database? In the long term, i'd like to make changes to certain posts and upload different posts while using CPT-1 but I don't think using a CSV every time will be easy or accurate. If I could just pull from a database that I make updates to, I can track those changes easily and manage it.
I have database experience but not so much with Wordpress databases. What tables would I have to pay attention to if I were to go down this route?
Wordpress uses MySQL as a backend, so there is no reason you can't just insert the data directly. You'll need to get the credentials Wordpress uses to connect to the database, and then connect yourself, probably from your own custom PHP script.
I am generally skiddish doing things like you described because Wordpress is a complex piece of software and I don't have a lot of awareness of what it is doing behind-the-scenes (nor do they really intend users to have such awareness, most functionality is hidden from the user.)
However, if you have been doing a CSV import, and you have tested it extensively, and it's working fine with that method, there is no reason you couldn't carry out this same thing with less manual work on your part via a PHP script.
I'm afraid I can't get much more specific in my answer because I don't have information about what exactly you did with the CSV.
A straightforward (but not super efficient) way of doing this would be a PHP script where you initiate a database connection to the database you update, and a second connection to the MySQL database, fetch a query of whatever rows you want to update (whatever you would normally be exporting via CSV) and iterate row-by-row and insert this data into the MySQL database. You can make this significantly more efficient by making a single prepared statement, and then executing it repeatedly with each row of values.
A more efficient way of doing it would be to pull the data into your PHP script and then format it as a single query which you could then add into the MySQL database.
If you already have CSV importing working, you could even do a "lazy" solution where you just write a PHP script that generates a CSV and then feeds it into MySQL and imports it the same way your other program was. It's hard for me to tell from what you said, which of these solutions would work. However, I have used all three solutions, depending on what I'm doing and what kind of error handling I want.
In general, if errors happen rarely-to-never you are probably better off with the single, bulk insert methods whether one query or PHP automating the export of a CSV and then passing it to be imported into Wordpress' MySQL database.

Setting Up a Connection to a Database from a Website and Running Queries

I am working on a project for a Database Systems class where we created a database and have to present it somehow. Our group had the idea to create an 'imdb' type website where various information about movies could be stored and presented to the user. We have our database created and are about to begin work on the front-end for the website.
We have some confusion among our group as to how to proceed. We understand that we need to use PHP to connect to the database with the username and password credentials, but from there:
Do you have to have the PHP connection code on every webpage, or just the home page?
What do the queries look like when communicating with the database to retrieve and display data?
Would a WordPress site be easier to use when working in this capacity as it's more or less a 'Proof of Concept' type project, not a fully fleshed out site?
As itoctopus said, every page will need to connect to the database. I have found the easiest way to accomplish this is to build your db connection in a "header" file (a file that is "included" in every php that needs access to the db).
Here are your answers:
Yes- you will need to have the connection on every page. Usually, the connection parameters and script is located in one file which you require on every page.
A query looks like:
SELECT `name` FROM `movies` ORDER BY ID DESC LIMIT 0, 10;
You may find some WordPress plugins which will do something similar. If it's a proof of concept, then I suggest you go this way (if such a plugin exists). It's also a good idea to read extensively on PHP/MySQL development or hire someone who can do that if you want to make that project real.

Can I create my own database from PHP?

I have a working PHP server. Now I want to use databases (MySQL or something similar). Is it possible to create a database from PHP?
I would like to emphasize that in my case I do not have any user-name and password which I can use to connect to MySQL server. I also do not have a control-panel where I could create a database or a table in an existing database.
ADDED:
I think SQLite is what I need. But if I try to create a new database from PHP I see that PHP tries to create a file in a directory which is different from the directory where my files are supposed to be. Then it reports that it is unable to create the database and I think it is because it tries to create in the directory to which I have no permission to write. Can I force PHP to create SQLite in a specific directory?
I'm assuming you want to create an SQL database without access to a stand-alone database server. You could use SQLite, which is a library that creates a lightweight database in a single file without separate processes.
It's not quite as efficient as a standalone database server, however, so if you need performance, use a proper database server. It's not an unreasonable requirement for a high-performance web app.
Mysql,no. SqlLite is a possibility, you only need write permissions on the filesystem.
www.sqlite.org/
I would like to emphasize that in my
case I do not have any user-name and
password to which I can use to connect
to MySQL server. I also do not have a
control-panel where I could create a
database or a table in an existing
database.
This question is confusing to me, because if you're using MySQL you should be able to create a database, with a username and password to connect, using their command line admin tool. That's also the way to create a database or a table as well.
Are you saying you don't have access to the admin tool? You don't have an account? If so, you'll need to ask for them from the person who does have such access.
An option is to set up an SQLite database in a directory outside of your htdocs folder. This is so people can't type in the name of the database file and download the entire database, severely compromising security.
If you are so inclined, you can even set up a layer of abstraction between PHP and the DBMS using PDO. Then, in order to create the database object, you would specify a DSN specific to SQLite write something like this:
$pdo_obj = new PDO('sqlite:/path/to/my_database.sqlite3');
and then query it like a normal PDO object using the PDO functions.
This method would better allow for easier migration to using a client-server DBMS once you can get one set up; it would just be a matter of copying the table structure and data over to the new database, and changing the DSN to something appropriate.
Creating a database through PHP is possible, but for that you need a connection to the database, which needs a username/password pair or some kind of authentication. Unless your database allows anonym logins or something similar, it's not possible.
Yes, just fire a create database sql query from PHP
http://dev.mysql.com/doc/refman/5.0/en/create-database.html

ODBC: when is the best time to create my database?

I have a windows program which generates PGP forms which will be filled in later.
Those PHP forms will populate a database. It looks very much like MySql, but I can't be certain, so let's call it ODBC.
And, yes, it does have to be a windows program.
There will also be PHP forms which query the database - examine which tables and fields it contains and then generates forms which can be used to search the database (e.g, it finds a table with fields "employee_name", etc and generates a form which lets you search based on employee name.
Let's call that design time and run time.
At design time, some manager or IT guy or similar gets to define the nature of the database and at runtime 1) a worker fills in the form daily and 2) management can extract reports.
Here's my question: given that the database is defined at "design time" (and populated at run time), where and how is best to do so?
1 I could use an ODBC interface from the windows program, but I am having difficulty finding something good to work with Delphi. Things like ADO and firebird tend to expect you to already have a database and allow you to manipulate it, but I can find no code example of how to create a database and some tables, so ...
2 I could used DOS commands from Delphi in my windows program. I just tried and got a response to MySql --version, but am not sure if MySql etc are more interactive. That is, can I use a script file or a very long stacked command with semicolons and returns separating? e.g 'CREATE DATABASE db; CREATE TABLE t1;'
3) Since the best way to work with databases seems to be PHP, perhaps my windows program could spit out a PHP page which would, when run in a browser, create the database.
I have tried to make this as uncomplicated as I can, but please feel free to ask questions. It may be that there are several valid ways, but there is probably one 'better' solution in terms of ease of implementation or maintenance.
Better scratch option 3. What if the user later wants to come back and have the windows program change the input form? It needs to update the database too.
Creating a database is usually a database administrator task. Unless it is a local database, maybe an embedded one, the user would need to know where and how create the database on the remote server, and she can have no clue about it. Where to store the database files? Which disks are available? And there could be many more parameters to set (memoery buffers size, etc.), users to be created and so on. And also you need very elevate privileges to be able to create a database, not something you give to average users or applications.
Thereby usually you ask the database administrator to create your database/schema, he will give you the credentials you need to connect, and then your application (or its setup) will create and initialize the needed objects (tables, etc.). Creating table (and other object) is usually as simple as running "CREATE TABLE...." statements. Just remember SQL takes one command only, if you need to run several commands you have to send them one after another yourself, although there are Delphi components which are able to split a script in commands and run one after another.

Tracking database usage using php and Mysql

I am designing a php mysql webapp. In that, a database usage log has to be maintained , which will record
1.User ip
2.Timestamp
3.operation performed(Add,edit or delete)
4.Colmns changed
5.rowid
6.old value
7.new value
Please suggest any example scripts.
If you are using your own database class, just add this logging to that class. Otherwise you will have to make a database class to do it and start using that class instead of using mysql_query directly.
There are many DB profilers available. You can configure that and use them. You can use XDebug.
Another link : http://particletree.com/features/php-quick-profiler/

Categories