Inserting MySQL data as an image path - php

When I try to do simple code like this everything works fine:
<?php
require 'connect.php';
if($result = $con->query("SELECT * FROM table")) {
if($row = $result->fetch_object()) {
echo $row->imgName, ' ',$row->divId, '<br>';
}
}
?>
But inserting an image is not working for some reason:
<?php
require 'connect.php';
if($result = $con->query("SELECT * FROM table")) {
if($row = $result->fetch_object()) {
echo "<img src='img/".$row['imgName']."' />";
}
}
?>
Can you help me out, please!

Change $row['imgName'] to $row->imgName

Related

How can I change the id of an SQL query automatically via PHP?

I'm creating a news website, and when I create a new article, the title/text are registered on the database. The articles are in a dynamic PHP page with header/footer, with the text itself being called from the database . On the index there is a list with links to every article, and I want those links to change the id automatically.
index.php
<?php include "header.php"; session_start();?>
<?php include "slideshow.php"; ?>
<?php include "db.php"; ?>
<br>
<table class="table_index">
<?php
while ($line = mysqli_fetch_array($query)) {
echo '<tr><td class="td_tit"><a href="example_article.php" >'.$line["title_article"].'</a></td></tr>';
echo '<tr><td class="td_subt"><a href="example_article.php" >'.$line["subtitle_article"].'</a></td></tr>';
$_SESSION['id_article'] = $line["id_tb_article"];
}
?>
</table>
<?php include "footer.php"; ?>
example_article.php
<?php
session_start();
include "header.php";
$id_article = $_SESSION['id_article'];
$query = "SELECT title_article, subtitle_article, content_article FROM tb_article WHERE id_tb_article = $id_article";
$conn = mysqli_connect('127.0.0.1:3307', 'root', '', 'article') or die("error");
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<div class='titlediv'><h1 class='title'>" . $row["title_article"]. "</h1></div><div class='titlediv'><h3 class='title'>". $row["subtitle_article"]. "</h3></div><div class='textdiv'><p class='text'>" . $row["content_article"]. "</p></div><br>";
}
} else {
echo "0 results";
}
include "footer.php";
?>
You shouldn't use session parameters for this task. Instead use get parameters (or post) here is a little example. Also your code is vulnerable to SQL-Injection, but thats your problem :D
Index.php rewrite your while loop like this
while ($line = mysqli_fetch_array($query)) {
echo '<tr><td class="td_tit">'.$line["title_article"].'</td></tr>';
echo '<tr><td class="td_subt">'.$line["subtitle_article"].'</td></tr>';
}
then fetch the id in example_article.php like this:
$id_article = $_GET['id_article'];

PHP - While loop not showing table records

In below code it shows table row value outside while loop but not shows inside while loop. While loop not working; Please let me know whats wrong in it?
<?php
$sql = "SELECT * FROM cl_banner ORDER BY id;";
$res = q($sql) or die(mysql_error());
if($res && mysql_num_rows($res)>0)
{
while($row = mysql_fetch_assoc($res));
{
echo $row["title"];
echo "hi";
} // End While
} // End If
?>
<?php
$sql = "SELECT * FROM cl_banner ORDER BY id";
^^^^
$res = mysql_query($sql) or die(mysql_error());
^^^^^^^^^^
if($res && mysql_num_rows($res)>0)
{
while($row = mysql_fetch_assoc($res));
{
echo $row["title"];
echo "hi";
} // End While
} // End If
?>
Try this:
<?php
$sql = "SELECT * FROM cl_banner ORDER BY id";
$res = mysql_query($sql) or die(mysql_error());
if($res && mysql_num_rows($res)>0)
{
while($row = mysql_fetch_assoc($res))
{
echo $row["title"];
echo "hi";
} // End While
} // End If
?>

Why is my images not appearing, when trying to get them from database

Hiya I am getting information from my database to present onto the webpage, everything is correct with connection as the information is present on the page.
But when it comes to the pictures they come up with a little box of where they are meant to be.
Heres the code I have:
<?php
error_reporting(0);
require './db/connect.php';
include './includes/header.php';
?>
<h2>Production</h2>
<?php
if($result = $connection->query("SELECT * FROM Production")){
if($count = $result->num_rows){
$Image = $rows['Image'];
while($row = $result->fetch_object()){
echo '<pre>'.'<img class="productionimages" src="path/'.$Image.'" />',' '
,$row->ProductionName,' ',$row->ProductionType, '</pre>';
}
$result->free();
}
}
echo $result;
include './includes/footer.php';
?>
Heres a picture below of what appears on the screen.
You need to set $Image within the while loop and $rows['Image'] should be $row->Image, like so.
<?php
if($result = $connection->query("SELECT * FROM Production")){
if($count = $result->num_rows){
while($row = $result->fetch_object()){
$Image = $row->Image;
echo '<pre>'.'<img class="productionimages" src="path/'.$Image.'" />',' '
,$row->ProductionName,' ',$row->ProductionType, '</pre>';
}
$result->free();
}
}
echo $result;
include './includes/footer.php';
?>

