Connect to old mysql server (4.0.20-standard) from php7 - php

I want to connect to old mysql server (Server is 4.0.20-standard) with php7. Upgrade mysql server or downgrade php is not an option.
I'm doing this: $conn = mysqli_connect($local,$user,$pwd,$db,$port);
But i'm getting this error: Warning: mysqli_connect(): Connecting to 3.22, 3.23 & 4.0 is not supported. Server is 4.0.20-standard.
Is possible do that?

The mysql native client(mysqlnd) which has been the default mysql driver since php 5.4 does not support old mysql servers, if you still want to connect to old versions of mysql servers, you should use another mysql driver libmysqlclient, which is needed to be compiled with php.
Here is the introductions of these two drivers:
https://dev.mysql.com/doc/apis-php/en/apis-php-mysqlinfo.library.choosing.html
I recommend you to build your mysqli with libmysqlclient and build your pdo with mysqlnd which is the default one and is recommended by official, so that you can connect to both versions of mysql servers.

Solution (didactic solution, do not use this on production environment):
Step 1: Install putty.exe + plink.exe in php7 machine. Step 2: I created these methods to do all the job for you/me. Example1:
$query = "SELECT * FROM user";
$rs = select($query);
if($rs)
for($index = 0; $index < count($rs);$index++){
//ALWAYS use this. Now u can use that array like a normal resultset $rs["lalala"]
$row1 = $rs[$index];
//use this like usual
echo $rs["username"];
}
Example2:
$query = "SELECT * FROM user";
$rs = select($query);
if($rs)
for($index = 0; $index < count($rs);$index++){
//ALWAYS use this. Now u can use that array like a normal resultset $rs["lalala"]
//$row1 = $rs[$index];
//now you didn't the line above...
echo $rs[$index]["username"];//you'll need something like $rs[0]["username"]
}
For update/delete/insert you can use something similar to the insert method. Example 3:
if(m_insert("insert into user (name,pwd,email) values ('kkk', 'hahaha', 'email#lala.com');")){
echo "</br>true</br>";
} else {
echo "</br>false</br>";
}
Remember1: You need to change the method variables (host, database, username, password, etc)...
Remember2: Change the mysql credentials at this line $commands[0] = 'mysql -u username -pPASSWORD...'

Related

odbc last insert id for php script

I'm trying the below script (script is in PHP, database is ODBC connection to db2 for as400) but I'm having issues due to the db2_last_insert_id being an unknown function.
I need to use the odbc setup for this script, and all other odbc functions work, but I can't find a function in ODBC that replicates the db2_last_insert_id functionality
What is the best way for me to grab the id of the inserted row within the script itself?
if($DB2connPROD){
$insertTable = "INSERT INTO testing_insert_php (name) VALUES ('Temp Name')";
$stmt = odbc_exec($DB2connPROD, $insertTable);
$ret = db2_last_insert_id($DB2connPROD);
if($ret) {
echo "Last Insert ID is : " . $ret . "\n";
} else {
echo "No Last insert ID.\n";
}
odbc_close($DB2connPROD);
}
Use VALUES identity_val_local() as your query. It will return a resultset with a single row and column.
You can't mix and match DB access libraries. If you are using ODBC, then you have to use ODBC for everything associated with that connection. You can't use ODBC to open a connection, and then DB2_ to operate on that connection.
In fact, both IBM_DB2 and PDO_IBM require a DB2 client, and the only one that works with DB2 for i is found in the DB2 Connect product. Unless the web server is running on IBM i itself.

pdo_oci driver not found but is listed in available drivers

I am trying to connect to an Oracle Database 11g XE running on windows from a remote Ubuntu 14.04 server using PDO_OCI in php. I followed this guide http://gist.github.com/tassoevan/10392954 (numerous times) and keep getting the following error:
could not find driver
However when I call PDO::getAvailableDrivers() it gives me this:
Array ( [0] => mysql [1] => oci [2] => sqlite )
Note: oci wasn't there before I followed the guide in the link above.
All of the other tutorials I could find were basically the same thing.
I have the Oracle instant client installed (version 11.1). The latest version was not even getting the driver to show up in the list of available drivers.
Also I know that the driver which is used in this method is older but there are no step by step guides on how to get it working with the lastest one. I am new to Oracle Databases as well as php so step by step help would be greatly appreciated.
UPDATE:
I figured out the driver problem. It was in my php code, and something very silly. I am posting for the benefit of others.
My original code had "OCI" in capital letters when setting the connection.
$tns = "
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = myIP)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = XE)
)
)
";
try{
$pdo = new PDO('OCI:dbname='.$tns ,'username','password'); // Capital 'OCI' here is WRONG so changed to 'oci:dbname='
} catch(PDOException $e){
echo $e->getMessage();
}
print_r(PDO::getAvailableDrivers());
However, I am having problems querying the Database. It seems nothing is happening. I have allowed the port 1521 in the Windows firewall for both incoming and outgoing. Any suggestion?
Here is my php code:
$stmt = $pdo->prepare("SELECT * FROM `PDO`;");
$stmt->execute();
$result = $stmt->fetchColumn(0);
echo $result;
The table PDO only has one column with one row.
Does the Oracle database need to be configured to allow for remote connections?
There were two problems with my PHP code.
1) The way the PDO connection was initiated was incorrect ... see update in original post. That solved my driver problem.
2) Oracle doesn't seem to like the backtick character, unlike MySQL. So I had to remove them from my query to look like this
$stmt = $pdo->prepare("SELECT * FROM PDO"); // Originally was SELECT * FROM `PDO`
$stmt->execute();
$result = $stmt->fetchColumn(0);
echo $result;
This solved my database querying problem.

