My drop down div isn't visible - php

I am working on a online shopping website.
This is a div that will display the item and the image should be also used from database.
Help me!
This is the code by which I am trying to fetch the data from mysql database created on phpmyadmin and construct this div.
The problem is that I can't see any output
<?php
include "connect_database.php";
$sql="SELECT * FROM product";
$result = mysqli_query($connection, $sql);
$rows=mysqli_fetch_assoc($result);
while ($rows=mysqli_fetch_assoc($result)) {
echo "<div class='item'>
<img class='image' src='".$rows["image"]."'><br/>
<span>".$rows["product_name"]."</span><hr/>
<span>".$rows["model_no"]."</span><hr/>
<span>Rs ".$rows["amount"]."</span>
</div>
";
}
?>

You call mysqli_fetch_assoc first before the while loop. That call reads the first row, but the result is not used. After that, the while loop kicks in and processes and outputs the rest of the rows. If you have only one row, you won't see anything.
So you can try this, with that line removed.
Note that I renamed $rows to $row. This isn't necessary, but since the variable will contain data of only one row, I think this name is better. It will help you understand the code better if you need to change it later.
<?php
include "connect_database.php";
$sql="SELECT * FROM product";
$result = mysqli_query($connection, $sql);
while ($row=mysqli_fetch_assoc($result)) {
echo "<div class='item'>
<img class='image' src='".$row["image"]."'><br/>
<span>".$row["product_name"]."</span><hr/>
<span>".$row["model_no"]."</span><hr/>
<span>Rs ".$row["amount"]."</span>
</div>
";
}
?>

echo '<div class="item">
<img class="image" src="'.$rows['image'].'"><br/>
<span>"'.$rows["product_name"].'"</span><hr/> <span>"'.$rows['model_no'].'"</span><hr/>
<span>Rs "'.$rows['amount'].'"</span>
</div>
';

<?php
include "connect_database.php";
$sql="SELECT * FROM product";
$result = mysqli_query($connection, $sql);
while ($rows=mysqli_fetch_array($result,MYSQLI_BOTH)) {
echo "<div class='item'>
<img class='image' src="<?php echo $rows['image']; ?>"><br/>
<span><?php echo $rows['product_name']; ?></span><hr/>
<span><?php echo $rows['model_no']; ?></span><hr/>
<span>Rs<?php echo $rows['amount']; ?></span>
</div>
";
}
?>

Related

How to fix PHP/SQL search query only fetching one row of data instead of all associated rows?

I'm working on a little project and I want to be able to search my database for all items associated with my search
My problem is that when I use my code below, I get only one result rather than an array of all results that fit my query. I have seen some solutions that suggested using fetchAll() but that doesn't seem to output anything.
What the PHP query looks like.
if(isset($_POST['search'])){
if(preg_match("/^[a-zA-Z]+/", $_POST['sname'])){
$search = $_POST['sname']; //name in form
$pdo = & dbconnect();
$userid = $_SESSION['user_id'];
$sql = "SELECT * FROM movie_dets Where user_id=? and Title LIKE '%" .$search."%'";
$stmt=$pdo->prepare($sql);
$stmt->execute([$userid]);
if(!$stmt)
{
die("Database pull did not return data");
}
$row=$stmt->fetch();
}
}
Then I have a foreach loop in my html that looks like so
<div class="row">
<?php
$loop = 0;
foreach ($stmt as $row): //loop through result set ?>
<div class="column" >
<div>
<figure>
<img src="<?php echo "/.../".$row['Cover'] ?>" alt="<?php echo $row['Title']; ?> cover" />
<figcaption>
...
</figcaption>
</figure>
</div>
</div>
<?php
$loop++;
if ($loop % 4 ==0) //to display four results the wrap.
{
echo "</div> <div class='row'>";
}
?>
<?php endforeach ?>
</div>
In my database, there are two titles called frozen. This is supposed to output all rows when given the 'fr' search word. Instead, it only fetches one of them.

Returning SQL data within an image tag using PHP