PHP Page stops loading after script

So this is a quick question I have about my php page. I wont put the code up because it would turn into solving the syntax. I was looking for the reason this happens.
This being, I have a .php page and it loads and about half way down and there is a short bit of php code. And in it, it echos to add a few items to a database. But then none of the rest of the html page loads.
Its a php type file.
Actually here is the part within the html.
<select id="price_select" name="priceBox">
<?php
...
//connects to database in code not shown
$result=mysqli_query("SELECT * FROM $tablename")or die(mysql_error());
$count=0;
while($row=mysqli_fetch_array($result)) {
$price = $row['price'];
if($price != NULL){
($count = $count+1);
}
echo "$(\"#price_select\").append('<option>" . $price . "</option>');";
}
if($count==0) {
echo "$(\"#price_select\").append('<option>Out of Stock</option>');";
}
?>
</select>
You have error on the following line:
$result=mysqli_query("SELECT * FROM $tablename")or die(mysql_error());
You must have similar:
$result=mysqli_query($link,"SELECT * FROM $tablename")or die(mysqli_error($link));
You php code should be similar:
$result=mysqli_query($link,"SELECT * FROM $tablename")or die(mysqli_error($link));
$count=0;
while($row=mysqli_fetch_array($result)) {
$price = $row['price'];
if($price != NULL){
($count = $count+1);
echo "<option> $price </option>";
}
}
if ($count === 0)
{
echo "<option>Out of Stock</option>";
}
change your code like below.
$html = '';
while($row=mysqli_fetch_array($result)) {
$price = $row['price'];
if($price != NULL){
($count = $count+1);
}
$html .= '<option>" . $price . "</option>';
}
if($count==0) {
echo '<option>Out of Stock</option>';
} else {
echo $html;
}
Modify the code adding <script>-tag as follows:
<select id="price_select" name="priceBox">
<?php
...
//connects to database in code not shown
$result=mysqli_query("SELECT * FROM $tablename")or die(mysql_error());
$count=0;
while($row=mysqli_fetch_array($result)) {
$price = $row['price'];
if($price != NULL){
($count = $count+1);
}
echo "<script>$(\"#price_select\").append('<option>" . $price . "</option>');</script>";
}
if($count==0) {
echo "<script>$(\"#price_select\").append('<option>Out of Stock</option>');</script>";
}
?>
</select>

mysql_fetch_array not fetching complete data?

I have a code which fetches data from a mysql table and converts it into pdf document, the code is working fine except it is skipping row 1.
Here is the code from which i have removed the pdf generation process since the problem is in the loop which is fetching data.
Please help.
<?php
session_start();
if(isset($_SESSION['user']))
{
$cr = $_POST['cour'];
$s = $_POST['sem'];
require('fpdf.php');
include('../includes/connection.php');
$sql = "SELECT * FROM `student` WHERE AppliedCourse ='$cr'";
$rs = mysql_query($sql) or die($sql. "<br/>".mysql_error());
if(!mysql_fetch_array($rs))
{
$_SESSION['db_error'] = "<h2><font color = 'RED'>No such course found! Pease select again.</font></h2>";
header('Location: prinrepo.php');
}
else {
for($i = 0;$i <= $row = mysql_fetch_array($rs);$i++)
{
$formno[$i] = $row ['FormNo'];
$rno[$i] = $row ['rollno'];
$snm[$i] = $row ['StudentNm'];
$fnm[$i] = $row ['FathersNm'];
$mnm[$i] = $row ['MothersNm'];
$addr[$i] = $row['Address'];
$pic[$i] = $row['imagenm'];
$comm[$i] = $row['SocialCat'];
echo $formno[$i]."<br />";
echo $rno[$i]."<br />";
echo $snm[$i]."<br />";
echo $fnm[$i]."<br />";
echo $mnm[$i]."<br />";
echo $addr[$i]."<br />";
echo $pic[$i]."<br />";
echo $comm[$i]."<br />";
echo "<br />";
}
}
mysql_close($con);
}
?>
You are fetching the first row outside of your for() loop then you miss it.
After mysql_query() your should use mysql_num_rows() to check if there are any rows in your result and then fetch them in the for loop.
More info here : http://php.net/manual/fr/function.mysql-num-rows.php
Your code would look like this :
$sql = "SELECT * FROM `student` WHERE AppliedCourse ='$cr'";
$rs = mysql_query($sql) or die($sql. "<br/>".mysql_error());
if(0 == mysql_num_rows($rs)) {
$_SESSION['db_error'] = "<h2><font color = 'RED'>No such course found! Pease select again.</font></h2>";
header('Location: prinrepo.php');
} else {
for($i = 0;$i <= $row = mysql_fetch_array($rs);$i++)
{
// Your code
}
}

Categories