php code display code in browser - php

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>

Related

php while loop runs in shell but not on the browser

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.

Custom MySQL & PHP Forum - List Topics Under Categories

I'm working on a custom MySQL & PHP forum, and I'm trying to get my topics to be seated/listed under their respective categories. When I create a topic in my admin panel I can set the category id, which corresponds with the id of the category. I've already browsed around quite a bit on the web and on the site here.
I've been able to get the categories to show up and have been able to seat where the topics would go, but the topic(s) aren't showing up, the param I pass keeps resulting in null, and causes each category to show that there are no topics created.
This is the function I'm working on:
function fetch_forum() {
global $connection;
$query = "SELECT * FROM forum_categories LEFT JOIN forum_topics ON forum_topics.forum_topic_category_id = forum_categories.forum_category_id";
$select_forum_data = mysqli_query($connection, $query);
$new_topic = "";
$pre_topic = "";
while($row = mysqli_fetch_assoc($select_forum_data)) {
$forum_topic_id = $row['forum_topic_id'];
$forum_topic_title = $row['forum_topic_title'];
$forum_topic_description = $row['forum_topic_description'];
$forum_topic_category_id = $row['forum_topic_category_id'];
$forum_category_id = $row['forum_category_id'];
$forum_category_title = $row['forum_category_title'];
$new_topic = $forum_topic_title;
if($forum_category_title != null) {
echo "<thead>";
echo "<tr>";
echo "<th><span class='glyphicon glyphicon-file' aria-hidden='true'></span> {$forum_category_title}</th>";
echo "<th>Topics</th>";
echo "<th>Posts</th>";
echo "<th>Last Post</th>";
echo "</tr>";
echo "</thead>";
}
if($new_topic != $pre_topic) {
echo "<tbody>";
echo "<tr>";
echo "<td><a href='topic.php?t_id=<?php echo $forum_topic_id; ?>'><h5> {$forum_topic_title}</h5></a></td>";
echo "<td>1</td>";
echo "<td>1</td>";
echo "<td>03-28-2017</td>";
echo "</tr>";
echo "</tbody>";
} else {
echo "<tbody>";
echo "<tr>";
echo "<td>No topics have been created</td>";
echo "</tr>";
echo "</tbody>";
}
$pre_topic = $forum_topic_title;
}
}
This is my index page:
<?php include "includes/header.php"; ?>
<?php include "includes/db.php"; ?>
<!-- Navigation -->
<?php include "includes/navigation.php"; ?>
<!-- Page Content -->
<div class="container">
<div class="row">
<!-- Blog Entries Column -->
<div class="col-md-8">
<!-- Forum Navigation -->
<?php include "includes/forum_navigation.php"; ?>
<!-- First Blog forum -->
<table class="table table-hover">
<?php fetch_forum(); ?>
</table>
<hr>
</div>
<!-- Blog Sidebar Widgets Column -->
<?php include "includes/sidebar.php"; ?>
</div>
</div>
<!-- /.row -->
<hr>
<?php include "includes/footer.php"; ?>
This is my header page:
<?php ob_start(); ?>
<?php session_start(); ?>
<?php include "functions.php"; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>My CMS Framework</title>
<!-- Bootstrap Core CSS -->
<link href="../css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="../css/blog-home.css" rel="stylesheet">
<link href="../css/style.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="../admin/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<script type="text/javascript" src="../../js/autoBreadcrumbs.js"></script>
</head>
<body>
And this is my forum: http://innovativestudios.net/forum/
FWIW your code works for me and produces:
using this assumed DB structure and dataset.
Could it be just a problem with your test data set?
Had to change the function a little bit, but this is the final result:
function fetch_forum() {
global $connection;
$query = "SELECT * FROM forum_categories LEFT JOIN forum_topics ON forum_topic_category_id = forum_category_id";
$select_forum_data = mysqli_query($connection, $query);
$new_topic = "";
$pre_topic = "";
while($row = mysqli_fetch_assoc($select_forum_data)) {
$forum_topic_id = $row['forum_topic_id'];
$forum_topic_title = $row['forum_topic_title'];
$forum_topic_description = $row['forum_topic_description'];
$forum_topic_category_id = $row['forum_topic_category_id'];
$forum_category_id = $row['forum_category_id'];
$forum_category_title = $row['forum_category_title'];
$new_category = $forum_category_title;
if($new_category != $pre_category) {
echo "<thead>";
echo "<tr>";
echo "<th><span class='glyphicon glyphicon-file' aria-hidden='true'></span> {$forum_category_title}</th>";
echo "<th>Topics</th>";
echo "<th>Posts</th>";
echo "<th>Last Post</th>";
echo "</tr>";
echo "</thead>";
}
if($forum_topic_title != null or $forum_topic_description != null) {
echo "<tbody>";
echo "<tr>";
echo "<td><a href='topic.php?t_id=$forum_topic_id'><h4>{$forum_topic_title}</h4></a><div class='row-fluid'><h6>{$forum_topic_description}</h6></div></td>";
echo "<td>1</td>";
echo "<td>1</td>";
echo "<td>03-28-2017</td>";
echo "</tr>";
echo "</tbody>";
} else {
echo "<tbody>";
echo "<tr>";
echo "<td>No topics have been created yet.</td>";
echo "</tr>";
echo "</tbody>";
}
$pre_category = $forum_category_title;
}
}

