Database query - how to put the result in the HTML Code? - php

I have a database where the path of pictures are stored. Now I want to get the path of the picture and the description but I dont know how to surround the data with HTML-Code. A "(Html-Code) " indicates Html-Code but in my example it is a bit more complicated. Maybe it is wrong the way I want to do it but I dont know another way.
Here the Php-Code
<?php include ("data_con.php");
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Dateipfad FROM last_upload WHERE ID = 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc(); {
}
} else {
echo "0 results";
}
$conn->close();
echo "Connected successfully";?>
Here the Html-Code
<div class="col-xs-5 col-sm-5 col-md-5 col-lg-5 no-gutter">
<div class="rahmen"> <img src="Pic/OP.jpg" class="img-fluid" alt="Responsive image">
<p>Episode 1</p></div>
First of all I just want to have the path of the file (Dateipfad) ecchoed in
<img src="Pic/Op.jp">
therefore I need to make it like that
<img="echo .row[Dateipfad]."
But all the " to indicate the html code is really hard.
The code have to be in
$row = $result->fetch_assoc(); {
}
but with all the HTML-Code it is really hard and doesnt work for me. I think i do it completly wrong. Can anyone help me?

Method 1
<?php $conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Dateipfad FROM last_upload WHERE ID = 1";
if ( $result = $conn->query($sql) ) {
while ($row = mysqli_fetch_row($result)){
echo '<img src ="$row['value']">'$row['value']'</img>';
}
} else {
echo "0 results";
}
$conn->close();
echo "Connected successfully";
Method Two
<?php $conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Dateipfad FROM last_upload WHERE ID = 1";
if ( $result = $conn->query($sql) ) {
while ($row = mysqli_fetch_row($result)){ ?>
// write html here
<ul>
<li>Just a test</li>
<li>Another test</li>
</ul>
<img src ="<?php echo $row['link']; ?>"><?php echo $row['value']; ?></img>
<?php }
} else {
echo "0 results";
}
$conn->close();
echo "Connected successfully";

Actually what you are looking for is something akin to
<img src="<?= $row[Dateipfad] ?>" />
Also, are you sure that you don't want to iterate on all the results? Will your query always return a single result or none at all?

<img src="<?php echo row[Dateipfad]; ?>" class="img-fluid" alt="Responsive image">
And don't use short handed echo statement <?= ?> for my sake.

update
if($result->num_rows > 0)
{
while ($row = mysqli_fetch_row($result)){?>
<div class="col-xs-5 col-sm-5 col-md-5 col-lg-5 no-gutter">
<div class="rahmen"> <img src="<?php echo $row['Dateipfad']; ?>" class="img-fluid" alt="Responsive image">
<p>Episode 1</p>
</div>
</div>
<?php}
}

Related

Echo different and one row in each image taking the last 2 rows in the database

My code display all values in the database in an single image, i want each image to display only one value in the database starting from the last 2 values.Any help? below is my code
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="grey.jpg">
<div class="top-left"> <p><?php $servername = "localhost";
$username = "root";
$password = "";
$dbname = "software";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT ID, description FROM test";
$result = $conn->query($sql);
//if ($result->num_rows == 1) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo " ". $row["description"]. "<br>";
}
//} else {
// echo "0 results";
//} ?> </p></div>
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="imagesb.jpg">
</div>
You can get the last two rows from the DB using something like
SELECT ID, description FROM test ORDER BY ID DESC LIMIT 2
(or whichever column you want to order by).
As for how you want it output it's not clear what you're asking but it sounds like you want to move the while loop outside of the div tag. Something like...
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "software";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT ID, description FROM test ORDER BY ID DESC LIMIT 2"; // Last 2 rows
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) : ?>
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="grey.jpg">
<div class="top-left">
<p><?php echo htmlspecialchars($row["description"], ENT_QUOTES); ?></p>
</div>
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="imagesb.jpg">
</div>
<?php endwhile;
Remember whenever you output any variables/DB content you need to escape it (htmlspecialchars in this case) otherwise your page could be hacked. Here is a random link I found about it that covers the basics https://www.inanimatt.com/php-output-escaping.html

