Unfortunately PHP file_get_contents not working - php

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

Related

Access database remotely to fetch data

I need to access a database remotely using PHP and display the fetched data in html. My code is as below
$servername = "192.168.56.1:3306";
$username = "root";
$password = "";
$dbname = "grh";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
error_log("Failed to connect to database!", 0);
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM ip_patient_logs";
$result = $conn->query($sql);
$rows = array();
if ($result->num_rows > 0) {
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
print json_encode($rows);
} else {
echo "0 results";
}
$conn->close();
I expected to be able to connect to the database and fetch the data , but it displays blank page. I am a beginner in PHP.

Echo only current user details

please can someone help me with this code? It shows all the users’ info but i need it to show only the info of the logged user.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "username";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT AEG FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 3) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["AEG"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Upon login, store the email/username/id in a $_SESSION variable;
$_SESSION['email'] = $email; // in this example I used email
Then on your file, you can access session variables using $_SESSION['variable'] and use it on your sql statement;
My modifications are the ones with comments.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "username";
/*Store session data in a variable*/
$email = $_SESSION['email'];
/**********************************/
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
/*Add where clause to your sql statement*/
$sql = "SELECT AEG FROM users WHERE email ='".$email."'";
/****************************************/
$result = $conn->query($sql);
if ($result->num_rows > 3) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["AEG"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Try this one if you are having 4 user then it is showing only one
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo "id: " . $row["AEG"]. "<br>";
}
}
else
{
echo "0 results";
}

Dynamically generate buttons with loop php

What I wan't to do is create buttons that are automatically generated from the database. So when I add a new record in the database the button is created Is this possible with a loop? So yes how do I create the button.
This is what I have so far:
<?php
$servername = "localhost";
$username = "root";
$password = "Iamthebest1009";
$dbname = "dktp";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM theme";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "". $row["theme_name"]. "<br>";
}
} else {
echo "no results";
}
$conn->close();
?>
Yes it is possible. you need to echo html
<?php
$servername = "localhost";
$username = "root";
$password = "Iamthebest1009";
$dbname = "dktp";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM theme";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$your_url ="https://www.google.com";
echo "". $row["theme_name"]. "<br>";
echo '<input type="button" name="' . $row["theme_name"]. '" value="'. $row["theme_name"].'">';
}
} else {
echo "no results";
}
$conn->close();
?>

MySQLi results disappear on webserver, but stick on localhost

I have no idea what's happening.
So on local host, everything loads perfectly fine:
Localhost
and this is the code for connecting:
$servername = 'localhost';
$username = 'root';
$dbname = 'x';
$password = '';
and on the webserver, it does not:
Webserver
and this is the code for the webserver:
$servername = 'localhost';
$username = 'parawtme_root';
$dbname = 'parawtme_xx';
$password = 'password_goes_here';
This is the only different bit in the code. Literally. What am I doing wrong?
My fetch script:
<?php
$sql = "$getdb
WHERE $tablenames.$pricename LIKE 'M9%'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo $tableformat;
while($row = $result->fetch_assoc()) {
include 'var.php';
echo $dbtable;
}
echo "</table>";
} else {
echo "0 results";
}
?>
Could it be do to a difference in different PHP servers?
Thanks.
Check connection error and mysqli query error
<?php
$servername = 'localhost';
$username = 'parawtme_root';
$dbname = 'parawtme_xx';
$password = 'password_goes_here';
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "$getdb
WHERE $tablenames.$pricename LIKE 'M9%'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo $tableformat;
while($row = $result->fetch_assoc()) {
include 'var.php';
echo $dbtable;
}
echo "</table>";
} else {
echo "0 results "; print_r(mysqli_error($conn));die;
}
?>

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);
?>

Categories