I'm using php and postgresql+postgis to automatically remove nested shells from simplified geojson files, I'm also doing a similar query with pgadmin to get other errors and manually fix them. All is working as needed but the php script won't start correctly sometimes, after starting it just stays there (with no cpu usage on postgresql or php) and I have to restart the postgresql server so the php script can work again.
This is the sql I'm using on both pgadmin and php (query.txt):
CREATE OR REPLACE FUNCTION read_file_utf8(path CHARACTER VARYING)
RETURNS TEXT AS $$
DECLARE
var_file_oid OID;
var_record RECORD;
var_result BYTEA := '';
BEGIN
SELECT lo_import(path)
INTO var_file_oid;
FOR var_record IN (SELECT data
FROM pg_largeobject
WHERE loid = var_file_oid
ORDER BY pageno) LOOP
var_result = var_result || var_record.data;
END LOOP;
PERFORM lo_unlink(var_file_oid);
RETURN convert_from(var_result, 'utf8');
END;
$$ LANGUAGE plpgsql;
SELECT replace(
ST_IsValidReason(ST_GeomFromGeoJSON(
read_file_utf8('some_file.geojson')
)),
' ',
','
);
On php I'm running pg_query like this:
$query = file_get_contents('query.txt');
$dbc = pg_pconnect('host=localhost port=5432 dbname=XXX user=XXX password=XXX');
if (!pg_connection_busy($dbc)) {
$has_error = true;
while ($has_error) {
$res = pg_query($dbc, $query);
if ($row = pg_fetch_row($res)) {
if (!fix_json($row[0])) { $has_error = false; }
}
else { $has_error = false; }
}
}
pg_close();
When I restart the postgresql service I get this error on php:
Warning: pg_query(): Query failed: FATAL: terminating connection due to administrator command
CONTEXT: while updating tuple (180,4) in relation "pg_proc"
server closed the connection unexpectedly
I'm on windows 10 x64 1703, php x64 5.6.30, postgresql server x64 10.1
Related
I am trying to insert a big file (few millions row) via SQL server BULK INSERT functionality. My SQL query will look like:
BULK INSERT MY_TABLE
FROM '\\myserver\open\myfile.csv'
WITH (
firstrow=2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
BATCHSIZE = 50000,
ERRORFILE = '\\myserver\open\myfileerror.log'
);
When I trigger it from MSSQL Server Management Studio, it always import it completely.
When I do it from my PHP code, sometimes it stop in the middle, without any error messages.
I tried with both sqlsrv_query or sqlsrv_prepare/sqlsrv_execute, same result.
sql; //like the query above
$statement = sqlsrv_query($connection, $sql);
if($statement === false) {
$error = sqlsrv_errors();
$error['sql'] = $sql;
throw new Exception(json_encode($error));
}
Would it be possible to get the logs of MSSQL from the $statement, the same I get from the MSSQL Studio? e.g. (50000 row(s) affected).
As a workaround, I have increased the BATCHSIZE to 1000000, but that is not a real solution.
Background information:
- PHP 7.1.9
- sqlsrv version: 4.3.0+9904
- sqlsrv.ClientBufferMaxKBSize: 10240
- Windows 2012 R2 Server
The issue was about the statement buffer. When I read it with sqlsrv_next_result, processing continue.
$statement = sqlsrv_query($connection, $sql);
if($statement === false) {
$error = sqlsrv_errors();
$error['sql'] = $sql;
throw new Exception(json_encode($error));
} else if($statement) {
while($next_result = sqlsrv_next_result($statement)){
#echo date("Y-m-d H:i:s",time()). " Reading buffer...\n";
}
}
I develop a webapp in php using sqlite to store data in a database. As seen on the internet I use PDO instead of SQLITE3 class. I did all steps shown in the internet to avoid this error message, but it still apears:
SQLSTATE[HY000] [14] unable to open database file
A periodic task is running every 30 seconds to get the number of entries in a table. This task is triggered by Javascript/Jquery. On the normal php files I do not have any problem using PDO. But using it in combination with Javascript/Jquery I receive the message above.
I set already the whole path and the database to user/group of the webserver and to chmod 0777.
My own written database class is created and stored in a Session variable, to avoid creation and destruction during the life of the webpage.
This function connects to the database (this and the next function are in the same class):
private $db = null;
private function connectdb(){
try {
$this->db = new PDO(database,"","",array(
PDO::ATTR_PERSISTENT => TRUE,
PDO::ERRMODE_EXCEPTION => TRUE));
} catch (PDOException $e) {
logerror($e->getMessage(), "connecthrsedb");
return exception;
}
}
This function is doing the query:
function getNumberofEntriesinTable() {
try {
if (is_null($this->db)) {
if ($this->connectdb() < 0) {
return -1;
}
}
$statement = "select count(*) from table;";
$res = $this->db->query($statement);
$res = $res->fetch();
$res = $res['count(*)'];
$this->db = null;
return $res;
} catch (PDOException $e) {
syslog(LOG_ERROR,$e->getMessage());
return -1;
}
}
The session variable $_SESSION['database'] is generated on startup.
This javascript/jquery function should do the stuff:
function getData(){
$.post("php/getdatafromdb.php",{action: 'getdata'},function(data){
console.log(data);
}
setTimeout(function () {getData();},30000);
}
The javascript/jquery functions runs well and is executed with the setTimeoutfunction.
However I don't know how to prevent or remove this issue. How can I solve this?
My PHP ini file is has the following entries concerning sqlite and pdo:
[sqlite]
; http://php.net/sqlite.assoc-case
;sqlite.assoc_case = 0
[sqlite3]
;sqlite3.extension_dir =
[Pdo]
; Whether to pool ODBC connections. Can be one of "strict", "relaxed" or "off"
; http://php.net/pdo-odbc.connection-pooling
;pdo_odbc.connection_pooling=strict
;pdo_odbc.db2_instance_name
[Pdo_mysql]
; If mysqlnd is used: Number of cache slots for the internal result set cache
; http://php.net/pdo_mysql.cache_size
pdo_mysql.cache_size=2000
; Default socket name for local MySQL connects. If empty, uses the built-in
; MySQL defaults.
; http://php.net/pdo_mysql.default-socket
pdo_mysql.default_socket=
Are there any sqlite pdo settings required?
I solved the problem. Unfortunatetly the error message is confusing, because PHP did not find the file.
Use always absolute paths, to avoid this error even if permissions, user and group are set correct.
I am trying to export data from MSSQL table to Excel. Below is my code.
Problem is that i am getting the following error:
Fatal error: Call to undefined function mssql_fetch_array() in C:xampp\code.php on line 25
I am running Windows Server 2008 R2, IIS7, SQL Server 2008, PHP Version 5.4.4.
I have un-commented the line: "extension=php_mssql.dll" found in C:\xampp\php\php.ini
<?php
// load library
require 'include\php-excel.class.php';
$i = 0; // used as a counter
$myServer = "SERVERNAME\SQLEXPRESS";
$myUser = "UserName";
$myPass = "xxxxxx";
$myDB = "dbName";
$conn = new COM ("ADODB.Connection") or die("Cannot start ADO");
$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB;
$conn->open($connStr); //Open the connection to the database
$result = "SELECT Col1, Col2, Col3 FROM myTable WHERE Col3='myCondition'";
//$rs = $conn->execute($result);
//$num_columns = $rs->Fields->Count();
// create data array and print headers on the first row
$data = array(1 => array ('No.', 'Col1', 'Col2', 'Col3'));
while($row=mssql_fetch_array($result)) {
//include additional rows
array_push($data, array($i, $row['Col1'], $row['Col2'], $row['Col3']));
$i++;
}
// If no results, indicate this on the first row
if ($i == 0){
$data = array(1 => array ('No results', 'empty', 'empty', 'empty'));
}
//generate file (constructor parameters are optional)
$xls = new Excel_XML('UTF-8', false, 'My Test Sheet');
$xls->addArray($data);
$xls->generateXML('MyReport');
?>
Thank you in advance.
PHP5.3 onwards doesn't support the mssql functions. If you check your PHP ext folder, you will find there isn't a php_mssql.dll no more
You're using an ADODB COM to create your DB connection. This isn't the same thing as using the mssql_xx library.
You can't mix and match between the two - they're not compatible.
In any case, you clearly don't have the mssql_xx library installed (I'm not even sure it's supported any more), so you can't use that.
Right above your failing call to mssql_fetch_array(), you have some commented out lines:
//$rs = $conn->execute($result);
//$num_columns = $rs->Fields->Count();
Not sure why you've commented these out, because you'll need them, or something like them. Particularly the first of those lines. And after that, you'd need to use the ADODB equivalent to mssql_fetch_array() to read the records in the loop.
However, my advice is to ditch all of this, and switch to using the PDO library instead. PDO supports the MS SQL database, and is standard PHP, unlike the ADODB stuff you're trying to use here.
See the PHP manual for the PDO library here: http://php.net/manual/en/book.pdo.php
After uncommenting the line for MS SQL library, have you restarted the web server for the change to take effect?
For sql, we can use sqlsrv_fetch_array with sqlsrv_query as:
$sql_query = "SELECT Col1, Col2, Col3 FROM myTable WHERE Col3='myCondition'";
$result = sqlsrv_query($conn,$sql_query);
while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC) ) {
echo $row['Col1'];
/*etc codes here*/
}
PHP 5.2.13, MS SQL Server 2008 R2
I have a stored procedure that looks something like this:
CREATE PROCEDURE [dbo].[StoreThing]
#pid int = 0,
#data binary(236) = NULL,
AS
BEGIN
INSERT INTO thingTable (pid, data) VALUES (#pid, #data)
END
(Greatly simplified, but that's beside the point)
I'm trying to call this procedure from a PHP script, as such:
function storeThing($pid, $data) {
global $dbconn;
if(strlen($data) != 236) return false;
$proc = mssql_init('StoreThing', $dbconn);
mssql_bind($proc, '#pid', $pid, SQLINT4);
mssql_bind($proc, '#data', $data, SQLCHAR, false, false, 236);
$result = mssql_execute($proc);
if(!$result) die(mssql_get_last_message());
return true;
}
However, this is not working quite right. I get the following error when I run it:
Warning: mssql_execute() [function.mssql-execute]: message: Implicit conversion from data type char to binary is not allowed. Use the CONVERT function to run this query. (severity 16)
Obviously, the problem is the SQLCHAR type I set for the data field. However, since there is no SQLBINARY constant, I am not sure what to set it to.
Alternately, will I have to change the stored procedure to accommodate PHP? I could do that, but it seems a bit dumb.
You can use the Microsoft SQL Server API, see http://msdn.microsoft.com/en-us/library/cc793139%28SQL.90%29.aspx
I have a stored procedure:
delimiter //
create procedure userlogin(in eml varchar(50))
begin
select * from users
where email = eml;
end//
delimiter ;
And the php:
$db = new mysqli("localhost","root","","houseDB");
$eml = "tsubi#gmail.com";
$sql = $db->query("CALL userlogin('$eml')");
$result = $sql->fetch_array();
The error that I get from the browser when I run the php script:
Fatal error: Call to a member function fetch_array() on a non-object...
I am using phpmyadmin version 3.2.4 and mysql client version 5.1.41.
You have to use mysqli_multi_query, not query. Check
http://us.php.net/manual/en/mysqli.multi-query.php , they have a good example
mysqli::query returns false if the query fails (instead of returning a result object or true). You need to test whether the result actually is an object:
$sql = $db->query("CALL userlogin('$eml')");
if (is_object($sql))
$result = $sql->fetch_array();
else
printf("Error: %s\n", $sql->error);
You will probably get an error message explaining why calling the stored procedure didn*t work out.