odbc connection only working once? - php

I'm running the following PHP script to query an ODBC database:
<?php
$dsn = "ODBCconnection";
$db_user = "user";
$db_pass = "pass";
date_default_timezone_set('America/Chicago');
$conn = odbc_connect($dsn, $db_user, $db_pass);
if (!$conn)
{
exit("connection failed: " . $conn);
}
$query = "SELECT COUNT(column) as count FROM table WHERE criteria";
$test = odbc_do($conn, $query);
$count = odbc_result($test, "count");
echo "The count is: $count";
odbc_close($conn);
?>
The problem is not the query or the PHP that I can tell, it works exactly the way I want it to when I run it the first time it comes back with:
The count is: 750
However if I load the page again, it says:
Warning: odbc_connect() [function.odbc-connect]: SQL error: Failed to fetch error message, SQL state HY000 in SQLConnect in C:\xampp\htdocs\mailtest\scripts\testscript.php on line 11
connection failed:
If I load it again from that page, it just hangs up and times out after my PHP timeout expires. The only way to stop it is to stop and restart Apache, and then it acts the same way; works the first time, says it can't connect the 2nd, then just runs until it times out the 3rd.
I'm using "Oracle in OraClient 10g_home2" ODBC driver and running XAMPP 1.7.4. I've never run into something quite like this, it's like it just gives up after the first go, anyone ever seen this happen and know how to fix it?

Related

How to fix MySQL "Got an error reading communication packets" error

