I'm working on a script to make versioning our database easier. In order to do this we have a number of files (each contains at least one statement and a couple have over a hundred) containing the SQL queries that we need to update the schema that are delimited using semicolons. It works nicely 90% of the time except occasionally one of the statements will fail and we're having problems getting these fixed. When this happens we have to delete the database and manually copy and paste each SQL statement in the failing file to test it. Some queries cause an exception but for some queries the function just returns 1.
I'm using the following code but I can't figure out how to find the statement that's having the problem:
$db = Zend_Db_Table::getDefaultAdapter();
try{
// loop through all the files
foreach($files as $file){
echo 'Running ', $file, PHP_EOL;
// use the connection directly to load sql in batches
if($db->exec(file_get_contents($dir . $file)) == 1){
// *how do I get the problem line here?*
return false;
}
$db->exec('INSERT INTO database_history SET file = "' . $file .'";');
}
} catch (Exception $e) {
echo 'AN ERROR HAS OCCURED:' . PHP_EOL;
echo $e->getMessage() . PHP_EOL;
return false;
}
Initialise a counter var like in the following example. Increment it each iteration by one. Output the counter var, when there's an error. I've called the var $line in this example. The exit terminates the script, after the var has been output. If you don't want that, you can just leave it out.
$line = 0;
foreach($files AS $file){
$line++;
// ...
if($db->exec(file_get_contents($dir . $file)) == 1){
echo '<pre>'; print_r($line); echo '</pre>'; exit;
return false;
}
// ...
}
Related
I have an issue where when I create a real-time subscription I get duplicate notifications from different Instagram IP addresses. I have it set up so that when I get a notification, I send a request for latest updates using the min_tag_id setting. I store that in my db to use it for the next request. I don't always get duplicates, but when I do, everything about the notification is the same (time, object,changed_aspect), except I can tell they are different from my debugging output which lists two almost identical requests... the only differing info being a different IP address and the REQUEST_TIME_FLOAT is different by about 1/10th of a second. They even have the same HTTP_X_HUB_SIGNATURE value.
My general algorithm is:
process_subscription_update($data){
# get old min_id
$min_tag_id = mysqli_fetch_object(mysqli_query($dbconnecti,sprintf("SELECT instagram_min_id+0 as instaid FROM xxxx WHERE xxxx=%d",$_GET['xxxx'])));
$min_id = $min_tag_id->instaid;
# make api call
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'https://api.instagram.com/v1/tags/'.$_GET['tag'].'/media/recent?client_id=xxxx&min_tag_id='.$min_id.($min_id==0?'&count=1':''));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
$i = json_decode($result);
if ($min_id == $i->pagination->min_tag_id) { exit; }
# write new min_id to db
record_min_id($i->pagination->min_tag_id);
$data2 = $i->data;
foreach($data2 as $d) {
process_instagram($d);
}
// debugging output: ****************
$file = file_get_contents($_SERVER['DOCUMENT_ROOT'].'instagram/updates.txt');
$foo = "\n";
foreach($_SERVER as $key_name => $key_value) {
$foo .= $key_name . " = " . $key_value . "\n";
}
$fulldata = $file . "\n\n\n" . $result . "\n min_id = " . $min_id . $foo;
$fulldata .= "\nTIME:".$data[0]->time;
$fulldata .= "\nOBJECT:".$data[0]->object;
$fulldata .= "\nCHANGED_ASPECT:".$data[0]->changed_aspect;
file_put_contents($_SERVER['DOCUMENT_ROOT'].'instagram/updates.txt', $fulldata);
// end debugging output *************
}
I'd like to avoid checking if the instagram message id already exists in my db within the process_instagram function, and with the duplicates only coming 1/10th of a second apart, I don't know if that would work anyway.
Anybody else experience this and/or have a solution?
I solved this. I don't think there is anything I can do about receiving the duplicate notifications. So, when writing the Instagram to my db, I have a field for the Instagram id and put a unique constraint on the field. After doing the mysqli INSERT, I check to see if the errno = 1062, and if it does, I exit.
mysqli_query($dbconnecti,"INSERT INTO xxx (foo, etc, instagram_id ...")
if ($dbconnecti->errno==1062) { exit; }
...
// more script runs here if we don't have a duplicate.
In the following example, can any folks show me how to code the binding part as I select all fields from the table
$stmt = $mysqli_conn->stmt_init();
if ($stmt->prepare("SELECT * FROM books")) {
$stmt->execute();
$stmt->bind_result( **WHAT DO I PUT HERE** );
$stmt->close();
}
In bind_result, you put in the variables where you want to fetch the data into, instead of using an array returned otherwise.
$stmt->bind_result($col1, $col2, $col3, $col4);
while($stmt->fetch_assoc())
echo "$col1 $col2 $col3 $col4";
Alternatively, if you don't want to bind the result
while($resultArray = $stmt->fetch_assoc()) {
echo "$resultArray[columnName1] $resultArray[columnName2] ...";
}
I have got a machine (Debian based) with some temperature sensors attached to it, and i would like to query them over snmp, from one script. I can work with one sensor ok, but i am struggling when i plug another one in.
What I am trying to do is loop through each device, and give each one an id, then use this ID as part of the OID, then give it a value.
I've never worked with snmp before, and my perl is not great so any help would be much appreciated. Below is my code:
#!/usr/bin/perl
use NetSNMP::agent (':all');
use NetSNMP::ASN qw(ASN_OCTET_STR ASN_INTEGER);
$BASE_OID=".1.3.6.1.4.1.41050";
$dev_id=1;
$string_value;
$integer_value;
sub pimon_handler {
my ($handler, $registration_info, $request_info, $requests) = #_;
my $request;
my $oid_key;
for($request = $requests; $request; $request = $request->next()) {
$oid_key=$BASE_OID . '.' . $dev_id;
my $oid = $request->getOID();
if ($request_info->getMode() == MODE_GET) {
if ($oid == new NetSNMP::OID($oid_key . '.0')) {
$request->setValue(ASN_OCTET_STR, $string_value);
}
elsif ($oid == new NetSNMP::OID($oid_key . '.1')) {
$request->setValue(ASN_INTEGER, $integer_value);
}
} elsif ($request_info->getMode() == MODE_GETNEXT) {
if ($oid == new NetSNMP::OID($oid_key . '.0')) {
$request->setOID($oid_key . '.1');
$request->setValue(ASN_INTEGER, $integer_value);
}
elsif ($oid < new NetSNMP::OID($oid_key . '.0')) {
$request->setOID($oid_key . '.0');
$request->setValue(ASN_OCTET_STR, $string_value);
}
}
}
}
#location of where we are going to find the 1wire devices
#sensors = `cat /sys/bus/w1/devices/w1_bus_master1/w1_master_slaves`;
chomp(#sensors);
#loop through the sensors we find
foreach $line(#sensors) {
#work out the temp we have got. Need to change this for other sensor types
$output = `cat /sys/bus/w1/devices/$line/w1_slave`;
$output =~ /t=(?<temp>\d+)/;
$integer_value = sprintf "%.0f",$+{temp} / 1000;
$string_value = $line;
my $agent = new NetSNMP::agent();
$agent->register("Pimon$looptest", $BASE_OID . '.' . $dev_id,
\&pimon_handler);
print "Dev $dev_id temp $line temp is $integer_value\n";
$dev_id ++;
}
Are you getting any errors or output?
I suspect that your problem lies in and around your reading the data file by shelling-out to cat instead of opening the file and looping over the linewise contents.
Try dumping the value of #sensors. if it is a single entry array, with the only element containing your entire file, then simply switch #sensors to be scalar. then split $sensors into an array and loop over that.
my $sensors = `read something`
chomp $sensors;
my #sensors = split(/\n/, $sensors);
foreach $line (#sensors) {
...
Methods like verifyText do not report the line number of the failure, making it hard to find the point of failure.
The code created by Selenium IDE PHPUnit export looks like this:
try {
$this->assertEquals($text, $this->getTitle());
} catch (PHPUnit_Framework_AssertionFailedError $e) {
array_push($this->verificationErrors, $e->toString());
}
The output for this line looks like line 2 below, which is completely untraceable
Failed asserting that '' matches PCRE pattern "/Harlem/".
Failed asserting that two strings are equal.
Failed asserting that '(Pattern A)' matches PCRE pattern "/\(Pattern B\)/".
I've amended the call to include the referenced text which lets me search for the text failure, but in a large test this is not sufficient. How do I get the line number/stacktrace for each verify failure within my code?
public function verifyTitle($text) {
$title = $this->getTitle();
try {
$this->assertEquals($text, $title);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
array_push($this->verificationErrors,
"Title is '$title' but should be '$text'");
}
}
Note: in order to get stacktraces returning references to my code for asserts, I am using the stacktrace hack
Created this verification Method to include (too much) stacktrace:
public function appendVerification($message,$e) {
array_push($this->verificationErrors,$message."\n".$this->dumpStack($e));
}
I also updated the referenced dumpStack method for PHPUnit tests to dumbly strip out framework calls by class name
protected function dumpStack(Exception $e) {
$stack = '';
foreach ($e->getTrace() as $trace) {
if (isset($trace['file']) &&
isset($trace['line'])) {
if (!isFramework($trace['file']))
$stack .= PHP_EOL .
$trace['file'] . ':' .
$trace['line'] . ' ';
}
}
return $stack;
}
function isFramework($fileName) {
$test = ((preg_match("/PHPUnit/i",$fileName) +
preg_match("/php.phpunit/i",$fileName)) > 0);
return $test;
}
End result, clickable in netbeans, free of any PHPUnit framework line numbers
Failed asserting that 'waitForElementPresent failed for selector: css=input#address.valid' is false.
C:\dev\Automation_Dev\phpTests\library\SeleniumUtilsTestCase.php:228
C:\dev\Automation_Dev\phpTests\library\SeleniumUtilsTestCase.php:115
C:\dev\Automation_Dev\phpTests\library\SeleniumTestCase.php:176
C:\dev\Automation_Dev\phpTests\usecases\CreateListingTest.php:72
C:\dev\Automation_Dev\phpTests\usecases\CreateListingTest.php:39
C:\dev\Automation_Dev\phpTests\usecases\CreateListingTest.php:23
C:\xampp\php\phpunit:46
I am new to php. have a task to ftp only todays files from the server containing files for over a week. how do i select or filter the files based on date and ftp to my local folder.
Help is much appreciated !!
Soloman
If you already have the filenames to check, use filemtime
Returns the time the file was last modified, or FALSE on failure. The time is returned as a Unix timestamp, which is suitable for the date() function.
To compare that today, you can use date('Y-m-d') to get today compare to date('Y-m-d', filemtime($filename))
To get the filenames, you could use readdir to read each one in turn.
<?php
if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Files:\n";
while (false !== ($filename = readdir($handle))) {
echo "$filename\n";
}
closedir($handle);
}
?>
The manual also has an FTP example which should show you how to ftp the file once found.
So, combining all this, you could get something like:
<?php
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
if ($handle = opendir('/path/to/files')) {
while (false !== ($filename = readdir($handle))) {
if (date('Y-m-d') == date('Y-m-d', filemtime($filename))) {
// upload the file
$upload = ftp_put($conn_id, $destination_file, $filename, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
}
}
closedir($handle);
// close the FTP stream
ftp_close($conn_id);
?>
Of course, you will need to fill in the dummy values as appropriate.
Disclaimer: I typed this in notepad++ and it is not tested for any mistakes!
link :
http://www.php.net/manual/en/function.ftp-rawlist.php
you connect to the server ,
get the list of the files with ftp_rawlist()
and get only file you want via ftp_fget()
example
<?php
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// get the file list for /
$rawfiles = ftp_rawlist($conn_id, '/');
foreach ($rawfiles as $rawfile) {
# parse the raw data to array
if(!empty($rawfile)) {
$info = preg_split("/[\s]+/", $rawfile, 9);
$arraypointer[] = array(
'text' => $info[8],
'isDir' => $info[0]{0} == 'd',
'size' => byteconvert($info[4]),
'chmod' => chmodnum($info[0]),
'date' => strtotime($info[6] . ' ' . $info[5] . ' ' . $info[7]),
'raw' => $info
// the 'children' attribut is automatically added if the folder contains at least one file
);
// pseudo code check the date
if($arraypointer['date'] is today)
ftp_fget(file);
}
// close the connection
ftp_close($conn_id);
// output the buffer
var_dump($buff);
?>