Converting to php MySQLi Object-Oriented - php

I want to convert my website to MySQLi object-oriented:
Below works. Before I convert my site is there anything I've missed or need to do better or differently?
Any help would be appreciated.
<?php
$query = "SELECT pagename, image, name FROM table";
$result = $db->query($query);
while ($row = $result->fetch_assoc())
{
?>
<figure>
<a href="<?php echo ($row['pagename']); ?>">
<img src="<?php echo ($row['image']); ?>"></a>
<figcaption>
<a href="<?php echo ($row['pagename']); ?>">
<h2><?php echo ($row['name']); ?></h2></a>
</figcaption>
</figure>
<?php
}
$result->free();
$db->close();
?>

Related

How to fetch comments from mysql database without destroying my html tags

I want to give a little style for my comment section, here is the code without any css, but i want to give it some style
<div id="comments">
<?php
$sql = "SELECT * FROM comments ORDER BY id LIMIT 2";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<p>";
echo $row['author'];
echo "<br>";
echo $row['message'];
echo "<br>";
echo $row['time'];
echo "</p>";
}
} else {
echo "there are no comments!";
}
?>
</div>
<button>More comments</button>
and down here is my html section of which i want to appear while handling my php comments where USER, COMMENT and TIME are are stored in my database, here is the html, how can i echo the above variables into the below html tags ?
<div class="media response-info">
<div class="media-left response-text-left">
<a href="#">
<img class="media-object" src="images/c1.jpg" alt="">
</a>
<h5>USER</h5>
</div>
<div class="media-body response-text-right">
<p>COMMENT</p>
<ul>
<li>TIME</li>
<li>Reply</li>
</ul>
</div>
</div>
You can do like this:
<div id="comments">
<?php
$sql = "SELECT * FROM comments ORDER BY id LIMIT 2";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) { ?>
<div class="media response-info">
<div class="media-left response-text-left">
<a href="#">
<img class="media-object" src="images/c1.jpg" alt="">
</a>
<h5><?php echo $row['author']; ?></h5>
</div>
<div class="media-body response-text-right">
<p><?php echo $row['message']; ?></p>
<ul>
<li><?php echo $row['time']; ?> </li>
<li>Reply</li>
</ul>
</div>
</div>
<?php }
} else {
echo "there are no comments!";
}
?>
</div>
hope it will help you.

display images from db

i have a table
tbl_image
--------------
imgID(int)
imgName(varchar)
image(blob)
here is code to display image :
<?php
$query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)){
$imgName = $row['imgName'];
echo '<div class="col-sm-3 gallery-grids-left">
<div class="gallery-grid">
<a class="example-image-link" href="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"
data-lightbox="example-set"
data-title='.$imgName.'>
<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"/></a></div></div>';
}
?>
but I'm really not like to use echo ''; so I changed to
<?php
$query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)){
$imgName = $row['imgName'];
?>
<div class="col-sm-3 gallery-grids-left">
<div class="gallery-grid">
<a class="example-image-link" data-lightbox="example-set"
href="<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"; '?>"
data-title="<?php echo imgName;?>">
<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"; '?>
</a>
</div>
</div>
<?php }
?>
It just display only image , no title no "image block" like this image demo
Plz help me to show my mistake?and how to fix it.
Many thanks,
HTML error in your code: wrong binding of PHP scripting
<?php
$query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)){
$imgName = $row['imgName'];
?>
<div class="col-sm-3 gallery-grids-left">
<div class="gallery-grid">
<a class="example-image-link" data-lightbox="example-set"
href="data:image/jpeg;base64,<?php echo base64_encode($row['image'] )?>"
data-title="<?php echo $imgName;?>">
<img src="data:image/jpeg;base64,<?php echo base64_encode($row['image']) ?> />
</a>
</div>
</div>
<?php }
?>
You have missed $ for variable imgName
Try below code:
<?php
$query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)){
$imgName = $row['imgName'];
?>
<div class="col-sm-3 gallery-grids-left">
<div class="gallery-grid">
<a class="example-image-link" data-lightbox="example-set"
href="<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"; '?>"
data-title="<?php echo $imgName;?>">
<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"; '?>
</a>
</div>
</div>
<?php }
?>
First of all, as #Suyog pointed out, you missed $ on imgName variable in data-title. Second, if you are simplifying the code to use html as much as possible - do that and remove the extra stuff. Have something like this:
<?php
$query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)){
$imgName = $row['imgName'];
?>
<div class="col-sm-3 gallery-grids-left">
<div class="gallery-grid">
<a class="example-image-link" data-lightbox="example-set"
href='<img src="data:image/jpeg;base64,<?php echo base64_encode($row['image'] );?>"/>'
data-title="<?php echo $imgName;?>">
<img src="data:image/jpeg;base64,<?php echo base64_encode($row['image'] ); ?>">
</a>
</div>
</div>
<?php } ?>
Other than that, I would strongly advise against storing binary data in the database. This is not what the database is for. Store images are files and store filenames in the database. It'll make your life easier.

