PHP Looping table contacts and creating multiple files - php

I have some code where I access the database and fetch all the data from a table called snippets.
Everything works fine except I now need to create a new html file and save it with the contents of a field called code for each.
So basically each "code" field is saved as a new html file.
Here is the current code:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT code FROM snippets";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//save each code record as a new html page
}
} else {
echo "0 results";
}
$conn->close();
How can I do that?

Try this...
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
// if you have id field in your record then try this way
file_put_contents($row['id'].".html", $row); // 1 full path with name 2 your data
}
} else {
echo "0 results";
}

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.

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

When I add a column, I can't retrieve it from mysql

After I add a column to my database, I want to retrieve it but not expected.
In PHP, I try reopening apache and mysql still not work.
Does anyone know how to resolve it? Thanks!
your question is not fully explanatory but with what I could try to understand you want to retrieve data or records from your database
you could try the code below and tweak it to work your way
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM databaseName";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data
while($row = $result->fetch_assoc()) {
print $row"<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

This page contains the following error: error on line 1 at column 1: Document is empty

Im unable to find out the solution , please help. below is the code. Thanks in advance
<?php
require_once('connect.php');
$sql = "select * from projet";
$result = $conn->query($sql);
$xml = new SimpleXMLElement('<xml/>');
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$mydata = $xml->addChild('mydata');
$mydata->addChild('Id',$row['idProjet']);
}
} else {
echo "0 results";
}
$conn->close();
header ("Content-Type:text/xml");
echo($xml->asXML());
?>
and the file connect.php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mtocrowdrise";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "connected succesfully";
Meanwhile i keep getting this error:
This page contains the following errors:
error on line 1 at column 1: Document is empty
Below is a rendering of the page up to the first error.
You shouldn't be writing/outputting any HTML to the page when said page is being cast as an XML Document (header ("Content-Type:text/xml");).
Remove echo "connected succesfully"; from connect.php.
You'll also (eventually) get the same error if:
...
} else {
echo "0 results";
}
...
header ("Content-Type:text/xml");
satisfies. So you should only cast the document to XML if there are no errors and there is actually some XML to display.
Something like the following would only set the Document to XML if there are results to display (per your original code):
require_once('connect.php');
$sql = "select * from projet";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$xml = new SimpleXMLElement('<xml/>');
// output data of each row
while($row = $result->fetch_assoc()) {
$mydata = $xml->addChild('mydata');
$mydata->addChild('Id',$row['idProjet']);
}
header ("Content-Type:text/xml");
echo($xml->asXML());
} else {
echo "0 results";
}
$conn->close();

If variable is in db then stop- if variable is not- then enter it

i have been trying to get this script done for a while now - im kind of new to php and mysql but i have been trying to get this to check the db for the username and then if the username exists - stop checking the db and if it doesn't exists add it to the db.
here is my code:
//input from application
$test = "wheelsmanx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT mainusername FROM CCCpro_test";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($row["mainusername"] === $test) {
echo "User Name Already In Use.";
}if($row["mainusername"] !== $test){
echo "this statement";
[code that inserts into db i can do this part myself]
}
}
$conn->close();
} else {
echo "0 results";
}
$conn->close();
The problem with your code is that you do the INSERT of the new name inside an if statement that has confirmed the existence of that user already. In addition I think you messed up your SELECT statement by selecting all the users.
Look into INSERT ON DUPLICATE for a better way to do it, or revise your code as below.
$sql = "SELECT mainusername FROM CCCpro_test WHERE mainusername = $test";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "User Name Already In Use.";
}
else{ //no rows selected therefore the user doesn't exist
[code that inserts into db i can do this part myself]
}
$conn->close();
PLEASE READ I have somewhere to go so I am being lazy so I did not bind the $test variable therefore DO NOT copy and paste this code without updating it to bind the $test variable. Please read this post about PDO and variable binding to prevent SQL injection.
here is my full working code if anyone needs it - it uses the post method - from an html form .... in case some one needs to hack it to pieces for something else
well guys i appreciate all of your help :D but i have found an answer or a way around it i suppose- i thought of it all night and day on how i could make it work and i came up with this
$servername = "127.0.0.1";
$username = "TESTUSER";
$password = "TESTPASS";
$dbname = "TESTDB";
$testusername = $_POST['mainusername'];
$testpassword = $_POST['mainpassword'];
//input from application
$test = $_POST['mainusername'];
$test2 = "0";
//Count switch
$countswitch = "0";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql1 = "INSERT INTO CCCpro_test ( mainusername, mainpassword ) VALUES ('$testusername','$testpassword' )";
$sql = "SELECT mainusername FROM CCCpro_test";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($row["mainusername"] === $test) {
echo "Im Sorry Username Already In Use";
$countswitch ++;
}
}
if($countswitch == $test2){
echo "User Name Registered";
$db_handle = mysql_connect($servername, $username, $password);
$db_found = mysql_select_db($dbname, $db_handle);
if ($db_found) {
$result1 = mysql_query($sql1);
mysql_close($db_handle);
}
}
if ($countswitch == 3){
echo "this";
}
} else {
echo "0 results";
}
$conn->close();

Categories