How to create json file from mysql php query - php

I'm trying to save the data retrieved from the database into a .json. This is what is just tried.
$sql = mysql_query("SELECT `positive`,`time` FROM sentiment WHERE acctid=1");
$response = array();
$posts = array();
while($row=mysql_fetch_array($sql))
{
$positive=$row['positive'];
$time=$row['time'];
$posts[] = array('positive'=> $positive, 'time'=> $time,);
}
$response['posts'] = $posts;
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
I got the following errors:
Warning: fopen(results.json) [function.fopen]: failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/test/getjson.php on line 29
Warning: fwrite() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/test/getjson.php on line 30
Warning: fclose() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/test/getjson.php on line 31
What could the problem be?

The folder /Applications/XAMPP/xamppfiles/htdocs/test isn't writable by Apache - change the permissions so it can write to it. In Windows Explorer, right click the test folder, and untick "Read Only".

Related

Why does fopen(...) fail to open stream? (PHP)

<?php
$con = mysqli_connect("localhost","root","","addressbook");
$file="localhost/IMDBAPI/-title.ratings.tsv";
$row = 1;
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000000, "\t")) !== FALSE) {
$num = count($data);
$row++;
$id=$data[0];
$name=$data[1];
$address=$data[2];
$phone=$data[3];
$sql="INSERT INTO adress (First_Name, Surname, Address) VALUES ('".$name."','".$address."','".$phone."')";
mysqli_query($con, "SELECT * FROM adress");
}
}
fclose($file);
?>
I have a TSV file named title.ratings.tsv and I am trying to insert values from that file into a MySQL table named adress (yes it is spelled incorrectly) into the columns First_Name, Surname, Address but I am getting the error ,
( ! ) Warning: fopen(localhost/IMDBAPI/-title.ratings.tsv): failed to open stream: No such file or directory in C:\wamp64\www\tsv.php on line 9
And,
( ! ) Warning: fclose() expects parameter 1 to be resource, string given in C:\wamp64\www\tsv.php on line 25
The name of File is indiferent. Is the name of file
"-title.ratings.tsv" or
"title.ratings.tsv" (without minus)
Do you have set permissions?
chmod 777 -title.ratings.tsv

imap_open() works locally but not on server

The issue is i'm trying to get the last email from my email using php, and it works fine locally. But then I uploaded the script to the client's server, and I don't get a response. When I check the logs, I get this:
[12-Feb-2020 08:43:21 location] PHP Warning: imap_open(): Couldn't open stream {<mail>:143/imap}INBOX in /path/triggerCall.php on line 2
[12-Feb-2020 08:43:21 location] PHP Warning: imap_headers() expects parameter 1 to be resource, boolean given in /path/triggerCall.php on line 7
[12-Feb-2020 08:43:21 location] PHP Warning: imap_num_msg() expects parameter 1 to be resource, boolean given in /path/triggerCall.php on line 14
[12-Feb-2020 08:43:21 location] PHP Warning: imap_header() expects parameter 1 to be resource, boolean given in /path/triggerCall.php on line 16
[12-Feb-2020 08:43:21 location] PHP Warning: imap_fetchbody() expects parameter 1 to be resource, boolean given in /path/triggerCall.php on line 20
[12-Feb-2020 08:43:21 location] PHP Warning: imap_close() expects parameter 1 to be resource, boolean given in /path/triggerCall.php on line 40
This is the code:
$inbox = imap_open('{<mail>:143/imap}', '<username>', '<password>');
$headers = imap_headers($inbox);
$last = imap_num_msg($inbox);
$header = imap_header($inbox, $last);
$body = imap_fetchbody($inbox, $last,1);
I have tried:
{<mail>:993/imap/novalidate-cert}INBOX
{<mail>:995/pop3/tls}INBOX
{<mail>:995/pop3/ssl}INBOX
{<mail>:993/imap/ssl}INBOX
{<mail>:110/pop3}INBOX
{<mail>:993/imap/tls/novalidate-cert}INBOX
{<mail>:143}INBOX
But none of those approaches work. Any ideas?
Try this
$inbox = imap_open('{mail.gmail.com/ssl/novalidate-cert}INBOX', 'email', 'password');
$mailbox = {outlook.office365.com:993/imap/ssl}.INBOX;
$this->mbox = imap_open($mailbox,USERNAME, PASSWORD, OP_READONLY) or die("can't connect: " . imap_last_error());

Why php crashes when writing and opening a file?

how the php is making error while opening and writing to the file.
function makeImage() {
$encoded_string = $_POST['encoded_string'];
$decoded_string = base64_decode($encoded_string);
$path = "C:\Inetpub\vhosts\apbclt.com\httpdocs\php\logo".$_POST['company_name'];
$file = fopen($path, 'wb');
$is_written = fwrite($file, $decoded_string);
fclose($file);
if($is_written > 0) {
global $mysqli;
$stmt = $mysqli->prepare("INSERT INTO Companies (logo) VALUES (?)");
$stmt->bind_param("s", $path);
$stmt->execute();
$stmt->store_result();
if($stmt->affected_rows > 0) {
$stmt->close();
return true;
}else{
$stmt->close();
return false;
}
}
}
and here is the error
PHP Warning: fopen(C:\Inetpubhosts\apbclt.com\httpdocs\php\logo): failed to open stream: Invalid argument in C:\Inetpub\vhosts\apbclt.com\httpdocs\php\create_company.php on line 54
PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\Inetpub\vhosts\apbclt.com\httpdocs\php\create_company.php on line 55
PHP Warning: fclose() expects parameter 1 to be resource, boolean given in C:\Inetpub\vhosts\apbclt.com\httpdocs\php\create_company.php on line 56
I searched SO but no answer to my case.
Update ::
when I changed the \v to \v like the first answer i got the following error.
PHP Warning: fopen(C:\Inetpub\vhosts\apbclt.com\httpdocs\php\logo): failed to open stream: Permission denied in C:\Inetpub\vhosts\apbclt.com\httpdocs\php\create_company.php on line 54
PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\Inetpub\vhosts\apbclt.com\httpdocs\php\create_company.php on line 55
PHP Warning: fclose() expects parameter 1 to be resource, boolean given in C:\Inetpub\vhosts\apbclt.com\httpdocs\php\create_company.php on line 56
ok I solved the issue instead of using the full file path I just used the path by going back a folder ..\logo