How to create jquery tabs using php?

I used jquery-ui tabs using php but it's not working, I am unable to identify the problem, please help me.
<div id="tabs">
<div class="row">
<div class="col-md-4">
<?php
$con = mysqli_connect("localhost", "admin", "123456", "gazette");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM gazette_details");
while ($row = mysqli_fetch_array($result)) {
echo "<ul>";
$id = '#tabs-' . $row['id'];
echo "<li>" . "<a href='$id'>" . $row['gazette_id'] . "</a>" . "</li>";
echo "</ul>";
}
mysqli_close($con);
?>
</div>
<div class="col-md-4">
<div id="tabs-12">1</div>
<div id="tabs-13">2</div>
<div id="tabs-14">3</div>
</div>
</div>
<div class="col-md-4">.col-md-4</div>
</div>
I used this website example: http://jqueryui.com/tabs/
you may try
<?php
$ARR_RESULT = array();
$con = mysqli_connect("localhost", "admin", "123456", "gazette");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM gazette_details");
while ($row = mysqli_fetch_array($result)) {
$ARR_RESULT[] = $row;
}
mysqli_close($con);
?>
<div id="tabs">
<ul>
<?php
foreach($ARR_RESULT as $arr)
{
echo '<li>'.$arr['gazette_id'].'</li>';
}
?>
</ul>
<?php
foreach($ARR_RESULT as $arr)
{
echo '<div id="tabs-'.$arr['id'].'">';
echo '<p>Tab-'$arr['id'].'</p>';
echo '</div>';
}
?>
</div>

$_SESSION changing randomly?

Okay, so I have a members database script where it displays all the members and as soon as you go to the page, it suddently changes your session['id'] to a random one. Not sure why its doing this but here is the code and I have tested it to find that it is only when this script is loaded that it does it.
Am I just stupid and can't spot the bug?
<?php
$link = mysqli_connect("localhost", "lunar_lunar", "", "lunar_users");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$result = mysqli_query($link, "SELECT * FROM users ORDER BY username");
while($row = mysqli_fetch_assoc($result)) {
$id=$row['id'];
$username=$row['username'];
$email=$row['email'];
$firstname=$row['firstname'];
$lastname=$row['lastname'];
$motto=$row['motto'];
$bio=$row['bio'];
$result4 = mysqli_query($link, "SELECT * FROM photo where id='$id'");
$row4 = mysqli_fetch_assoc($result4);
$image=$row4['filename'];
$src = (empty($image)) ? "upload/your-photo.jpg" : "site_images/$id/$image";
$motto = (empty($motto)) ? "No motto" : $motto;
$bio = (empty($bio)) ? "No biography" : $bio;
echo "<div class='panel panel-default'>
<div class='panel-heading'>
<h3 class='panel-title'><a href='public.php?id=".$id."'>".$username."</h3></a>
</div>
<div class='panel-body'>
<div class='gravatar span3' style='padding:0px;margin:0px;'>
<img src='
".$src."' alt='' width='85' height='85'>
</div>
<br />
<div class='page-header'>
<br />
</div>
<p style='margin-right:450px;'>
".$bio."
</p>
</div>
<div class='panel-footer'>".$motto."</div>
</div>";
}
?>
Top of document:
<?php
session_start();
if(!isset($_SESSION['id'])) {
header("Location: index.php");
} else {
}
?>
It basically checks if you are signed in, I use this same script to check on every page so I know that works.
Sorry, this was my mistake. I was overwriting $id, when I changed $id to $memid it fixed.

Show all fields based on ID

