I have made a very simple PHP7 page that shows the values of temperature sensors in a table. I extract this data from a MySQL database. The PHP7 is running with apache2 on a raspberrypi 3.
This all works well and I am able to get the code to run on the CLI and it generates HTML just fine. But when I run the page in the browser it only shows what comes before and after the while loop. All the other PHP/HTML shows fine. the file name is tempsense.php
<html>
<head>
<title>
TEMP
</title>
</head>
<body>
HOME
<?php
$con = mysqli_connect("localhost","root","*********");
$db = mysqli_select_db($con,"connected_sensors");
$query = "SELECT * FROM sensors";
$result = mysqli_query($con, $query);
echo "<table border='1'>";
echo "<tr>";
echo "<th>Sensor ID</th><th>Sensor Name</th><th>Temp F</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>", $row['sensor_id'], "</td><td>", $row['sensor_name'], "</td><td>", $row['sensor_value'], "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
The source from the browser is
<html>
<head>
<title>
TEMP
</title>
</head>
<body>
HOME
<table border='1'><tr><th>Sensor ID</th><th>Sensor Name</th><th>Temp F</th></tr></table> </body>
and from the PHP CLI
<html>
<head>
<title>
TEMP
</title>
</head>
<body>
HOME
<table border='1'><tr><th>Sensor ID</th><th>Sensor Name</th><th>Temp F</th></tr><tr><td>28-0517c15db7ff</td><td>Device_0</td><td>79.5866</td></tr><tr><td>28-0117b3e303ff</td><td>Device_1</td><td>79.1366</td></tr></table> </body>
I know this is probably dumb but I'm loosing my mind.
Thanks in advance.
Related
I am working on a PHP project that needs to send data to Pastel.
How to connect to pastel's pervasive and insert the records directly for the pastel to use?
Using PHP on Windows is relatively easy. You'll need to create an ODBC DSN pointing to your database. Then, you can use ODBC to connect. Here's a very simple sample using the PSQL demodata:
<html>
<head>
<title>Title</title>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<META NAME="Author" CONTENT="Mirtheil Software">
</head>
<body>
<h1>Title</h1>
<?php
$conn=odbc_connect("demodata","","");
$rs=odbc_exec($conn,"select * from Class");
echo "<table border=1>\n";
$numfields = odbc_num_fields($rs);
for($i=1;$i<=$numfields;$i++){
$fn=odbc_field_name($rs,$i);
echo "<th>$fn</th>";
}
echo "\n";
while(odbc_fetch_row($rs)){
echo "<tr>\n";
for($i=1;$i<=$numfields;$i++){
$fv=odbc_result($rs,$i);
echo "<td>$fv</td>";
}
echo "</tr>\n";
}
echo "</table>\n";
echo "<p>Number of Fields: $numfields</p>\n";
?>
</body>
</html>
I'm fetching some data from a MySQL database onto a WebPage. I came up with a simple PHP page that queries the database and shows the query in the form of a table and it works just fine.
However, when I try to add bootstrap to the page and style it, the entire page stops working.
Here's my index.php
<!DOCTYPE html>
<html>
<head>
<title>MakersBox</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<center>
<h1>Sensor Testing Dashboard</h1>
<div class="container">
<?php
echo "<table class='table table-hover' align=center>";
echo "<thead><tr><th>Sensor Id</th><th>Temperature</th><th>Light</th><th>Last Updated</th></tr></thead>";
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td>" . parent::current(). "</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "\n";
}
}
$servername = "localhost";
$username = "root";
$password = "paanikiboond";
$dbname = "testbox1";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM sensor ORDER BY LastUpdate DESC LIMIT 1");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
</div>
$url1=$_SERVER['REQUEST_URI'];
header("Refresh: 3600 ; URL=$url1");
?>
</body>
</html>
Any idea what I may be doing wrong here?
First, I think you must separte properly your HTML, and your PHP.
In your code, mixte HTML tag + HTML tag from PHP code in your HTML body is... not too regulatory. And very difficult to maintain.
You can, create an architecture like that :
<?php
// Create connexion, and get your results.
$connexion = new PDO();
$results = [];
?>
<!DOCTYPE html>
<html>
<head>
<!-- Your link and scripts-->
</head>
<body>
<h1></h1>
<div>
<?php if (!empty($results){ ?>
<table class='table table-hover'>
<!-- Show your results. -->
<? foreach($results as $result)
{
}
?>
</table>
<?php }else{
echo 'no results found';
}
?>
</div>
</body>
</html>
Also, Boostrap is a Javascript library, It doesn't work directly on PHP, but it read the HTML structure, with your elements and their classes. If your javascript is loaded, and your structure is well done, that work! You have samples and documentation of Boostrtap here;
In your code you forgotten to close your <head></head> element and you've a problem in this lines :
echo "</table>"; // that is PHP, but PHP is not close with `?>`
</div> <!-- That is HTML but in PHP code witouth `echo ''` -->
If PHP and HTML has well separted, you will have fewer problems of forgetting :D You have many links in Internet for understand this good practise
PS : don't forgot to code with proper line indentions: it's much more readable:
<html>
<head> <!-- Very difficult to see we havn't a </head> close tag -->
<body>
<div>
My content
</div>
</body>
</html>
<html>
<head> <!-- Very easy to see we havn't a </head> close tag -->
<body>
<div>
My content
</div>
</body>
</html>
Hope I help you.
for one thing you arent closing your <head>, so your code should be
<!DOCTYPE html>
<html>
<head>
<title>MakersBox</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
...
EDIT: close to the bottom your code should look like
...
echo "</table>";
echo "</div>";
$url1=$_SERVER['REQUEST_URI'];
header("Refresh: 3600 ; URL=$url1");
?>
</body>
</html>
</center><!--that where you forgot to add this as it needs to be closed off-->
</body>
</html>
Also, you might be better off put Javascript on the bottom of the pages, Not sure if it will work.
Second things you need use Web developer tools to diagnosed this issues. it will help a lot. also, you might need to test out different browser if it's same issues or not.
I tried your code. I only find these three mistakes:
You must use echo for build HTML code from PHP and then close your
div container
$conn = null;
echo "</table>";
//wrong way expecting statement
</div>
//correct way for use HTML inside PHP tags
echo "</div>";
The line where you add a header "refresh" generate a warning because
"Cannot modify header information - headers already sent"
$url1=$_SERVER['REQUEST_URI'];
header("Refresh: 3600 ; URL=$url1");
You use <center> tag that it's deprecated so you should use CSS instead and you not close this tag
i am trying to buid a Php site that show data from my MySQl database.
and i think im almost there, everything works except the Pictures.
i cant get my php site to show the pictures with the picture reference from Sql database.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
$con=mysqli_connect("localhost","root","","db1");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<head>
<link rel="stylesheet" href="styles.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>untitled</title>
</head>
<body>
<div class="content">
<?php
$sql = "SELECT id, Producent, Model, kategori FROM tb1";
$result = mysqli_query($con,"SELECT * FROM `tb1");
echo "<table>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>";?> <img scr="<?php echo $row["Billedurl"]; ?>"/> <?php echo " </td>";
echo "<td>" .$row["Producent"] .$row["Model"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<!-- end .content --></div>
<!-- end .container --></div>
<div class="footer"><br>
<!-- end .footer --></div>
</body>
</html>
You need to select all the columns you are going to use, so if you need Billedurl, you should change:
$sql = "SELECT id, Producent, Model, kategori FROM tb1";
to:
$sql = "SELECT id, Producent, Model, kategori, Billedurl FROM tb1";
Now the value of that column will be available in $row["Billedurl"].
Edit: It seems that now you have the correct value in your html, but the path to the image is not correct as it is a relative path.
You should prefix your variable with the correct folder so that the image is found by the browser. That can be as simple as just using an absolute path but that depends on where the pic/ directory is located.
So if your variable contains pic/l_jabra_evolve80.jpg" and the pic/ folder is on the root of the web-server, you can do something like:
# before the loop
$imagePrefix = '/';
# in the loop
... <img scr="<?php echo $imagePrefix . $row["Billedurl"]; ?>"/> ...
Now the browser will try to fetch the image from /pic/l_jabra_evolve80.jpg.
You need to do little bit of debugging:
first change little bit in your query
$sql = "select * FROM tb1";
$result = mysqli_query($con, $sql);
Then try to check print_r($result) and check if you are getting everything here or not.
I am doing a project using my university web server where my httpdocs are stored.
Everything works fine including my other PHP scripts which connect to phpmyadmin database both posting and getting from the database. However when I put this script on the web server and try to access it the page outputs some of the PHP code and I cannot find out why. I am not seeing any open tags etc. The page looks like this:
<!DOCTYPE html>
<html>
<head>
<title>Gallery</title>
Home<br />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
mysql_connect("XXXXXXX","XXXXX","XXXXXXXX","XXXXXXXXXXX");
mysql_select_db("sql1103884");
$res=mysql_query("SELECT * FROM Images");
echo "<table>";
while($row=mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>";?> <img src="<?php echo $row["imagepath"]; ?>" height="100" width="100"> <? php echo "</td>";
echo "<td>"; echo $row["name"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
If your file extension is not php then please change it to .php and second replace with below code. there is space between php opening tag like this <? php so it should be <?php
<!DOCTYPE html>
<html>
<head>
<title>Gallery</title>
Home<br />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
mysql_connect("XXXXXXX","XXXXX","XXXXXXXX","XXXXXXXXXXX");
mysql_select_db("sql1103884");
$res=mysql_query("SELECT * FROM Images");
echo "<table>";
while($row=mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>";?> <img src="<?php echo $row["imagepath"]; ?>" height="100" width="100"> <?php echo "</td>";
echo "<td>"; echo $row["name"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
i have this code in php. i want my images displayed to be horizontally displayed. those images are saved in xampp. can anybody help me? this is my code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
mysql_connect("localhost","root","");
mysql_select_db("dbLetters");
$res=mysql_query("select * from tbLetters");
echo "<table>";
while ($row = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>";?> <img src = "<?php echo $row["images"]; ?>" height="200" width="200"> <? php echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
your response will be a big help. thanks. :))
Unless you use a data url, you actually need to set up a separate script to pull that will provide the image to the browser.
This will make your image url something like image.php?id=<image_id>. The image.php script will fetch the image and provide the image to the browser.
Its not usually a good idea to store image data in mysql.