PHP to SQL Server without ODBC or MSSQL support

I'm in a situation where my Windows hosting has PHP support, but the PHP is not configured to support ODBC or MSSQL. I can't get them to change that, so I'm wondering if there's a way to connect to SQL Server through other means - maybe including some files that provide the functions that I'd need?
Leaving it up here in the hopes that it will make it easier for other people to get around this type of limitation.
Copied here for completeness:
<?php
$db = new COM("ADODB.Connection");
$dsn = "DRIVER={SQL Server}; SERVER={SERVER};UID={USER};PWD={PASS}; DATABASE={DB}";
$db->Open($dsn);
$rs = $db->Execute("SELECT * FROM table");
while (!$rs->EOF)
{
echo $rs->Fields['column']->Value."<BR>";
$rs->MoveNext();
}
?>

Warning/Error:- No tuples available at this result index PHP-APACHE-ODBC-MYSQL-CENTOS

I have PHP sample code which will fetch the data from the MYSQL database through ODBC driver on Linux(CentOS) machine.
I have created DSN and same able to connect through following command
isql -v
But when i try to same DSN through PHP code i am getting "No tuples available at this result index" due to which unable to read the data from database through PHP APACHE configuration.
If anyone provides the solution,It will more helpful for me to proceed further.
Below are my sample code and other details,Please correct if anything wrong on below configuration details-
Below is sample PHP code:
<?php
$conn = odbc_connect("DSN", "username", "password");
$sql = 'select * from tablename';
$rs = odbc_exec($conn,$sql);
echo "<table><tr>";
echo "<th>User Name</th></tr>";
while(odbc_fetch_row($rs)) {
$user = odbc_result($rs,"fieldname");
echo "<tr><td>$user</td></tr>";
}
odbc_close($conn);
echo "</table>";
?>
Below is odbc.ini file:
[DSN]
Description = MySQL ODBC Database
DRIVER = MySQL
TraceFile = /tmp/odbcerr.log
SERVER = 127.0.0.1
PORT = 3306
USER = username
PASSWORD = password
DATABASE = database
OPTION = 3
SOCKET = /var/lib/mysql/mysql.sock
Below is odbcinst.ini file:
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/libmyodbc5.so
Setup = /usr/lib/libodbcmyS.so
FileUsage = 1
UsageCount = 3
The comments in the documentation for odbc_connect suggest that this can be caused by using an incorrect cursor type. Try each of the three possible values as the fourth argument: SQL_CUR_USE_IF_NEEDED, SQL_CUR_USE_ODBC, or SQL_CUR_USE_DRIVER.
I am very curious as to why you're using ODBC to connect to MySQL. PHP has many better ways to read from MySQL, including the excellent PDO class (which itself can connect via ODBC if needed).

PHP with Oledb

Can I use PHP with Oledb connection?
As far as I know database connection as provided by PHP extension are all odbc.
You can use ActiveX Data Objects (Microsoft's OLEDB ActiveX layer) in PHP-Win without any third party extension as such:
$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
// Microsoft Access connection string.
$conn->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\inetpub\wwwroot\php\mydb.mdb");
// SQL statement to build recordset.
$rs = $conn->Execute("SELECT myfield FROM mytable");
echo "<p>Below is a list of values in the MYDB.MDB database, MYABLE table, MYFIELD field.</p>";
// Display all the values in the records set
while (!$rs->EOF) {
$fv = $rs->Fields("myfield");
echo "Value: ".$fv->value."<br>\n";
$rs->MoveNext();
}
$rs->Close();
Look at the ADOdb Library for PHP extension. I've never used it, but it seems to be compatible with OLEDB providers.
maybe......
found an article on it.
found the PHP extension for it.
Don't know anything about it. Best of luck.

Categories