include ('xyz.php') in php file doesn't work - php

I'm trying to include a php file inside another php file, but it is not working and I don't know why.
Moreover, I'm getting no erroes. allow_url_include is enabled in php.ini file.
I'm using XAMPP server.
Below here is part of my code:
q.php
<div class="article">
<? php
include ('a.php');
?>
</div>
where a.php simply has echo statement:
echo "hello";
I'm posting bigger section of my code now.
<div class="artical">
<?php
$username = "root";
$password = "";
$database = "techinsight";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $username, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found)
{
$SQL = "SELECT * from questions";
$result = mysql_query($SQL);
while($db_field=mysql_fetch_assoc($result))
{
$x = $db_field['Qid'];
while($x==1 && $x==NULL)
{
$SQL = "SELECT * from questions";
$result = mysql_query($SQL);
$db_field = mysql_fetch_assoc($result);
$x = $db_field['Qid'];
}
}
if($x==$x)
{
for($x; $x>0; $x--)
{
$SQL = "SELECT * from questions WHERE Qid=$x";
$result = mysql_query($SQL);
$db_field = mysql_fetch_assoc($result);
$str_que = $db_field['question'];
echo "<div class='dabba'>
<div class='block_a'> <?php include('a.php'); ?> //here it is.
</div> <br>
<div class='block_b'>
it is 2nd section. <br>
</div><br>
<div class='block_c'>
last one.<br> </div>
</div> <br><br>";
}
}
}
?>
</div>

Try:
<?php //Before you had <? php <--
include "a.php";
?>
Make sure the files are in the same directory.

You have a space between <? and php
Remove it.

It can be a couple of things...
1. Place the included file in the correct folder
Make sure that when you use include you either provide the folder path correctly or place the file in the same folder as where you reference / call it from.
2. It is <?php and not <? php
You have a space too much. Actually, if you use <? php I believe PHP will look for a function called php as <? may also be an opening tag (assuming short tags are activated in your php.ini file: short_open_tag=On).
Bonus: make sure error reporting is turned on while debugging
Another thing that I'd recommend you to do is to setup your php.ini file to report for all errors while debugging:
error_reporting(E_ALL);
I haven't tested it, but I'm pretty sure your above code would have resulted in a notification on the missing function php.
And the real bonus (following your updated post) - what you did wrong
You have added the include inside echo - obviously it won't work. So replace this...
echo "<div class='dabba'>
<div class='block_a'> <?php include('a.php'); ?> ...
...with this...
echo "<div class='dabba'>
<div class='block_a'>"; include('a.php'); echo "...

Related

Running PHP on a new apache server and Its not working

I have an Apache server on my Mac but I recently got a PC so I'm moving my code over to it.
On my mac this code works:
db.php:
<?php
$url = "127.0.0.1";
$user = "username";
$pass = "password";
$conn = mysqli_connect("$url","$user","$pass","space");
?>
index.php
<html>
<head>
<?
include 'db.php';
?>
<!-- Header stuff -->
</head>
<body>
<!-- Body stuff -->
<?php
$spaceSelect = mysqli_query($conn, "SELECT * FROM `world`");
$i = 1;
while($space = mysqli_fetch_array($spaceSelect)){
echo "<button id='sector' data-toggle='modal' data-target='#".$space['id']."Modal'>".$space['id']."</button>";
$i += 1;
if($i % 10 == 1){
echo "<br>";
};
echo "stuff";
};
?>
</body>
</html>
When I run this on the new server I get the error
Notice: Undefined variable: conn in S:\shared\space\index.php on line 30
This fixes if I move "include db.php"
index.php
<html>
<head>
<?
?>
<!-- Header stuff -->
</head>
<body>
<!-- Body stuff -->
<?php
include 'db.php';
$spaceSelect = mysqli_query($conn, "SELECT * FROM `world`");
$i = 1;
while($space = mysqli_fetch_array($spaceSelect)){
echo "<button id='sector' data-toggle='modal' data-target='#".$space['id']."Modal'>".$space['id']."</button>";
$i += 1;
if($i % 10 == 1){
echo "<br>";
};
echo "stuff";
};
?>
</body>
</html>
This is really annoying me and I wasn't getting this error when the server was on my Mac. Is there something I need to change in httpd.conf?
1st : Find the php.ini file and
You should find a line that says
short_open_tag = Off
change this to:
short_open_tag = On
If the line doesn't exist just add it.
2nd : After editing php.ini file restart the apache service
try a include_once in the header section. Normally, changes in the httpd.conf are only needed if you use sub folders.

Database path on raspberry