When I run the following file I get the database data i.e it prints it out on the website so I know my connections are good.
<html>
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo $row["name"], $row["image"];
}
?>
</div>
</html>
However when I try and format the results like below
<html>
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo <div id = "bookbar">
<img src= "$row['image']" alt = "image">
<p> $row['name'] </p>
</div>
}
?>
</div>
</html>
it doesn't work. Can anyone help me fix the code?
Maybe try this your code didn't close/open the php tags properly also don't echo like that
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<div id = "bookbar">
<img src= "<?php echo $row['image'] ?>" alt = "image">
<p><?php echo $row['name']; ?></p>
</div>
<?php
}
}
$conn->close();
?>
If you want to echo something out
Its better to close on open the php tags like so
PHP goes here
?>
HTML goes here
<?php
PHP goes here
And if you want to echo something inside the HTML just do this
<span> <?php echo "something" ?> </span>
much easier and makes the code easier to read.
change your echo statement to -
echo '<div id = "bookbar"><img src= "' . $row['image'] . '" alt = "image"><p>'. $row['name'] .'</p>'
Your issue is a syntax problem - you can't use echo like that, it has to echo a string variable. You should be seeing an error message about it.
You could keep the echo statement and put all the HTML inside a string, and concatenate (or interpolate) the PHP data into it. But IMO the easiest thing here in terms of readability and maintenance is to step out of the PHP tags, print the HTML, embed some PHP tags in it for the variables, and then step back in again to continue with the code. It makes the HTML far easier to understand:
?>
<div id="bookbar">
<img src="<?php echo $row['image'] ?>" alt="image">
<p><?php echo $row['name'] ?></p>
</div>
<?php
When you are in php mode you should echo strings as php variables wrapped with single quotes:
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo '<div id = "bookbar">';
echo '<img src="' . $row['image'] . '" alt = "image">';
echo '<p>' . $row['name'] . '</p>';
echo '</div>';
}

error in query using MySQL and PHP, based on showing data from database

code:
<!-- content -->
<article id="content" class="tabs">
<div class="wrapper">
<div class="box2">
<?php
//session_start();
$sql = "SELECT * FROM `db`.`news` ORDER BY `date` DESC LIMIT 20";
include 'php/dbconnection.php';
$conn = OpenCon(); //$conn=$_SESSION['conn'];
$result = mysqli_query($conn,$sql);
if($result){
$rows = mysqli_fetch_array($result);
echo "<div class='wrapper tab-content'>";
echo "<section class='col1'>";
echo "<h4><span>" . $rows['date']. "</span> </h4>";
echo "<p class='pad_bot2'><strong>". $rows['title']. "
</strong></p>";
echo "<p class='pad_bot1'>".$rows['des'] ."</p>";
echo "</section>";
echo "</div>";
}
?>
</div>
</div>
</article>
<!-- /content-->
Here I'm trying to, print 'news' table from database, but I'm unable to print anything on webpage.
can anyone point out, what is wrong in this ?
Thank You in advance for any help!
You have to put mysqli_fetch_row($result) inside a loop.
Example with your case:
if($result){
while ($rows=mysqli_fetch_row($result)){
echo "<div class='wrapper tab-content'>";
echo "<section class='col1'>";
echo "<h4><span>" . $rows['date']. "</span> </h4>";
echo "<p class='pad_bot2'><strong>". $rows['title']. "
</strong></p>";
echo "<p class='pad_bot1'>".$rows['des'] ."</p>";
echo "</section>";
echo "</div>";
}
}
// Free result set
mysqli_free_result($result);
Hope this helps!
For commented $conn=$_SESSION['conn'];
You cannot store resource handles within a $_SESSION. See PHP.net:
Some types of data can not be serialized thus stored in sessions. It
includes resource variables or objects with circular references (i.e.
objects which passes a reference to itself to another object).
As your object contains the connection link it can't be serialized, Just reconnect with every request.
And you can make clean template like below
<!-- content -->
<article id="content" class="tabs">
<div class="wrapper">
<div class="box2">
<?php
//session_start();
$sql = "SELECT * FROM `db`.`news` ORDER BY `date` DESC LIMIT 20";
include 'php/dbconnection.php';
$conn = OpenCon();
$result = mysqli_query($conn,$sql);
if($result):
while ($row=mysqli_fetch_row($result)):
?>
<div class='wrapper tab-content'>
<section class='col1'>
<h4><span><?php echo $row['date'];?></span></h4>
<p class='pad_bot2'>
<strong><?php echo $row['title'];?></strong>
</p>
<p class='pad_bot1'><?php echo $row['des'];?></p>
</section>
</div>
<?php
endwhile;
else:
?>
<p>No active news</p>
<?php
endif;
?>
</div>
</div>
</article>
<!-- /content-->
// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);
// Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
So for your code, you need to change:
$rows = mysqli_fetch_array($result);
to:
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
Also, you should check for number of rows before fetching by using:
$rowcount=mysqli_num_rows($result);
Only if $rowcount is more than 0, you should mysqli_fetch_array.

MySQL query doesn't show all the records correctly