Picture references from MySql

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.

How can I pass caption text under thumbnails to another web page using PHP and MYSQL

I am creating a video gallery in PHP and MYSQL. The gallery have video thumbnail and text captions under them. When the user click on the image thumbnail or the text caption, it will take them to another web page that will display the video and the caption under it. I want the caption that the user clicked on to be the same caption that is displayed under the video when they are forwarded to the video web page. How do I pass the caption that is under my thumbnail when its clicked, so it can be send to another webpage? I want the caption that is clicked on to displayed under the video.
This is the code for gallery.php :
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<?php
$con=mysqli_connect("localhost","root","","image_display");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result=mysqli_query($con,"select * from table1");
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$image_link = $row['imagelink'];
$image_path = $row['image1'];
$caption = $row['caption'];
echo "<div id='gallery'>";
echo "<a href=".$image_link.val=$caption" value="fixed text"><img src=".$image_path." width='150' height='150' alt='' /> <br>";
echo $caption."</a>"; echo "</div>";
}
?>
</body>
</html>
This is the code for display_video.php:
<!DOCTYPE html>
<html>
<head>
<title>PHP!</title>
</head>
<body>
<?php
$caption=$_GET['caption'];
$embedvideo; //This will hold embed video code
?>
<div id="watchvideo" align="center">
<?php
echo "<h1>$caption=</h1>";
echo $embedvideo; ?>
<p><a href="gallery.php">Back to gallery</p>
</div>
</body>
</html>
Try this, hopefully help you:
gallery.php
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<?php
$con = mysqli_connect("localhost","root","","image_display");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result=mysqli_query($con,"select * from table1");
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$image_link = $row['imagelink'];
$image_path = $row['image1'];
$caption = $row['caption'];
echo "<div id='gallery'>";
echo "<a href=".$image_link."?caption=".urlencode ($caption)." value='fixed text'><img src=".$image_path." width='150' height='150' alt='' /> <br>";
echo $caption."</a>"; echo "</div>";
}
?>
</body>
</html>
display_video.php
<!DOCTYPE html>
<html>
<head>
<title>PHP!</title>
</head>
<body>
<?php
$caption=$_GET['caption'];
$embedvideo = "This will hold embed video code";
?>
<div id="watchvideo" align="center">
<?php
echo "<h1>".$caption."</h1>";
echo $embedvideo; ?>
<p><a href="gallery.php">Back to gallery</p>
</div>
</body>
</html>

arranging images in xampp database

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.

Categories