php - Displaying more than one image from a database

I am trying to display images (and other data connected to them, e.g. titles) in a table. The filenames of the images are stored in my database as imgname, but the problem is that there is only one image that is being displayed.
Here is the code I use. What should I change?
mysql_select_db($database_connection, $connection);
$query_img = "SELECT imgname FROM img ORDER BY imgname";
$img = mysql_query($query_img, $connection) or die(mysql_error());
$row_img = mysql_fetch_assoc($img);
$totalRows_img = mysql_num_rows($img);
Table
<ul>
<li>
<img src="images/<?php echo $row_img['imgname']; ?>">
<h3><?php echo $row_title['title']; ?></h3>
<p><?php echo $row_description['des']; ?></p>
</li>
<li>
<img src="images/<?php echo $row_img['imgname']; ?>">
<h3><?php echo $row_title['title']; ?></h3>
<p><?php echo $row_description['des']; ?></p>
</li>
<li>
<img src="images/<?php echo $row_img['imgname']; ?>">
<h3><?php echo $row_title['title']; ?></h3>
<p><?php echo $row_description['des']; ?></p>
</li>
</ul>
Since I use a simmilar code for the titles and descriptions, I have the same problem with them.
Just use a loop to iterate through the database resultset:
$img = mysql_query($query_img, $connection) or die(mysql_error());
while($row_img = mysql_fetch_assoc($img)) {
?>
<li>
<img src="images/<?php echo $row_img['imgname']; ?>">
<h3><?php echo $row_title['title']; ?></h3>
<p><?php echo $row_description['des']; ?></p>
</li>
<?php
}
mysql_select_db($database_connection, $connection);
$query_img = "SELECT imgname FROM img ORDER BY imgname";
$img = mysql_query($query_img, $connection) or die(mysql_error());
<ul>
<?php
while($row_img = mysql_fetch_assoc($img)) {
?>
<li>
<img src="images/<?php echo $row_img['imgname']; ?>">
<h3><?php echo $row_title['title']; ?></h3>
<p><?php echo $row_description['des']; ?></p>
</li>
<?php
}
?>
</ul>
Use a foreach to loop through the results and include the <li> in that loop to get a list element per row returned from the query.
$img = mysql_query($query_img, $connection) or die(mysql_error());
$result = mysql_fetch_assoc($img);
foreach ($result as $row_img)
{
?>
<li>
<img src="images/<?php echo $row_img['imgname']; ?>">
<h3><?php echo $row_img['title']; ?></h3>
<p><?php echo $row_img['des']; ?></p>
</li>
<?php
}

check if specific row exists mysql

