JSON_Encoded Value to be used in snother file - php

<?php
echo $xml = file_get_contents("C:\wamp\www\Sample API\index.php");
$result = json_decode($xml);
var_dump ($result);
?>
Index.php:
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$db=mysqli_select_db($conn,"login");
$sql ="SELECT * FROM loginuser WHERE ID=11" or die('MySQL Error.');
$result=$conn->query($sql);
$data = array();
while($data1 = $result->fetch_assoc())
{
$data[] = array('post'=>$data1);
}
$output = (array('posts' => $data));
$out =json_encode($output);
echo $out;
?>
i am trying to get output value of index.php in this file and i'm trying this given code but $result returns the null value.Where as the included file returns the desire value but in this case json_decode returns null value.

You can just change this:
echo $out;
to:
return $out;
And then you just can require the file into the other one like this:
$xml = require_once("C:\wamp\www\Sample API\index.php");
$result = json_decode($xml);
var_dump($result);

Related

I can't print the query results from my database through php, nothing happens

I'm trying to echo the query made to my database. So far the code runs but nothing happens. I'm very new at coding so help or an explanation about what I'm doing wrong would be awesome! Thanks a lot!!!
<?php
$servername = "servername";
// REPLACE with your Database name
$dbname = "dbsname";
// REPLACE with Database user
$username = "username";
// REPLACE with Database user password
$password = "password";
$ID = 1;
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
echo "Error de conexion.";
}
$sql = "SELECT * FROM registro WHERE ID = '1' ";
$result = $conn->query($sql);
while ($data = $result->fetch_assoc()){
$sensor_data[] = $data;
}
echo $data;
$conn->close();
Thanks everyone who replied, #AbraCadaver was right, I can't echo an array so I used print_r() instead and printed the variable $data. Now the php file prints the array with the query results. Thank you all!
$sql = "SELECT * FROM registro WHERE ID = $ID";
while ($data = $result->fetch_assoc()){
$sensor_data[] = $data;
print_r($data);
}

Unfortunately PHP file_get_contents not working

I am trying to fetch an url that is stored in the database. I am able to echo the url but when I try to use file_get_contents function to fetch the page, it says failed to open stream: No such file or directory
PHP code to get the data from database
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Job, Link FROM primarydata";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$job = $row["Job"];
$link = $row["Link"];
echo $job;
echo $link;
$htmlcontent = file_get_contents($link);
echo $htmlcontent;
}
} else {
echo "0 results";
}
$conn->close();
?>
I am able to echo the URL but in next link it doesn't execute the file_get_contents function

read a list of urls and read divs

Im trying to read the data of some divs in a list of urls, but i dont find the correct way, actually i only get a white page, but if do a print_r on the commented code (the $aElements) i can see the url loaded correctly, so i think the error is on the foreach, but i can't fix it, i need the link inside the div with the id=city
require_once('simple_html_dom.php');
set_time_limit (2000);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "buss";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT url FROM business";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$html = file_get_html($row["url"]);
$aElements = $html->find('div[id=city]', 0)->find('a');
$localList = array();
// here if i do a print_r i get the html of each url listed and is fine (of $aElements)
foreach($aElements as $element) {
if(substr($element->href, 0, 1) == '/') {
$ownerName = trim(str_replace('Punto de', '', $element->plaintext));
$localList[$artistName] = $element->href;
$file = fopen("newfile.txt", "a");
fwrite($file, $localList[$ownerName] = $element->href . PHP_EOL);
fclose($file);
}
}
}
} else {
echo "0 results";
}
$conn->close();

Formatting fwrite output to a external file - Implode Error

The code I am working on queries a database through php and then placed the results into an array called CS. This array is then encoded so it can work with javascript. It is then supposed to edit the output so there is a newline after every row. The latter is where I have the problem. I get implode: invalid arguments passed every time regardless of how I edit the implode function.
Here is the code:
<?php
$servername = "*****";
$username = "****"; --> edited for privacy.
$password = "*******";
$database = "********";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
// sql statement for tables and naming array to hold it in
$sql = "SELECT COURSE_ID FROM cs";
$result = $conn->query($sql);
$CS = array();
if ($result->num_rows > 0) {
// fill array with results
while($row = $result->fetch_assoc()) {
array_push($CS, $row);
}
} else {
echo "0 Results";
}
// encodes php array so it can be used in javascript
$json_array = json_encode($CS);
$conn->close();
// fills Computer_Science.js with the contents of the json_array and adds new lines in between
$json_array_lines = implode($json_array, "/n"); --> this line
$fp = fopen('..\js\DegreePlans\Computer_Science.js', 'w');
fwrite($fp, print_r($json_array_lines, TRUE));
fclose($fp);
?>
I'm at a loss on how to fix the error. Any help given will be appreciated.
I fixed it!
<?php
$servername = "*****";
$username = "****"; --> edited for privacy.
$password = "*******";
$database = "********";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
// sql statement for tables and naming array to hold it in
$sql = "SELECT COURSE_ID FROM cs";
$result = $conn->query($sql);
$CS = array();
if ($result->num_rows > 0) {
// fill array with results
while($row = $result->fetch_assoc()) {
array_push($CS, $row);
}
} else {
echo "0 Results";
}
$conn->close();
//encode the array so it can be used in javascript and use regular expressions to format it.
$json_string = json_encode($CS);
$re = "/.,/";
$subst = "},\r\n"; --> right here!
$json_string = preg_replace($re, $subst, $json_string);
$fp = fopen('..\js\DegreePlans\Computer_Science.js', 'w');
fwrite($fp, print_r($json_string, TRUE));
fclose($fp);
?>

PHP Database output. Arrays

In first case, i will get full array:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test.com";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM articles WHERE writer='$w_name' ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row[header];
}
}
mysqli_close($conn);
?>
In the second case, i will get just 1st value from array:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test.com";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM articles WHERE writer='$w_name' ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$q=$row[header];
}
}
mysqli_close($conn);
?>
<div>
<? echo $q ?>
</div>
What should I do, to make 2nd case work like 1st? I mean, how to put into $q, all values of array, not just the first.
You are not defining $q as an array at all.
$q=array();
...
while ($row = mysqli_fetch_assoc($result)) {
$q[]=$row['header'];
}
...
an example of output :
foreach($q as $header) {
echo $header.'<br>';
}
You are overwriting $q . Try using $q[]=
change $q=$row[header]; line to $q[]=$row[header];
and then not echo $q; but print_r($q);
Try to change this into the while
$q = array();
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
array_push($q, $row['header']); // i guess header is a column of the table
}
}
and then you must have your array when print the $q variable, i mean do an print_r($q)
More info about array_push: http://php.net/manual/es/function.array-push.php
Hope this helps :)

Categories