I'm new in raspberry and I'm trying to read a simple database from a php page but there is something wrong: I can't read the database content: Here the php page code:
<!DOCTYPE html>
<html>
<body>
<H1>Test Database</H1>
<?php
$sq = sqlite_open('miodatabase.db', 0666, $sqlite_error);
if(!$sq)
{
die(“Errore Sqlite: “.$sqlite_error);
}
$result = sqlite_query($sq, 'select * from test');
while($data = sqlite_fetch_array($result))
{
echo $data[‘nome’];
}
sqlite_close($sq);
?>
</body>
</html>
The database is "miodatabase" that contains a table called "test". I put the database in \var\www\html folder (is correct?) but when I open the page I see a blank page. I'm sure the database contains the table (tested with sqlite3 commands) and the table contains one row. Where I need to put the database? Why I can't see nothing? Thanks
If you don't see anything, i think it's a php configuration issue.
Did you try to add :
<?php phpinfo(); ?>
at the beginning of your script. If you have blank page, look your apache/nginx configuration.
You can also try to run your script from the command line. Maybe helpful in some case.
UPdate
If you use SQLite3 follow this code
class MyDB extends SQLite3
{
function __construct()
{
$this->open('miodatabase.db');
}
}
$db = new MyDB();
$result = $db->query('select * from test', SQLITE3_OPEN_READWRITE );
//var_dump($result->fetchArray());
while($data = $result->fetchArray())
{
echo $data[‘nome’];
}
$db->close();
I solved: I type again the command
sudo apt-get install php5-sqlite
sudo /etc/init.d/apache2 restart
and then I can see in the php info the sections about sqlite3.
The I update my php page like this:
//Enable show error
ini_set('display_errors', 'On');
error_reporting(E_ALL|E_STRICT);
$db = new SQLite3("miodatabase.db");
$sql_select='SELECT * FROM test';
$result=$db->query($sql_select);
echo "<table border='1'>";
echo "<tr>";
$numColumns=$result->numColumns();
for ($i = 0; $i < $numColumns; $i++)
{
$colname=$result->columnName($i);
echo "<th>$colname</th>";
}
echo "</tr>";
while($row = $result->fetchArray(SQLITE3_NUM))
{
echo "<tr>";
for ($i = 0; $i < $numColumns; $i++)
{
$value=$row[$i];
echo "<th>$value</th>";
}
echo "</tr>";
}
echo "</table>";
To open the database I use now
$db = new SQLite3("miodatabase.db");
because the sqlite_open is not supported by this version of sqlite.
Now all works correctly

How to convert dynamic php file which accepts data from database to pdf?

I am working on WordPress website. And on front-end I am giving the functionality to create a resume online & on submitting the form he will get a option to download & print the resume in PDF format. Till now everything is working fine If I am passing a simple html file to convert into PDF. But I want to create a PDF out of the fields of resume inserted in the database. If I am passing the plain html file for conversion it works fine.
But what if I want to create a dynamic PDF file of the PHP file.
Here is my tried code:
I have used html2pdf conversion library for this.
This is a file from which the call to test.php goes.
require("html2pdf/html2fpdf.php");
$htmlFile = "test.php";
$buffer = file_get_contents($htmlFile);
$pdf = new HTML2FPDF('P', 'mm', 'Letter');
$pdf->AddPage();
$pdf->WriteHTML($buffer);
$pdf->Output('test.pdf', 'F');
And the test.php file is:
<html>
<body>
<?php
$username = "";
$password = "";
$hostname = "";
$dbname = "";
// Create connection
$con = mysqli_connect($hostname, $username, $password, $dbname);
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo $result = mysqli_query($con,"SELECT * FROM wp_resume ORDER BY id DESC LIMIT 1");
echo "hioooooo";
while($row = mysqli_fetch_array($result))
{
?>
<div>
<label>First Name</label><script type="text/php"><?php echo $row['firstName']; ?></script><br>
<label>Last Name</label><script type="text/php"><?php echo $row['lastName']; ?></script><br>
</div>
<?php
}
?>
</body>
</html>
Now after submitting the form I am getting a PDF file which contains only this data:
First Name
Last Name
And I want all the details of the entry inserted. Clearly this ignores the code inside the <?php ?> tags. So even if I used var_dump(), I didn't get anything.
SO please help me out in this guys. How can I pass the PHP data to PDF file.
Hey I got the solution for this. And the correct code is:
require("html2pdf/html2fpdf.php");
$html = '<h2>Resume</h2>';
$query = "SELECT * FROM wp_resume order by id desc limit 1";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result))
{
$html .= '<div>
<label>First Name</label> '. $row['firstName'].'<br>
<label>Last Name</label> '. $row['lastName']. '<br>
</div>';
}
$pdf = new HTML2FPDF('P', 'mm', 'Letter');
$pdf->AddPage();
$pdf->WriteHTML($html);
$pdf->Output('test.pdf', 'F');
I hope this will help somebody....
file_get_contents returns the content of a file, without it being parsed as PHP...
Replace
$buffer = file_get_contents($htmlFile);
With
ob_start();
include($htmlFile);
$buffer = ob_end_clean();
What we are doing: opening an output buffer (this prevents the echo from going to the client as they appear) then we include the PHP file so it gets parsed and all. Remember all the echo's are in the output buffer, so grab it with ob_end_clean() and use the generated HTML to make a PDF.

Echo php code as text