The query am running against my database to get the 3 records order it by Random. The problem is that sometimes it shows all 3 records sometimes it only shows 2, 1 and other times its just blank. In the database I have around 28 records.
What I have tried
I have tried without LIMIT - Problem Same
I have echoed out $suggested_profile_id found all 3 records coming out.
This is the query that gets the records LIMIT it by 3
<?php
$sql = "SELECT * FROM members WHERE member_status='activated' ORDER BY RAND() DESC LIMIT 3";
$query = $db->SELECT($sql);
if($db->NUM_ROWS() > 0){
$rows = $db->FETCH_OBJECT();
?>
This is the code that runs and gets all 3 records in a loop.
<!-- Suggested Friends -->
<div class="col-md-0 media-body">
<?php
foreach($rows as $row){
$member_id = $row->member_id;
$sql = "SELECT * FROM profile WHERE profile_id='$member_id' LIMIT 1";
$query = $db->SELECT($sql);
$rows = $db->FETCH_OBJECT();
foreach($rows as $row){
$suggested_profile_id = $row->profile_id;
$suggested_profile_photo = $row->profile_photo;
$suggested_profile_username = $row->profile_username;
$suggested_profile_name = $row->profile_name;
if(
$suggested_profile_id != GET_SESSION_ID_VALUE(ENCRYPTION_KEY)&&
!is_in_ARRAY($make_string_to_ARRAY, $suggested_profile_id)
){
?>
<div class="row margin0">
<div class="col-md-4 pad0">
<a href="/<?php echo $suggested_profile_username; ?>" title="<?php echo $suggested_friends_profile_name; ?>" >
<?php
global $suggested_friends_profile_id;
$member_dir = dirname(dirname(dirname(__FILE__))) . "/members/" . $suggested_profile_id ."/smalll_" . $suggested_profile_photo;
if(file_exists($member_dir)){
?>
<img alt="<?php echo $suggested_profile_name; ?>" title="<?php echo $suggested_profile_name; ?>" src="/members/<?php echo $suggested_profile_id; ?>/smalll_<?php echo $suggested_profile_photo; ?>" width="50" height="50">
<?php
} else {
?>
<img alt="<?php echo $suggested_profile_name; ?>" title="<?php echo $suggested_profile_name; ?>" src="/assets/images/default.jpg" width="50" height="50">
<?php
}
?>
</a>
</div>
<div class="col-md-8 pad0">
<?php echo $suggested_profile_name; ?>
<span class="f12 gray">271 Mutual Friends</span>
Add as friend
</div>
</div>
<?php
}
}
}
?>
</div>
<!-- ** Suggested Friends -->
What am I missing? Is there any alternative way I can achieve this...thanks!
It looks to me like you're overwriting your $rows variable within the inner select.
foreach($rows as $row){ // <-- first $rows / $row
$member_id = $row->member_id;
$sql = "SELECT * FROM profile WHERE profile_id='$member_id' LIMIT 1";
$query = $db->SELECT($sql);
$rows = $db->FETCH_OBJECT(); <-- $rows overwritten
foreach($rows as $row){
Break your display from your application logic and you won't have such a hard time debugging this kind of thing. Besides, you have a lot of duplicated code and that makes things hard to manage as well as being hard to debug.
Further, you wouldn't have this problem if you ran one query: SELECT * FROM members JOIN profile ON members.member_id = profile.profile_id and not only does your code get simpler and your double-foreach loop problem disappear, but your database access will also be a lot more efficient.

Output of the table in div class

$resultSet = mysql_query("SELECT * FROM news ORDER BY id DESC LIMIT 7");
$product_count = mysql_num_rows($resultSet);
if($product_count >0){
while( mysql_fetch_array($resultSet)){
$id= $row["id"];
$cat= $row["categories"];
$title= $row["title"];
$image= $row["image"];
$txt= $row["txt"];
$res= $row["resource"];
}
}
else
{ echo'site is under maintaince ';}
echo " <div class=\"row-fluid\">
<div class=\"span6 post no-margin-left\">
<figure>
<img src=\"$image\" alt=\"Thumbnail 1\" />
<div class=\"cat-name\">
<span class=\"base\">'$cat'</span>
<span class=\"arrow\"></span>
</div>
</figure>
<div class=\"text\">
<h2>'$title'</h2>
<p>$txt</p>
<div class=\"meta\">By ' $res' | '$date' | 15 comments</div>
</div>
</div>
</div>";
?>
The code is not showing the output results of the table. Can you tell me what I am doing wrong? I want to show the output in the div class. My paging is working correctly, but it is not showing the output correctly. Can you guide me? I will really be grateful.
while( mysql_fetch_array($resultSet)){
$id= $row["id"];
$cat= $row["categories"];
$title= $row["title"];
$image= $row["image"];
$txt= $row["txt"];
$res= $row["resource"];
}
}
Where is $row in the lopp? However, to populate all the results, you need the printing inside the loop, otherwise you won't get them iterated

Categories