I am building a blog and trying to show all comments that apply to the post.
Each post has an ID and each comment is stored with the post ID.
here is my code:
<?php
$con = mysql_connect("localhost","cl49-XXX-b","X");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cl49-XXX-b", $con)or die( "Unable to select database line 873");
$result=mysql_query("SELECT * FROM blogcomments WHERE ID='".$ID."'") or die('Error on Line 215 :'.mysql_error());
echo " <ul class='comments'> "; // first row beginning
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$name = $row['name'];
$email = $row['email'];
$comment = $row['comment'];
$created=$row['created'];
echo" <li>
<div class='comment'>
<div class='thumbnail'>
<img class='avatar' alt='' src='img/avatar.jpg'>
</div>
<div class='comment-block'>
<div class='comment-arrow'></div>
<span class='comment-by'>
<strong>$name</strong>
<span class='pull-right'>
<span> <a href='#'><i class='icon-reply'></i> Reply</a></span>
</span>
</span>
<p>$comment</p>
<span class='date pull-right'>$created</span>
</div>
</div> ";
echo " </li>"; // it's time no move to next row
}
?>
When I run this code the page only shows one comment, although my DB has 3 comments with the correct ID.
I would consider using mysqli_ as mysql_ has been depreciated. I'd also consider using a while loop, hopefully this will help:
<?php
$DBServer = 'localhost';
$DBUser = 'xxxx';
$DBPass = 'xxxx';
$DBName = 'xxxx';
$mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
if ($mysqli->connect_errno) {
echo "Failed to connect to the database: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$query = "SELECT * FROM blogcomments WHERE ID='". $ID ."'";
echo " <ul class='comments'> ";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
?>
<li>
<div class='comment'>
<div class='thumbnail'>
<img class='avatar' alt='' src='img/avatar.jpg'>
</div>
<div class='comment-block'>
<div class='comment-arrow'></div>
<span class='comment-by'>
<strong><?php echo $row['name']; ?></strong>
<span class='pull-right'>
<span><a href='#'><i class='icon-reply'></i> Reply</a></span>
</span>
</span>
<p><?php echo $row['comment']; ?></p>
<span class='date pull-right'><?php echo $row['created']; ?></span>
</div>
</div>
</div>
</li>
<?php
} $result->close();
} $mysqli ->close();
echo "</ul>";
?>
I haven't tested this for bugs, but let me know if you like further information.
When you do this :
mysql_query("SELECT * FROM blogcomments WHERE ID='".$ID."'")
You select just one comment in the database.
Maybe you have to do that :
mysql_query("SELECT * FROM blogcomments WHERE post_ID='".$post_ID."'")
Because you are selecting the row where ID field value is $ID in the table blogcomments.
I guess you store the referred post_id to whom the comment is connected in a field called something like "post_id". You better point your select query to that field ;)
If you put the structure of the table in the question, I might help :)
You're not closing your <ul>. Might it just be a HTML formatting issue?

when no rows retrieved from SQL database then dont display a div

i tried the empty selector in jquery but it doesnt work. the content is still being displayed. i am retrieving some rows from the SQL database.. if there is no database then i dont want to display that div.
<div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?>
</p>
</marquee>
</div>
</div>
</div>
Is that your whole code? Are you working with ajax? If you're using pure PHP without ajax just set the output div into your "if"-conditions?
If you want to use jQuery, try:
if ( ($("div.scrollableArea p").text()).length > 0 )
{
$("div.scrollableArea p").show();
}
else
{
$("div.scrollableArea p").hide();
}
But you can do it without jQuery, I guess.
You need to put the HTML inside your php if clause.
Simple example:
<div id="scrollingText">
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysqli_connect_errno($con)) {
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?>
</p>
</marquee>
</div>
</div>
<?php } else { ?>
<div class="errordiv">Display this on error</div>
<?php } ?>
</div>
thank you guys with ur help i made some change. the below code did it:
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysql_num_rows($result) > 0)
{
?><div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?> </p>
</marquee>
</div>
</div>
</div>
<?php } ?>

Categories