Update Access Database from PHP script? - php

I'm relatively new to PHP and databases. Currently I'm working with an existing Access database and am able to read and display the data just fine. However, I'm wanting to update a user record through an email verification link. I have the server send an email with a link of www.domain.com/verify.php?userID=#
I am able to read this GET variable just fine, but I'm completely lost as to how I update the record. Everything I search for is for updating MySQL databases whereas I'm using odbc.
Does anyone know how I could set this up?

something like this:
odbc_exec($conn, "UPDATE table SET field=value"); // $conn is your connection identifier

You need to use a ODBC database client library in PHP to connect to a ODBC datasource. For example you can use PDO with the driver ODBC and DB2 Functions (PDO_ODBC).

Related

Unable to read an MS Access Table (linked to an ODBC Data Source) using PHP

I am working on an application in which the data from an ODBC enabled Data Source is imported to an MS Access 2007 Database using a Linked Table. The tricky part is that every time a Link Refresh is made (or the linked table is opened) the ODBC Data Source prompts out a Window where 4 different parameters (username, password, servername, workgroupname) have to be input.
While this is possible manually, I find no way to read this linked Table programatically.
I am using a PHP Script to read the linked table. The Execution of PHP Script is halted indefinitely when I try to read this linked table. My DSN is properly configured and can successfully read the data in all other cases including those linked tables in which the data source doesnot require any input parameters/credentials. However, it fails in this case.
In a nutshell, my system is PHP<-->MS Access (Linked Table)<--ODBC Data Source. The PHP Script aims to read the updated data from the ODBC Data Source using the Linked Table. For some reasons, PHP cannot directly interface with the ODBC Data Source, so I am using MS Access as an intermediary.
The odbc_connect function of PHP allows only DSN Name, username, password, cursor_type as parameters. I am not sure how do I input the 4 parameters (username, password, servername, workgroupname) as required by the ODBC Data Source to which my MS Access table is linked.
I am using PHP 5.4 on Windows 7 with MS Access 2007.
I request the developer community to help me with necessary pointers on this. Any suggestions for Workaround is also welcome.
Thanks in Advance.
if you are using double ODBC: PHP > ODBC > MS ACCESS > ODBC > SOURCE.. you have following options.
Cut off the "Access" middle man and connect directly.
if you want to use MS Access, use "FILE DNS" to connect to the linked tables from MS ACCESS where file DNS has all 4 parameters initialized. (This will eliminate MS Access asking you to enter the server information when refreshing linked tables)
Haven't tried this from PHP side but you can loop through the msysobjects table or database.tabledefinitions and update the link with your custom connection string.
Any way, the solution for your trouble would be. Make Access not to prompt for parameters when refreshing linked tables by either using file dns or save passwords.
Connect directly to the data source where linked table derives. If that is MS Access, SQL Server, MySQL, etc. use the appropriate, installed ODBC driver for that particular database.
Here is a PHP/MS Access DSN-less solution using PDO.
Also, see different connection strings which includes workgroup, user, and password parameters.
<?php
$database="C:\Path\To\database.accdb";
# open the connection
try {
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};
DBq=$database;Uid=Admin;Pwd=;");
$sql = "SELECT * FROM table1";
$STH = $dbh->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
echo "\n";
while($row = $STH->fetch()) {
# output query results
echo $row;
}
# close the connection
$dbh = null;
?>

phpmyADMIN or mySQL? I am a bit confused with PHP

Hey guys the code below is what I am trying to run. I am trying to run a practice MySQL server because I am going to host it on my schools free database which is MySQL
I am using phpmyADMIN but I am a bit confused because yall are saying I am not connecting to MySQL
so what is this code to create a database? Do I need to download MySQL or something? I thought phpmyadmin is the same syntax to create a database/tables/ and values? since I have MySQL turned on?
//creation of database
$sql= 'CREATE DATABASE project';
if(mysql_query($sql,$con)){
echo 'DB created succesfully';
}
else{
echo 'error creating DB' . mysql_errno();
}
PHPMyAdmin is a web-based software program created with PHP to help you administrate MySQL databases. It has its own internally built code to connect to your MySQL databases and perform standard functions such as insert, select, delete, update, etc.
PHPMyAdmin does not really have anything to do with you creating your own PHP program. You will still need to connect to MySQL (as PHPMyAdmin does behind the scenes). You can still use the same code that PHPMyAdmin generates for you for use in your own PHP programs, but you must already be connected to MySQL as a user with valid permissions.
In your PHP code it is important to note that you are using the mysql_* functions which have been deprecated and you should be using the mysqli_* functions instead.
Your code should actually look something like this:
//connect to db
$link = mysqli_connect("localhost","my_user","my_password");
//creation of database
$sql= 'CREATE DATABASE project';
if(mysqli_query($link, $sql)){
echo 'DB created succesfully';
}
else{
echo 'error creating DB' . mysqli_errno();
}
MySQL is the database server you connect to in order to execute queries such as CREATE DATABASE project. phpmyadmin is a PHP-based front end to MySQL to allow easier browsing and manipulation of the database.
Think of text documents on your computer. notes.txt is a text file, but you could open it with any text editor such as Notepad or TextEdit.
Your specific snippet of code is missing the database connection string, so something more appropriate might look like:
$connection = new mysqli('db_host', 'db_user', 'db_pass');
$sql = "CREATE DATABASE project";
$results = mysqli_query($connection, $sql);
//check for errors
Without connecting to the database, your SQL is all just strings of text with no meaning.
I tried a lot hoping to get a good job here
If you are logged into phpmyadmin, then the only code you can execute is just SQL statements. Like: create database my_db1

Use MySQL and PostgreSQL in the same PHP script

I'm trying to do some migrations from an old site to a new site. The old site uses MySQL and the new site uses PostgreSQL. My problem is I wrote a migration script in PHP that queries info from the old DB so that I can insert them into the new DB within that same script. The reason I need the script is I have to call other functions that do things and manipulate the data since the table columns aren't a one for one match so I can't just do a backup and restore type situation. I have a class for both DB's that I use.
The mysql queries work but postgres' don't. They get error messages saying pg_query(): 19 is not a valid PostgreSQL link resource in xxx
So is it possible to run them both in the same script? If I call the two scripts separately it works ok but I can't get the data from the old server to the new one.
I've looked everywhere and don't see many questions needing to use both DB's in one file.
Any help would be cool.
You are using the same variable for both resources and passing the mysql resource to the postgresql function

Performance: Query to external MYSQL or HTTP post to a PHP file on the server?

I'm developing an Android application that will need to query to an external MYSQL database. To do that, which is the best way to get the best performance and to trying to avoid overcharging of database:
Do a query directly to MYSQL Database, or
Do a HTTP POST to a PHP file and do a local query to MYSQL Database (note that using this way I can control the input and play with it before doing the query)
Here is a complete solution for you to connect your application to MySQL database by using JSON and PHP. You can do scripting in PHP page before executing query. Visit this page,
http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
Do a query directly to MYSQL Database is better. Let mySql to manage him self, he should be able synchronize what ever is needed and what ever is not.

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

Categories