I want to echo block of code which is dynamically generated. For example:
<?php
$cid = $camp_id;
$hostname = "$host";
$db_user = "$dbuser";
$db_pass = "$dbpass";
$db_name = "$dbname";
$mysqli = new mysqli();
$mysqli->connect($hostname, $db_user, $db_pass, $db_name);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}
etc....
?>
I got access to $camp_id and other variables because they are in the file which is included.
I tried to store this code in variable with < pre> and < code> tag and echo after that but couldn't make it work.
Also how can I insert $camp_id to this. Below is example what I think (I know it's not correct just for understanding.
$generated_code = "<.code><?php $cid = <?php echo $camp_id;?> $hostname = $host; etc... </code > ?>";
I used space and dot before code and pre because if not it doesn't show as tag..
Thanks
You could also try like this:
<?php
ob_start();
?>
<code>$cid = <?php echo $camp_id; ?> , $hostname = <?php echo $host; ?></code>
<?php
echo ob_get_clean();
?>
Depending on the circumstances and what your code is like, using ob_start() and ob_get_clean() functions allow your code to be more legible in color coded IDEs, since your output wont look like one solid block of color, instead it will be styled like it should in html for better readability.
You need to follow the rules for strings in PHP, and next to that you need to follow the rules for HTML, or better, output plain text:
<?php
header('Content-Type: text/plain;');
echo '<?php
$cid = ' . $camp_id .';
etc....
?>';
Using EOF inside single quote have better result. But No space after 'EOF'
<?php
$head= <<<'EOF'
<?php $var=2; ?>
EOF;
?>

Use php for Output Buffering and jQuery to send ob_get_contents

I am trying to capture the contents of my php page using output buffering:
<?php
function connect() {
$dbh = mysql_connect ("localhost", "user", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db("PDS", $dbh);
return $dbh;
}
session_start();
if(isset($_SESSION['username'])){
if(isset($_POST['entryId'])){
//do something
$dbh = connect();
$ide = $_POST['entryId'];
$usertab = $_POST['usertable'];
$answertable = $usertab . "Answers";
$entrytable = $usertab . "Entries";
$query = mysql_query("SELECT e.date, q.questionNumber, q.question, q.sectionId, a.answer FROM $answertable a, Questions q, $entrytable e WHERE a.entryId = '$ide' AND a.questionId = q.questionId AND e.entryId = '$ide' ORDER BY q.questionNumber ASC;") or die("Error: " . mysql_error());
if($query){
//set variables
$sectionOne = array();
while($row=mysql_fetch_assoc($query)){
$date = $row['date'];
$sectionOne[] = $row;
}
}else{
//error - sql failed
}
}
?>
<?php
ob_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script src = "jQuery.js"></script>
<script>
$(document).ready(function(){
$("#export").click(function(e){
//post to html2pdfconverter.php
$("#link").val("<?php echo(ob_get_contents()); ?>"); //THIS DOESN'T WORK
$("#nm").val("Entry Report.pdf");
$("form#sendanswers").submit();
});
});
</script>
<title>Personal Diary System - Entry Report - <?php echo($date); ?></title>
</head>
<body>
<h1>Entry Report - <?php echo($date); ?></h1>
<div id = "buttons">
<form id = "sendanswers" name = "sendanswers" action="html2pdfconverter.php" method="post">
<input type = "hidden" name = "link" id = "link" value = "">
<input type = "hidden" name = "nm" id = "nm" value = "">
<input type = "button" name = "export" id = "export" value = "Export As PDF"/>
</form>
</div>
<h3>Biological Information</h3>
<?php
echo('<p>');
$i = 0;
foreach($sectionOne as &$value){
if($i == 1 || $i == 3){
$image = "assets/urine".$i.".png";
echo("<br/>");
echo($value['question']." <br/> "."<img src = \"$image\"/>");
echo("<br/>");
}else{
echo($value['question'].' : '.$value['answer']);
}
echo("<br/>");
$i++;
}
echo('</p>');
?>
</body>
</html>
<?php
}
$contents = ob_get_contents(); //THIS WORKS
ob_end();
?>
I assign the contents of ob to $contents using ob_get_contents(); This works, and echoing $contents duplicates the html page.
However, in my jQuery, I am trying to assign this to a hidden text field ('link') using:
$("#link").val("<?php echo($contents); ?>");
This doesn't work however..And I have a feeling its because I am accessing $contents too eraly but not too sure...any ideas?
$("#link").val("<?php echo(ob_get_contents()); ?>"); //THIS DOESN'T WORK
at the point you do that ob_get_contents call, you've only output about 10 lines of javascript and html. PHP will NOT reach back in time and magically fill in the rest of the document where you do this ob_get_contents().
You're basically ripping the page out of the laser printer the moment the page starts emerging, while the printer is still printing the bottom half of the page.
I fail to see why you want to embed the contents of your page into an input field. If you want to somehow cache the page's content in an input field, you can just use JS to grab the .innerHTML of $('body').
Well, you have two problems.
The first is what you suspect. You can't access that stuff until later. The second problem which you may not realize is that you will have quoting issues in JavaScript even if you manage to find a way to reorder this and make it work. It's recursive, in a bad way.
What you should do instead is change your $('#export').click handler to do an Ajax call, render the HTML you need to appear in the link on the server in a separate PHP script (no output buffering necessary) and then have your code inject the result of that call into the page the way you're trying to do in your click handler now.

Categories