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.
Related
and sry in advance because i'm new here and i'm completly new in php/ajax coding.
I use a free script (Ajax vote up/down) from this site
I have a simple php page for my shoutcast server to show the song played
<?php
require_once "inc.php";
$array = array(); // Let's store our shoutcast variables into an array.
$array['host'] = "xxx.xxx.xx.xxx"; // Your Shoutcast Host
$array['port'] = "xxxx"; // Your Shoutcast Port
$array['extra'] = "/admin.cgi?sid=1&mode=viewxml&page=1"; // The bit that follows in the url to access the xml of the stats
$array['user'] = "xxxxx"; // Admin username (Default is usually "admin")
$array['password'] = "xxxxxxx"; // Admin Password
$radioStats = new radioStats( $array['host'], $array['port'], $array['extra'], $array['user'], $array['password']);
$returnStats = $radioStats->returnStats();
$song = $returnStats['currentSong'];
?>
<div align="center">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<link href="votingfiles/voting.css" rel="stylesheet" type="text/css" />
<script src="votingfiles/voting.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
setInterval(function(){cache_clear()},54000);
});
function cache_clear()
{
window.location.reload(true);
}
</script>
<div id="radio_stats">
<?php
if( $returnStats['serverStatus'] != 0 ) {
?>
<?php
if( $returnStats['currentSong'] != "" ) {
echo $returnStats['currentSong'];
} else {
echo "Undefined";
}
?></div>
<br /><br />
<div class="vot_updown1" id="vt_$song"></div>
<?php
}
else {
?>
This radio server appears to be offline.
<?php
}
?>
My problem is :
All request Php/ajax/mysql works but actually when i make a vote, it's registered in the db like :
vt_$song 1 0
How can i do to get the real name of the song like the original php request do :
echo $returnStats['currentSong']; or echo $song;
To register in the db like :
vt_Kidd Guti-Step Everything 1 0 (or any other song played)
Example : [HERE]
Thanks in advance for any help.
In this line the $song is not parsed as PHP so the literal text $song is showing:
<div class="vot_updown1" id="vt_$song"></div>
Try:
<div class="vot_updown1" id="vt_<?php echo $song; ?>"></div>
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
I have 3 php pages.
Counter.php - I want this file to display how many clicks Counter 2, Counter 3 got.
<!DOCTYPE html>
<html>
<head>
<title>Counter</title>
</head>
<body>
You visited page Counter2
<?php include 'Counter2.php'; echo $Counter; ?>
Times.
You visited page Counter3
<?php include 'Counter3.php'; echo $Counter3; ?>
Times.
</body>
</html>
Counter2.php - I want to count the amount of click this page has had.
<?php
session_start();
if(isset($_SESSION["counter"]))
{
$counter = $_SESSION["counter"];
}
else
{
$counter = 0;
}
$_SESSION["counter"] = $counter + 1;
?>
<!DOCTYPE html>
<html>
<head>
<title>Counter</title>
</head>
<body>
<?= $counter ?>
</body>
</html>
Counter3.php - I want to count the amount of click this page has had.
<?php
session_start();
if(isset($_SESSION["counter3"]))
{
$counter3 = $_SESSION["counter3"];
}
else
{
$counter3 = 0;
}
$_SESSION["counter3"] = $counter3 + 1;
?>
<!DOCTYPE html>
<html>
<head>
<title>Counter</title>
</head>
<body>
You visted this page this many times:
<?= $counter3 ?>
</body>
</html>
Now, everything is working how it should be, HOWEVER, when I include the php files <?php include 'Counter3.php'; echo $Counter3; ?>
to log the amount of clicks it continues to increase when I load the Counter.php file. I do not want it to, I just want Counter.php to log how many clicks Counter2 and Counter3 has had.
And stop Counter.php increasing the click number.
You can use $_SERVER['REQUEST_URI'] to add an extra if to both counter2.php and counter3.php, and if the $_SERVER['REQUEST_URI'] points to counter.php, you can skip incrementing the counters
There are several options for this problem:
Save both values in a database and access the values through counter.php, instead of including the files
Use $_SERVER['REQUEST_URI'] to let the counter only increment if the URI does not contain counter.php
Including a page makes the code run, so it would increase all the counters in the pages. Instead just use the $_SESSION[] variable directly.
<?php
session_start()
?>
<!DOCTYPE html>
<html>
<head>
<title>Counter</title>
</head>
<body>
You visited page Counter2 <?php echo $_SESSION["counter2"]; ?> Times.
You visited page Counter3 <?php echo $_SESSION["counter3"]; ?> Times.
</body>
</html>
Also, in PHP you can do the following:
$_SESSION["counter3"] = isset($_SESSION["counter3"]) ? $_SESSION["counter3"] : 0;
//$_SESSION["counter3"] = $_SESSION["counter3"] ?? 0; // only in PHP 7
Also on a side note, you don't have to create this code on every page as it might get messy in the long run.
<?php #counter.php
if(session_status() == PHP_SESSION_NONE){
session_start();
}
if(isset($_SESSION['counter'][$_SEVER['REQUEST_URI']])){
$_SESSION['counter'][$_SERVER['REQUEST_URI']] = $_SESSION['counter'][$_SEVER['REQUEST_URI']] + 1
} else {
$_SESSION['counter'][$_SERVER['REQUEST_URI']] = 1;
}
function countpagetimes(){
return $_SESSION['counter'][$_SERVER['REQUEST_URI']];
}
?>
<?php #somepage.php
include 'counter.php';
echo 'You visited this page ' . countpagetimes() . ' times'.
?>
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 "...
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.