I have created some PHP code for my website to call a stored procedure in my database. This will get text from a table so that I can dynamically update the text on the web page without modifying it in code.
This part works very well so far. However, When I open the MySQL error log I see the following message printed:
Aborted connection 161 to db: 'dbname' user: 'username' host: 'localhost' (Got an error reading communication packets)
I have checked the firewall, which is inactive.
I have attempted using 127.0.0.1 instead of localhost in the PHP code.
I have cleared the results using "mysqli_free_result($result);", this did seem to remove one of the two errors I got per query.
I have checked the max allowed packet size, which is 16M.
I have un-commented the extension mysqli in the php.ini file.
PHP:
<?php
$servername = "localhost";
$username = "username";
$password = "password ";
$dbname = "dbname";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "Call StoredProcedure('primarykey',#OutValue);";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo $row["ColumnHeader"];
}
} else {
echo "0 results";
}
mysqli_free_result($result);
$t_id = mysqli_thread_id($conn);
mysqli_kill($conn,$t_id);
mysqli_close($conn);
?>
SQL:
CREATE DEFINER=`username`#`hostname` PROCEDURE `StoredProcedure`(IN PrimaryKey
VARCHAR(50), OUT Result VARCHAR(50))
BEGIN
start transaction;
select ColumnName
from dbname.tablename
where PrimaryKeyColumn = PrimaryKey;
commit;
END
As I mentioned, I am getting the expected result from my query and it is perfectly functional, so I am not sure what can be the cause.
The server is running MySQL version 5.7.27-0ubuntu0.18.04.1((ubuntu)). Any help with this would be greatly appreciated!
Update - 11/Sep/2019:
I have attempted to check how long the execution of the my PHP script takes. To do this I added the following code borrowed from this thread:
Tracking the script execution time in PHP.
// Script start
$rustart = getrusage();
// Code ...
// Script end
function rutime($ru, $rus, $index) {
return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
- ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}
$ru = getrusage();
echo "This process used " . rutime($ru, $rustart, "utime") .
" ms for its computations\n";
echo "It spent " . rutime($ru, $rustart, "stime") .
" ms in system calls\n";
However, the result was the following:
This process used 0 ms for its computations It spent 1 ms in system calls
Which should not cause any timeouts.
I also activated the general log file on the server and i did see that the it would track the query as follows:
Command Type, Detail
Connect, username#hostname on dbname using Socket
Query, Call StoredProcedure('PrimaryKey', #result)
I am curious about there being no log saying disconnect though I do not know if this is default behaviour from MySQL.
Another curious thing I discovered was that MySQL Workbench states my query time for the stored procedure is on average 7ms but all resources I could find states that PHP waits for the query to finish before continuing.
Query, Total Time, Max Time, Avg Time
CALL StoredProcedure ( username # hostname ) , 98.94, 66.26, 7.07
In short, I still have not found any solutions to the issue, but potentially some leads that could eventually lead to a resolution.
Update - 18/Sep/2019:
After more digging on the issue I've come across the following thread:
https://dba.stackexchange.com/questions/144773/mysql-aborted-connection-got-an-error-reading-communication-packets
It is suggesting that due to MySQL and PHP being installed on the same server, they are competing for RAM. Considering my server is running on a Raspberry Pi 3 model B, this seems like a plausible explanation for the issue I am facing.

PHP Oracle connection

I am attempting to write some connection code with PHP to a Oracle database my school is hosting.
I'm using oci_connect() at the moment to make this connection, but it is failing.
$conn = oci_connect('username', 'password', 'hostname/SID');
I can access the oracle database through sqlDeveloper, as well as phpmyadmin, so I know the login information is correct.
I checked the oracle version with select * from v$version;, it shows as 12c Enterprise.
What is wrong with my php code for connecting? Is there a better way to make an oracle connection through PHP?
This is the test code I'm running, from http://php.net/manual/en/function.oci-error.php
<?php
echo "running";
$conn = oci_connect("username", "paswwrod", "address/SID");
if (!$conn) {
$e = oci_error(); // For oci_connect errors do not pass a handle
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
echo "ending";
?>
The string "running" gets echoed, but "ending" does not, the script just stops working when it attempts oci_connect()
have you also tried including the port number to the oracle db server like so?
$conn = oci_connect("user", "pass", "localhost:1234/xe");

Cloud9 and MySQL, Connection Refused

I'm sure this will be pretty easy, but I've been looking at this for a while, and haven't figured it out yet.
I've got a project in Cloud9 that I am trying to set up to access a MySQL database.
I've started the MySQL engine (mysql-ctl start), so I don't think that's the issue.
Here are my variables:
$db_hostname = "[username]-[projectname]-[assigned id]"; //this was retrieved by using 'SELECT ##hostname;' within MySQL
$db_database = "c9"; //default cloud9 database
$db_username = "[username]";
$db_password = ""; //the default is no password
$db_port = 3306; //the default cloud9 MySQL port
And here is my mysqli connect statement:
$conn = mysqli_connect($db_hostname, $db_username, $db_password, $db_database, $db_port) or die("Connection failed: " . mysqli_connect_error());
And this is the error that is being sent back:
Warning: mysqli_connect(): (HY000/2002): Connection refused in
/home/ubuntu/workspace/insertWorkoutScript.php on line 11
Call Stack:
0.0009 248728 1. {main}() /home/ubuntu/workspace/insertWorkoutScript.php:0
0.0012 249768 2.
mysqli_connect() /home/ubuntu/workspace/insertWorkoutScript.php:11
Connection failed: Connection refused
Line 11 (the line that the error identifies) is the $conn = mysqli ... statement. I've verified that the variables are being filled (I've got an echo for each variable name and it's value). I've double and triple checked the value I'm using for $db_hostname (which as I said, I retrieved from cloud9 using the SELECT ##hostname; statement). And as I said, I've made sure to Start the MySQL instance with the $ mysql-ctl start line in the terminal. Thoughts on what simple thing I'm missing here?
Thanks
Thank you #HPierce for the reminder to try getenv('IP'). I had tried that earlier but got a bad function response. My guess is that I typed it incorrectly, or was using it incorrectly. I've tried that again, and am now getting a good connection. So the correct settings are:
$db_hostname = getenv('IP'); // as opposed to using results from select ##hostname;
$db_database = "c9"; //default cloud9 database
$db_username = "[username]";
$db_password = ""; //the default is no password
$db_port = 3306; //the default cloud9 MySQL port
Everything else stayed the same. And I've also now solved my SQL insertion error as well.
Thanks again for your help.

PHP/MYSQLI: mysqli_query fails in PHP

First off, I'm a total noob in mysql(i) language and know a little more than the basics of PHP.
Note: I do not manage or own / have access to the server on which the webpage currently is hosted. I can however access the phpMyAdmin page.
That said, I've got a webpage on which I am trying out some stuff. Right now I'm trying to make a log-in page linked to a database.
Now, behold the code:
$mysql_host = "localhost";
$mysql_user = "my_user";
$mysql_database = "my_database";
$mysql_password = "my_password";
$table = "my_tablename";
// Create connection
$con=mysqli_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// sending query
$result = mysqli_query("SELECT * FROM my_tablename");
if (!$result) {
echo "Query to show fields from table failed! :-(";
}
Now, here comes the actual problem. As soon as I launch the page it will give me my "Query to show fields from table failed!" error message. But when I enter the same query on the phpMyAdmin 'SQL' tab, I get the wanted results. How come the webpage gives me an error, while the phpMyAdmin gives me the results, and, how do I solve it?
Use correct syntax:
$result = mysqli_query($con, "SELECT * FROM my_tablename");
You forgot to link current mysqli connection. First parameter is link - which mysqli connection you want to use (good for multiple conns) and then the second is your query.

Access denied for user 'www-data'#'localhost - how to deal with that?

I face with a strange problem yesterday. I have server running Debian with installed PHP 4.4.4-8 and mysql 5.5.9. That server serves a several websites.
For some reason randomly I get that error "Access denied for user 'www-data'#'localhost' (using password: NO)" when I try to load the webpage.
If I hit refresh the page loads normally, but afer several clicks that message appears again. Username which that page use to connect to mysql server is not www-data.
Does anyone has faced similar problem ?
www-data is the Debian user that runs apache and php. If you attempt a query when you don't have a valid connection, php/mysql will attempt to create a connection using <unix-user>#localhost with no password. This is where www-data#localhost (using password:NO) is coming from.
The most likely reason that this has started happening now (though it has been running fine for 2-years prior) is that your db load has increased to the point where some connections are unable to succeed (probably due to max_connections, or max_user_connections; though this can also result from other limits like memory, threads, etc). When this happens, your call to mysql_connect will emit an error message, and return FALSE. If you fail to detect this failure, then your next mysql call (probably mysql_query, or mysql_select_db) will attempt the connection to www-data#localhost -- thus causing the problem you're seeing.
I suggest enabling error reporting, and error display (as suggested by #DarkMantis) :
ini_set('error_reporting', E_ALL|E_STRICT);
ini_set('display_errors', 1);
Also, be sure that your call to mysql_connect is not preceded by a # sign; and make sure to check the return value. It should look something like this:
$cxn = mysql_connect('localhost','yourusername','yourpassword');
if( $cxn === FALSE ) { die('mysql connection error: '.mysql_error()); }
It sounds like the query that is causing the error happens when something specific is called. This could mean that when the query is called, you aren't connected to the database with the correct username/password.
Try to ensure that you are definatly connected, use or die(mysql_error()); at the end of all your query variables to debug them.
Also, use the following two lines at the top of your php file:
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
That will show you any little php errors that may occur within your class/file which you may not have picked up before.
If your still having a problem after this, please post your PHP code and I will take a look at it directly.
Thanks!
i faced the same problem.
The problem was in my config.php!
I simply changed the $dbserver from
"127.0.0.1" -> "localhost".
Now the connection works again!
For absent-minded people, this error may happen when mysql_query() is called after mysqli_connect(), when it should be mysqli_query().
Use password 'NO'
(MySQLi Object-Oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Deactivating safe mode fixed it for me:
Notice: mysql_connect(): SQL safe mode in effect - ignoring host/user/password information in /var/www/html/test.php on line 4
For solve this problem. I had to change the connection script Using
(MySQLi Object-Oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Categories