I have a mysqli query which fetches all the images from the table(I have 5 images that I display). I am using a jquery slider to display them. The problem is if there are no 5 images I see blank page like if the user uploads just two images then the rest three thumbnails will be empty and when you click on them it shows empty area. I don't want that happen so how do I only show the thumbnail if the image exists instead of showing empty thumbnail?
I tried the below but this doesn't work. I just need to see if image_one exists then show the thumbnail and the same for the rest of the images.
<?php
$stmt = $mydb->prepare("SELECT * FROM table where title = ? AND id = ? limit 1 ");
$stmt->bind_param('ss', $title, $id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$path = 'images/';
?>
<div id="slides">
<?php if($result->num_rows > 0){?><div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_one']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_one']?>"/></a></div><?php };?>
<?php if($result->num_rows > 0){?><div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_two']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_two']?>"/></a></div><?php };?>
<?php if($result->num_rows > 0){?> <div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_three']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_three']?>"/></a></div><?php };?>
<?php if($result->num_rows > 0){?><div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_four']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_four']?>"/></a></div><?php };?>
<?php if($result->num_rows > 0){?><div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_five']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_five']?>"/></a></div><?php };?>
</div>
<div id="slide_menu">
<ul id="slide"> <!-- This is the thumbnail area -->
<li class="fbar"> </li>
<?php if($result->num_rows > 0){?><li class="menuItem"><img src="<?php echo $path.$row['image_one']?>" /></li><?php }; ?>
<?php if($result->num_rows > 0){?><li class="menuItem"><img src="<?php echo $path.$row['image_two']?>" /></li><?php }; ?>
<?php if($result->num_rows > 0){?><li class="menuItem"><img src="<?php echo $path.$row['image_three']?>" /></li><?php }; ?>
<?php if($result->num_rows > 0){?><li class="menuItem"><img src="<?php echo $path.$row['image_four']?>" /></li><?php }; ?>
<?php if($result->num_rows > 0){?><li class="menuItem"><img src="<?php echo $path.$row['image_five']?>" /></li><?php }; ?>
</ul>
</div>
we can not see your database design so look at your default value for
image_one and so on
The row count is allways 0 or 1 (because of Limit 1)
Important are the fields image_one to image_five
These fields are always present, regardless of whether they are empty or with file names are filled.
depending on the default value test it
for example one of
if ($row['image_one'] > '') {
if ($row['image_one'] > null {
put an if arround building html.
<?php if($result->num_rows > 0){?>
<?php if ($row['image_one'] > '')
{?>
<li class="menuItem">
<img src="<?php echo $path.$row['image_one']?>" />
</li>
<?php
}?>
.... next 4 other tests
<?php if ($row['image_two'] > '')
....
<?php } // END__$result->num_rows > 0 ?>
try something like
...
<ul id="slide"> <!-- This is the thumbnail area -->
<li class="fbar"> </li>
<?php
foreach ( $row as $element => $val){
if(isset($val)) {
print "<li class='menuItem'><a href=''><img src=".$path.$val."/></a></li>";
}
}
?>
</ul>
...
and the same for the 1st (<div id="slides">) block
Im not 100% sure for pdo but its not too far away
<?php
$sql = "SELECT * FROM table where title = $title AND id = $id limit 1 ";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
?>
<div class="slide">
<a class="fancybox" href="images/<?php echo $row['DB-IMAGE-PATH-HERE']; ?>" data-fancybox-group="gallery">
<img class="cloudzoom appsld" src="images/<?php echo $row['DB-IMAGE-PATH-HERE']; ?>"/>
</a>
</div>
<?php } // end while loop ?>
</div> <!-- end id="slides" -->

How do I show all the results from an SQL statement?

I wanted to display all of my shirts but I always get the same error
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /homepages/36/d362778419/htdocs/home/specials.php on line 34
Here is the code I have currently in place
<?php
require "connect2.php";
$sql = mysql_query("SELECT * FROM shirts WHERE all='1'");
$runrows = mysql_fetch_array($sql);
$title = $runrows['title'];
$picture = $runrows['picture'];
$newp = $runrows['newp'];
$date = strftime("%b %d, %Y %l:%M %p" ,strtotime($runrows['date']));
$oldp=$runrows['oldp'];
$viewl=$runrows['viewl'];
$shirtl = $runrows['shirtl'];
echo "";
?>
<div class="specialsListBoxContents centeredContent back" style="width:25%;"><div class="product-col" > <div class="img">
<a href="detail.php?id=<?php echo $id; ?>"><img src="<?php echo $picture; ?>" alt="<?php echo $title; ?>" title=" <?php echo $title; ?> " width="190"
height="160" /></a> </div> <div class="prod-info"> <div class="wrapper">
<div class="price"> <strong><span class="normalprice"><?php echo $oldp; ?
></span><br /><span class="productSpecialPrice"><?php echo $newp; ?></span></strong> </div>
<div class="button"><a href="detail.php?id=<?php echo $id; ?>"><img src="images/button_add_to_cart.gif" alt="Add to Cart" title=" Add to Cart " width="54"
height="49" /></a></div> </div> </div> </div></div>
<?php
?>
mysql_*() functions return boolean FALSE if they fail, which means your query call did not succeed. Add this code to find out the reason why:
$sql = mysql_query("SELECT * FROM shirts WHERE all='1'");
if ($sql === FALSE) {
die(mysql_error());
}
Here's some sample code:
<?php
$sql = mysql_query("SELECT * FROM shirts WHERE `all`='1'");
if (!$sql ) {
echo "Could not successfully run query from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($sql) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
while ($row = mysql_fetch_assoc($sql))
{
?>
<a href="detail.php?id=<?php echo $row['id']; ?>"><img src="<?php echo $row['picture']; ?>" alt="<?php echo echo $row['title']; ?>" title=" <?php echo $row['title']; ?> " width="190" height="160" />
<?php
}
?>
And here is more info. Notice the exiting php mode inside the while.

Categories