Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I'am searching for an Tool in !PHP! that will return maybe an Array or just something, with which i can lookup if there are any Changes.
Its beeing used to get Data from MySQL turning it into a JSON File and if the JSON File differs from the MySQL Output, then the file should be new generated.
So i just need to type a Function or something else, if there's been changes made, (Yes/No) do X.
Do you guys know any Tool that would work with my Idea?
Thanks for Helping me out.
Your question is vague but, if I understood right, here is an idea:
<?php
class Tool {
var $db; // you will have to provide a valid db connection
function __construct (& $db) {
$this->db = $db;
}
public function execute ($query, $file){
$arr = $this->fetch($query);
$dbJSON = json_encode($arr);
$fileJSON = file_get_contents($file);
if( md5($dbJSON) != md5($fileJSON) ){
$this->write($dbJSON, $file);
}
}
protected function fetch($sql) {
$arr = array();
$result = $this->db->query($sql);
if($result){
// Cycle through results
while ($row = $result->fetch_object()){
$arr[] = $row;
}
// Free result set
$result->close();
}
return $arr;
}
protected function write ($content, $file) {
$fp = fopen('w+', $file);
fwrite($fp, $content);
fclose($fp);
}
}
?>
and a use case:
<?php
$db = new mysqli('localhost','user','pass','database');
// Check for errors
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}else{
$tool = new Tool($db);
$tool->execute('SELECT * FROM users','/com/user.json');
}
?>
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 days ago.
Improve this question
I'm using php to automate a backup process of my databases, process runs with no error and create the .sql.gz files, but all of them are of a fixed size of 350 byte, useless. I'm using exec() function to read the return value and get the output from mysqldump.
return is always 0 and I cannot print the output to see if there is an error.
This is the code I'm using:
#Process begins
$conn = new mysqli("localhost", $user, $pwd);
$result = $conn->query("SHOW DATABASES");
if ($conn->affected_rows > 0){
$conn->close();
while( $db = mysqli_fetch_row($result)){
$return=null;
$output=null;
if (($db[0]!="information_schema") && ($db[0]!="mysql")) {
$filename = "$targetdir/$db[0]-$filetime.sql";
$cmd = "mysqldump -u $user -p$pwd $db | gzip > $filename.gz";
exec($cmd, $output, $return);
if ($return==0) {
file_put_contents($logfile, serialize($output), FILE_APPEND);
} else {
file_put_contents($logfile, $return, FILE_APPEND);
file_put_contents($logfile, serialize($output), FILE_APPEND);
}
}
}
}else{
file_put_contents($logfile, "No databases found to backup", FILE_APPEND);
}
exit(0);
If anybody can help, specially to read the output I will gladly appreciate it
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm writing a php function which must open and read line by line the content of a txt file saved in the same directory of the php file and checks if the first value of the line is equal to a number which I give it as a parameter of the function and if this number isn't in the file I will create a new line with the number followed by another information separated by a space. This funcion is in a webhook file of a Facebook Messenger bot and it doesn't work if the function isn't commented out and I don't know why because I checked the code several times and I haven't found the error.
The code of the function is:
function messagesNumber($userid)
{
$fo = ("url of the files", "a+");
$found = false;
if($fo) {
$line = fgets($fo);
while(! feof($fo) && $found == false) {
$words = explode(" ", $line, 2);
if($words[0] == $userid) {
$found = true;
$words[1] += 1;
$messagges = $words [1];
str_replace($line, $words, $fo);
}
$line = fgets($fo);
}
if(! $found) {
fprintf($fo, "%d 1", $userid);
$messages = 1;
}
}
else
$messagges = -1;
fclose($fo);
return $messagges;
}
It returns: -1 if the file can't be open or an integer that coincides with the second number of every line.
It is an example of the txt file:
282318736127 5
827813487273 3
...
The first number is the id which Facebook assign to every user and the second number is a counter who counts how many messages a user writes to the bot and I increase this last number every times that a specific user sends a message to the bot.
easy ... your missing a $
if(! $found) {
fprintf($fo, "%d 1", $userid);
messages = 1; // missing $
}
to
if(! $found) {
fprintf($fo, "%d 1", $userid);
$messages = 1;
}
okay edit ...
easy your missing an fopen
$fo = ("url of the files", "a+");
to
$fo = fopen("url of the files", "a+");
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am using Windows and working with CSV/XLS data. I have a data that need to edit with PHP, but I'm not sure how.
Here is sample the data:
code1,data1
D0001,X1235466
D0002,X1232265
D0003,X1235166
D0004,X5235466
D0005,X6232265
D0006,X7235166
.. and so on--about 20,000 lines.
I am trying to make the file look like this:
code1,data1,code2,data2,code3,data3
D0001,X1235466,D0002,X1232265,D0003,X1235166
D0004,X5235466,D0005,X6232265,D0006,X7235166
and so on, then save it to new CSV file. Thanks.
Alright, I was bored, so I wrote this. You really need to attempt writing the code before posting the question, though, and show your work.
Here goes:
<?php
$desiredColumns = 3;
$inputFd = fopen('data.csv', 'r');
$outputFd = fopen('output.csv', 'w');
$buffer = [];
// Write the CSV header.
fputcsv($outputFd, [
'code1', 'data1', 'code2', 'data2','code3', 'data3'
]);
//Skip header line.
fgetcsv($inputFd);
while (!feof($inputFd)) {
$line = fgetcsv($inputFd);
if (empty($line) || empty($line[0])) {
continue;
}
$line = array_map('trim', $line);
$buffer[] = $line;
// If our buffer has $desiredColumns in it, flush the buffer
// to our output file.
if (count($buffer) == $desiredColumns) {
outputCsv($outputFd, $buffer);
$buffer = [];
}
}
// If we had a total number of entries that is not a multiple
// of 3, we need to output the remaining records.
if (!empty($buffer)) {
outputCsv($outputFd, $buffer);
}
// Function for flushing a record buffer to the output file.
function outputCsv($outputFd, $buffer)
{
$outputData = [];
foreach ($buffer as $record) {
$outputData[] = $record[0];
$outputData[] = $record[1];
}
fputcsv($outputFd, $outputData);
}
Now, as an example of this being run:
$ cat data.csv
code1,data1
D0001,X1235466
D0002,X1232265
D0003,X1235166
D0004,X5235466
D0005,X6232265
D0006,X7235166
$ php so.php
$ cat output.csv
code1,data1,code2,data2,code3,data3
D0001,X1235466,D0002,X1232265,D0003,X1235166
D0004,X5235466,D0005,X6232265,D0006,X7235166
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
someone could please tell why this code does not work? it does not create data.txt. and the file does not save anything.
<?php
$txt = "data.txt";
if(isset($_POST['info1']) && isset($_POST['info2']) && isset($_POST['info3']) && isset($_POST['info4']) && isset($_POST['info5']) && isset($_POST['info6'])) {
// check if both fields are set
//open file in read mode to get number of lines
$handle = fopen($txt, 'r');
//check file opened
if($handle) {
//get number of lines
$count = 1;
while(fgets($handle) !== false) {
$count++;
}
fclose($handle);
//open in append mode.
$handle = fopen($txt, 'a');
//prepare data to be writen
$txt = $count . ' ' . $_POST['info1'].'/'.$_POST['info2'].'/'.$_POST['info3'].'/'.$_POST['info4'].'/'.$_POST['info5'].'/'.$_POST['info6']. "\r\n";
//write data to file
fwrite($handle,$txt);
fclose($handle);
}
}
?>
In fopen function mode param with value r doesn't make file being created.
See fopen documentation.
Also the issue could be write permissions for user that runs that PHP script.
These is only my guesses. Anyway you should configure your PHP (in dev environment) to show you every error/warning in order to debug such issues. You can do that in php.ini file or straight in PHP file with this code:
error_reporting(E_ALL);
ini_set('display_errors', 1);
You are first trying to open the file read only, and if that succeeds you close it and open it again with write access.
This will only work if the file already exists.
If the file doesn't exist, your text will not get written to it.
Move the closing bracket if if ($handle) { in front of the second fopen call.
if($handle) {
//get number of lines
$count = 1;
while(fgets($handle) !== false) {
$count++;
}
fclose($handle);
}
//open in append mode.
$handle = fopen($txt, 'a');
//prepare data to be writen
$txt = $count . ' ' . $_POST['info1'].'/'.$_POST['info2'].'/'.$_POST['info3'].'/'.$_POST['info4'].'/'.$_POST['info5'].'/'.$_POST['info6']. "\r\n";
//write data to file
fwrite($handle,$txt);
fclose($handle);
by the way, you can reduce this to three lines of code.
$lines = file($txt);
$count = sizeof($lines);
file_put_contents($txt, $count . ' ' . $_POST['info1'].'/'.$_POST['info2'].'/'.$_POST['info3'].'/'.$_POST['info4'].'/'.$_POST['info5'].'/'.$_POST['info6']. "\r\n", FILE_APPEND);
to check for multiple variables in isset create only one isset to check all veriables
change this
if(isset($_POST['info1']) && isset($_POST['info2']) && isset($_POST['info3']) && isset($_POST['info4']) && isset($_POST['info5']) && isset($_POST['info6']))
to
if(isset($_POST['info1'],$_POST['info2'],$_POST['info3'],$_POST['info4'],$_POST['info5'],$_POST['info6']))
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to generate a csv file, using data from a database. How does one do this in PHP CodeIgniter framework?
Take a look at this answer of mine and you will know exactly how you can do it.
Reports in Codeigniter
You need to use Codeigniter Database Utility Class
And need to return query instead of result_array or result
function query_to_csv( $query, $filename, $attachment = false, $headers = true) {
if($attachment) {
// send response headers to the browser
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.$filename);
$fp = fopen('php://output', 'w');
} else {
$fp = fopen($filename, 'w');
}
$result = mysql_query($query);
if($headers) {
// output header row (if at least one row exists)
$row = mysql_fetch_assoc($result);
if($row) {
fputcsv($fp, array_keys($row));
// reset pointer back to beginning
mysql_data_seek($result, 0);
}
}
while($row = mysql_fetch_assoc($result)) {
fputcsv($fp, $row);
}
fclose($fp);
exit;
}