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?
Related
i'm trying to display quotes but i can't seem to get the data to show on the index page i'm still new to php and mysql and its been a ruff road
html index page
<div class="quote">
<div class="container">
<blockquote class="blockquote">
<p class="mb-0">"<?php echo $row['feedback']; ?>"</p>
<footer class="blockquote-footer"><?php echo $row['companyname']; ?></footer>
</blockquote>
<blockquote class="blockquote-reverse">
<p class="mb-0">"<?php echo $row['feedback']; ?>"</p>
<footer class="blockquote-footer"><?php echo $row['companyname']; ?></footer>
</blockquote>
<blockquote class="blockquote">
<p class="mb-0">"<?php echo $row['feedback']; ?>"</p>
<footer class="blockquote-footer"><?php echo $row['companyname']; ?></footer>
</blockquote>
</div>
</div>
and database connect
<?php
$hostname = "";
$username = "";
$password = "";
$db = "";
$dbconnect=mysqli_connect($hostname,$username,$password,$db);
if ($dbconnect->connect_error) {
die("Database connection failed: " . $dbconnect->connect_error);
}
$query="SELECT companyname,feedback,status from review WHERE status=? ORDER BY RAND() LIMIT 3";
?>
<?php
$hostname = "";
$username = "";
$password = "";
$db = "";
$dbconnect=mysqli_connect($hostname,$username,$password,$db);
if ($dbconnect->connect_error) {
die("Database connection failed: " . $dbconnect->connect_error);
}
$query=mysqli_query($dbconnect,"SELECT companyname,feedback,status from review WHERE status=? ORDER BY RAND() LIMIT 3");
$row = mysqli_fetch_assoc($query);
?>
This question already has answers here:
How can I get useful error messages in PHP?
(41 answers)
Closed 6 years ago.
I'm creating some code to display all the records from a MySQL database into a PHP view.
The query is perfect: using HeidiSQL it retrieves all the values needed:
At the moment, is giving the following error:
How can I debug this?
The code:
<?php $sql = "select * from specials group by Start_Date DESC";
$result = #mysqli_query($sql)
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<div class='deal-item col-md-12'>
<div class='col-md-4'>
<img src='lascruces_styles/img/deals-img/oil-deal.jpg' alt='' class='deal-thumb'>
<p class='expire'>The deal expires '.$row['End_Date'].'</p>
</div>
<div class='col-md-6 info-container'>
<h2 class='deal-title'>'.$row['Special_Name'].'</h2>
<p class='offer-user'>Offered by
<a href=''>'.$row['Added_By'].'</a></p>
<p class='deal-desc'>'.$row['Description'].'</p>
<div class='share-row'>
<a href='' class='share'>Share this deal</a>
<div class='social'>
<i class='icon-facebook'></i>
<i class='icon-gplus'></i>
<i class='icon-linkedin'></i>
<i class='icon-mail-squared'></i>
</div>
</div>
</div>
<div class='col-md-2 view-deal-container'>
<p class='old-price'>'.$row['Normal_Price'].'</p>
<p class='current-price'>'.$row['Special_Price'].'</p>
<a href=''><div class='view-deal'>
<p>VIEW DEAL</p>
</div>
</a>
</div>
</div>';
mysqli_free_result ($result); // Free up the resources }
}
?>
You're missing a couple of things.The main things are that you're missing the connection parameter in your mysqli_query() call, and your single quotes are messing all your syntax. Try in this manner:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "select * from specials group by Start_Date DESC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo '<div class="deal-item col-md-12">
<div class="col-md-4">
<img src="lascruces_styles/img/deals-img/oil-deal.jpg" alt="" class="deal-thumb">
<p class="expire">The deal expires ' . $row['End_Date'] . '</p>
</div>
</div>';
}
} else {
echo "0 results";
}
mysqli_close($conn);
You're not quoting your string properly, which is causing syntax errors. You're also mixing mysql_* with mysqli_* here.
I'm assuming the MySQLi connection you're using is called $mysqli here, change it accordingly (or if you're using another API, you need to adapt to use that, APIs don't mix).
Properly quoted it should look something like this.
<?php
$sql = "select * from specials group by Start_Date DESC";
if ($result = mysqli_query($mysqli, $sql)) {
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<div class='deal-item col-md-12'>
<div class='col-md-4'>
<img src='lascruces_styles/img/deals-img/oil-deal.jpg' alt='' class='deal-thumb'>
<p class='expire'>The deal expires ".$row['End_Date']."</p>
</div>
<div class='col-md-6 info-container'>
<h2 class='deal-title'>".$row['Special_Name']."</h2>
<p class='offer-user'>Offered by
<a href=''>".$row['Added_By']."</a></p>
<p class='deal-desc'>".$row['Description']."</p>
<div class='share-row'>
<a href='' class='share'>Share this deal</a>
<div class='social'>
<i class='icon-facebook'></i>
<i class='icon-gplus'></i>
<i class='icon-linkedin'></i>
<i class='icon-mail-squared'></i>
</div>
</div>
</div>
<div class='col-md-2 view-deal-container'>
<p class='old-price'>".$row['Normal_Price']."</p>
<p class='current-price'>".$row['Special_Price']."</p>
<a href=''><div class='view-deal'>
<p>VIEW DEAL</p>
</div>
</a>
</div>
</div>";
}
mysqli_free_result ($result); // Free up the resources
}
?>
I am trying to repeat a div the same div multiple times with as many rows as i have in the database table. a non working example i made of what i want is below.
<?php for ($i=0; $i < 3; $i++) {
?><!-- switch the three with the number of rows in the table-->
I would want to loop the container and switch the database query with the value of i.
<div id = "bodyContainer">
<form class="review">
<div class="reviewContainer">
<div class = "images">
<?php
$link = mysqli_connect("localhost", "root", "root","DJP");
if (mysqli_connect_error()) {
die("Could not connect to database");
}
$query = "SELECT * FROM reviews WHERE id = '<?php echo $i ?><!--switch the value of i here-->'";
$result=mysqli_query($link, $query);
$row = mysqli_fetch_array($result);
echo '<img height = 90% width = 100% src="data:image/jpeg;base64,'.$row[3].' " />';
?>
</div>
<!--<div class="title">
<?php
//echo $row[1];
?>
</div>-->
<div class = "comment">
<?php
echo $row[2];
?>
</div>
<div class = "rating" style = "float: right;">
<?php
echo $row[4];
?>
<br/>
<br/>
stars
</div>
</div><!--end review-->
</form>
</div><!--end body container-->
<?php;}?><!-- end the for loop so all the divs can be re-done with a new entry for the query. I would move the $link out of loop also.-->
Just fetch all the rows once and then go through them as you print the data:
$link = mysqli_connect("localhost", "root", "root","DJP");
if (mysqli_connect_error()) {
die("Could not connect to database");
}
$query = "SELECT * FROM reviews";
$result=mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)){ ?>
<div id = "bodyContainer">
<form class="review">
<div class="reviewContainer">
<div class = "images">
<?php
echo '<img height = 90% width = 100% src="data:image/jpeg;base64,'.$row[3].' " />';
?>
</div>
<!--<div class="title">
<?php
//echo $row[1];
?>
</div>-->
<div class = "comment">
<?php
echo $row[2];
?>
</div>
<div class = "rating" style = "float: right;">
<?php
echo $row[4];
?>
<br/>
<br/>
stars
</div>
</div><!--end review-->
</form>
</div><!--end body container-->
<?php } ?>
So I've written out simple code that SHOULD display every comment in the comment table (there is only 1 comment at the moment). The issue is - it doesn't.
What the code SHOULD do: display every comment in the comment table.
What it DOESN'T do: doesn't display ANY comments.
The code below doesn't work:
<!-- Posted Comments -->
<?php
$result = mysqli_query("SELECT * FROM comment WHERE adventureID = 2");
while($row = mysqli_fetch_assoc($result)) { ?>
<div class="media">
<a class="pull-left" href="#">
<img class="media-object" src="http://placehold.it/64x64" alt="">
</a>
<div class="media-body">
<h4 class="media-heading"><?php echo $row['userID']; ?>
<small><?php echo $row['dateTime']; ?></small>
</h4>
<?php echo $row['commentText']; ?>
</div>
</div>
<?php } ?>
You forgot to include the connection variable in mysqli_query:
$result = mysqli_query($con, "SELECT * FROM comment WHERE adventureID = 2");
Where $con is the connection variable. Refer this Link
If this is the only piece of code you have I was wondering if you have made a connection with the database in which the comments are stored in.
Making a connection could look a bit like this:
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$db= 'test';
// Create connection
$connect = new mysqli($servername, $username, $password, $db);
// Check connection
if ($connect->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
I am trying to get the code below to loop using a while statement. The problem I am having is getting the divs to worth with in the PHP. Any ideas on how I can make this happen? My database consists of a columns called month, day, description, and location.
<div class="events-module">
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sandbox", $con);
$result = mysql_query("SELECT * FROM events"); ?>
<?php while($row = mysql_fetch_array($result)) ?>
<div class="date">
<div id="month"><h4><?php echo $row['month']; ?></h4></div>
<div id="day"><h3><?php echo $row['day']; ?></h3></div>
</div>
<div class="event">
<div id="description"><p><?php echo $row['description']; ?></p></div>
<div id="location"><p><b><?php echo $row['location']; ?></b></p></div>
</div>
<?php endwhile;
mysql_close($con);
?>
</div>
You didn't say exactly what's wrong. But, it seems you are missing your start block for your while loop
while (.... ) :
^^^
... // div stuff
end while