Mysql query in php of webserver

This is the php java script to run on webserver. In it i will get input from android app the query is
<?php
$mysqlDebug = true;
//create connection
print "hello 1";
mysql_connect("localhost", "pu", "Ind");
print "hello 2";
mysql_select_db("leengage");
print "hello 3";
print mysql_error();
// Check connection
if (mysql_error())
{
echo "Failed to connect to MySQL: " . mysql_error();
print "hello 4";
}
print "hello 5";
mysql_query("INSERT INTO details_master(name ,phone, email, birthdaydate, birthdaymonth, anniversarydate, anniversarymonth,created)
VALUES ('Peterr', 'Griffinn','hello#hello.com',12,12,12,12,'0000-00-00 00:00:00')");
print mysql_error();
// escape variables for security
print "hello 7";
$name = mysql_real_escape_string($conn, $_POST['name']);
$phone = mysql_real_escape_string($conn, $_POST['phone']);
$email = mysql_real_escape_string($conn, $_POST['email']);
$birthdaydate = mysql_real_escape_string($conn, $_POST['birthdaydate']);
$birthdaymonth = mysql_real_escape_string($conn, $_POST['birthdaymonth']);
$anniversarydate = mysql_real_escape_string($conn, $_POST['anniversarydate']);
$anniversarymonth = mysql_real_escape_string($conn, $_POST['anniversarymonth']);
$created = mysql_real_escape_string($conn, $_POST['created']);
$sql = "INSERT INTO details_master(name ,phone, email, birthdaydate, birthdaymonth,
anniversarydate, anniversarymonth,created)
VALUES ('$name','$phone', '$email', '$birthdaydate', '$birthdaymonth', '$anniversarydate',
'$anniversarymonth', '$created')";
print "hello 8";
if (!mysql_query($conn, $sql))
{
die('Error: ' . mysql_error($conn));
}
echo "1 record added";
?>
The first insert query is running fine and inserting records from app but second insert query is not working it gives following errors.
[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in /home on line 24
[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in /home on line 25
[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in /home on line 26
[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in /home on line 27
[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in /home on line 28
[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in /home on line 29
[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in /home on line 30
[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in /home on line 31
[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_query() expects parameter 2 to be resource, string given in /home on line 37
[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_error() expects parameter 1 to be resource, null given in /home on line 38
Just in case I am getting input from android app and code i am using for this is on create:
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
And on button click is:
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
// define the parameter
postParameters.add(new BasicNameValuePair("name",name));
postParameters.add(new BasicNameValuePair("phone",phone));
postParameters.add(new BasicNameValuePair("email",email));
postParameters.add(new BasicNameValuePair("birthdaydate",s1));
postParameters.add(new BasicNameValuePair("birthdaymonth",s2));
postParameters.add(new BasicNameValuePair("anniversarydate",s3));
postParameters.add(new BasicNameValuePair("anniversarymonth",s4));
postParameters.add(new BasicNameValuePair("created",formattedDate));
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://purplefront.net/PurpleEngage/mydatainsert.php", postParameters);
// store the result returned by PHP script that runs MySQL query
String result = response.toString();
//parse json data
Log.d("result data", result);
}
catch (Exception e) {
Log.e("log_tag","Error in http connection!!" + e.toString());
}
You keep referencing $conn, which doesn't appear to be set anywhere. It looks like you're trying to use mysqli_ syntax (connection object, then sql) with mysql_ functions (which use the last opened connection)
This answer assumes you convert all your calls to mysqli_ (you can't mix and match) but this should point you in the right direction
$conn = mysqli_connect("localhost","pu","Ind");
$name = mysqli_real_escape_string($conn, $_POST['name']);
if (!mysqli_query($conn,$sql)) {
die('Error: ' . mysqli_error($conn));
}

PHP error: Warning: ociparse() parameter 1 to be resource

As there is not ready made function in oracle to validate the Query so created.
So I tried below code to check whether the QUERY is valid or not.
if(isset($_POST['btn_Submit_Query']))
{
$check_query=$_POST['txtQuery'];
echo $check_query;
$valid = false;
$stmt = oci_parse($DB, $check_query);
echo "Statement" . $stmt;
//oci_define_by_name($stmt, 'NUMBER_OF_ROWS', $number_of_rows);
oci_execute($stmt, OCI_DEFAULT);
echo oci_num_rows($stmt);
}
I got following Warnings in the Execution:
Warning: oci_parse() expects parameter 1 to be resource, object given in D:\xampp\htdocs\app\DashBoardSite\Admin\querybuilder.php on line 899
Statement
Warning: oci_execute() expects parameter 1 to be resource, null given in D:\xampp\htdocs\app\DashBoardSite\Admin\querybuilder.php on line 902
Warning: oci_num_rows() expects parameter 1 to be resource, null given in D:\xampp\htdocs\app\DashBoardSite\Admin\querybuilder.php on line 903
Where is my mistake?
You must first connect to the database. This connection must a "resource" to variable $DB.

Categories