This is my sample code please help me guys
<?php
$con =mysql_connect("localhost", "" , "");
$sdb= mysql_select_db("image",$con);
$sql = "SELECT * FROM `tbl_image` WHERE id=21";
$mq = mysql_query($sql) or die ("not working query");
$row = mysql_fetch_array($mq) or die("line 44 not working");
$s = $row['img_url'];
echo '<img src="'.$s.'">';
?>
loop row.
$mq = mysql_query($sql) or die ("not working query");
while ($row = mysql_fetch_array($mq)) {
echo '<img src="' . $row['img_url'] . '" >';
}
should change your last 2 lines to this
while ($row = mysql_fetch_assoc($result)) {
$s = $row['img_url'];
echo '<img src="'.$s.'">';
}
<?php
$con = mysql_connect("localhost", "" , "");
$sdb = mysql_select_db("image",$con);
$sql = "SELECT * FROM `tbl_image` WHERE id=21";
//echo "SELECT * FROM `tbl_image` WHERE id=21"; //and run the query in database if it is working fine then you can use while loop.
$mq = mysql_query($sql) or die ("not working query"); // if this line is not working try eho the above query like i have mentioned in comments.
while ( $row = mysql_fetch_array($mq)) {
echo '<img src="' . $row['img_url'] . '" >';
}
?>
You can try following code.
while($row=mysql_fetch_object($qry))
{
$result[] = $row['img_url'];
echo '<img src="' . $row['img_url'] . '" >';
}
print_r($result)
Related
i am training to add and display the img from the database, but i cant display the img from the database
i have used this code .. How can i solve it?
<?php
$conn = #mysqli_connect("localhost", "root", "");
if ($conn) {
$db = mysqli_select_db($conn, "name_of_db");
}
$query = mysqli_query($conn, "SELECT * FROM Table_name");
while ($row = mysqli_fetch_object($query)) {
$ID = $row->ID;
$adi = $row->adi;
$adres = $row->adres;
$te11 = $row->Tel1;
$tel2 = $row->Tel2;
$TC = $row->TC;
$Pasaport = $row->Pasaport;
$Para = $row->Para;
$Odedi = $row->Odedi;
$foto = $row->fotograf;
$Mekkeotel = $row->Mekkeotel;
$Medineotel = $row->Medineotel;
echo '<img src="' . $row->field_name_of_photo_in_db . '" border=0>';
}
?>
You would recommend you not assigning all the information to separe variables, because it doesn't make any difference. You are just increasing your typing and you are basically losing more time. As for your question, change
echo '<img src="' . $row->field_name_of_photo_in_db . '" border=0>';
to
echo '<img src="' . $row->fotograf . '" border=0>';
and it should work fine now.
I am trying to show the blop images i have in my mysql database. But all i get is a whitebox with no picture in it.
PHP code:
<?php
require_once "include/config.php";
session_start();
$sql = "SELECT * FROM docenten";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "<dt><strong>Foto:</strong></dt><dd>" .
'<img src="data:image/jpeg;base64,'.
base64_encode($row['foto']).
'" width="290" height="290">' . "</dd>";
}
}else{
echo "0 result";
}
?>
But when the code runs all i get is this: http://imgur.com/nhIO2LQ
Anyone know a solution?
use addslashes before insert
$img = addslashes(file_get_contents($_FILES['images']['tmp_name']));
$query = "INSERT INTO tableName (id,image) VALUES('','$image')";
I am trying display images on webpage, where image path stored in database and images is stored in server.But i am not able to display those images using following codes, so pls somebody help me with this issue,..
<form method="post" enctype="multipart/form-data" action="file_upload.php">
<table>
<?php
$dbhost = 'xxxxxxxx';
$dbuser = 'xxxxxxxxx';
$dbpass = 'xxxxxxxxxx';
$db_name = 'xxxxxxxxxx';
$tbl_name = 'xxxxxxxxxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("$db_name")or die("cannot select DB");
$query1 = mysql_query("select * from '$tbl_name' where id='1'");
$rows1 = mysql_fetch_array($query1);
$path1 = $rows1['image'];
$query2 = mysql_query("select * from '$tbl_name' where id='2'");
$rows2 = mysql_fetch_array($query2);
$path2 = $rows2['image'];
$query3 = mysql_query("select * from '$tbl_name' where id='3'");
$rows3 = mysql_fetch_array($query3);
$path3 = $rows3['image'];
echo '<tr><td><img src="$path1"></td>' ;
echo '<td><img src="$path2"></td>' ;
echo '<td><img src="$path3"></td></tr>' ;
?>
</form>
</table>
Take a look à this code :
$b = "aaaaaaa";
echo '"$b"'; // will displayt "$b"
echo "'$b'"; // will displayt 'aaaaaaa'
echo "\"$b\"";// will displayt "aaaaaaa"
enter code here
So, for your problem it's simply because if you want a variable to be evaluated in string you must put it between "" and not ''.
<table>
<?php
$path1 = "/upload/image1.png";
$path2 = "/upload/image2.png";
$path3 = "/upload/image3.png";
echo "<tr><td><img src='$path1'></td>" ;
echo "<td><img src='$path2'></td>" ;
echo "<td><img src='$path3'></td></tr>" ;
?>
</table>
Or
<table>
<?php
$path1 = "/upload/image1.png";
$path2 = "/upload/image2.png";
$path3 = "/upload/image3.png";
echo '<tr><td><img src="'.$path1.'"></td>' ;
echo '<td><img src="'.$path2.'"></td>' ;
echo '<td><img src="'.$path3.'"></td></tr>' ;
?>
</table>
Anas
If you have the uploads directory in the current path, try the following - notice the period in front of $pathX telling, it is current directory.
echo '<tr><td><img src=".$path1"></td>' ;
echo '<td><img src=".$path2"></td>' ;
echo '<td><img src=".$path3"></td></tr>' ;
(Stop using mysql_* functions as they are depreciated long back)
Use mysqli or PDO
in your example use loop so that easily access all the rows in resultset like below
$query1 = mysql_query("select * from ".$tbl_name);
while($rows = mysql_fetch_array($query1))
{
$path = $_SERVER['DOCUMENT_ROOT'].$rows['image'];
?>
<tr><td><img src="<?=$path?>"></td>
<?php
}
Change:
mysql_query("select * from '$tbl_name' where id='3'");
to
mysql_query("select * from '".$tbl_name."' where id='3'");
**Try it**
<table>
<?php
$dbhost = 'xxxxxxxx';
$dbuser = 'xxxxxxxxx';
$dbpass = 'xxxxxxxxxx';
$db_name = 'xxxxxxxxxx';
$tbl_name = 'xxxxxxxxxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("$db_name")or die("cannot select DB");
$query = mysql_query("SELECT * FROM $tbl_name");
while($array = mysql_fetch_assoc($query))
{
echo '<tr>';
echo "<td><img src='". $v['image'] ."' width='200' height='200'></td>" ;
echo '</tr>';
}
?>
</form>
</table>
<table>
<?php
try
{
$db = new PDO('mysql:host=localhost;dbname=test', 'root', 'password');
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
$query = $db->query("SELECT * FROM '$tbl_name'")->fetch();
echo '<tr>';
foreach($query as $k => $v){
echo '<td><img src="' . $v['image'] . '"></td>' ;
}
echo '</tr>';
?>
</table>
There is a problem with your queries:
mysql_query("select * from '$tbl_name' where id='3'");
if you write it like this, the variable $tbl_name doesn't get resolved.
Try it like this:
mysql_query("select * from '".$tbl_name."' where id='3'");
same for the src-attribute of the image elements
i Have This PHP and JS Code on my web page.
<?php
include 'connect1.php';
$query = "SELECT URL FROM remontee_nlf ORDER BY URL ASC";
$result = mysql_query($query) or die (mysql_error());;
$counter = 0;
// write the values from the database into the javascript array
echo "<script type='text/javascript'>";
echo "this.styleListArray = new Array();";
if ($result) {
while($row = mysql_fetch_array($result)) {
echo("this.nameArray[" . $counter . "] = '" . $row['URL'] . ", " . $row['user_fname'] . "';"); // displays 'lname, fname'
$counter += 1;
}
}
echo("</script>");
?>
The Problem is that when i execute the page containing the code, a part of this code doesn't get executed and it just shows on the page as a simple text :
"); echo "this.styleListArray = new Array();"; if ($result) { while($row = mysql_fetch_array($result)) { echo("this.nameArray[" . $counter . "] = '" . $row['URL'] . ", " . $row['user_fname'] . "';"); // displays 'lname, fname' $counter += 1; } } echo(""); ?>
I tried to figure it out, but i couldn't get it, if you can help brothers, that would be wonderful.
Try rewriting your code like this:
include 'connect1.php';
$query = "SELECT URL FROM remontee_nlf ORDER BY URL ASC";
$result = mysql_query($query) or die (mysql_error());
$counter = 0;
// write the values from the database into the javascript array
echo <<<HTML
<script type='text/javascript'>
this.styleListArray = new Array();
HTML;
$strLine = '';
if ($result) {
while($row = mysql_fetch_array($result)) {
$strLine.= "this.nameArray[" . $counter . "] = '" . $row['URL'] . ", " . $row['user_fname'] . "';";
$counter += 1;
}
}
echo $strLine;
echo("</script>");
First of all change your queries to use mysqli or pdo connections
secondly try following code
$sql = 'SELECT URL FROM remontee_nlf ORDER BY URL ASC';
$res = mysql_query($sql, $con);
$rows = array();
while ($row = mysql_fetch_assoc($res))
$rows[] = $row['URL'];
$str = implode('", "', $rows);
$data = '["'.trim($str).'"]';
echo '<script type="text/javascript">';
echo "var data = $data;";
echo 'console.log(data)';
echo '</script>';
check you console log.
You have $result = mysql_query($query) or die (mysql_error());; change it to
$result = mysql_query($query) or die (mysql_error());
Also make sure $row['URL'] and $row['user_fname'] are available.
i'm trying to run this php code which should display a quote from mysql, but can't figure out where is it going wrong. the result variable is null or empty. can someone help me out. thanks!
<?php
include 'config.php';
// 'text' is the name of your table that contains
// the information you want to pull from
$rowcount = mysql_query("select count(*) as rows from quotes");
// Gets the total number of items pulled from database.
while ($row = mysql_fetch_assoc($rowcount))
{
$max = $row["rows"];
//print_r ($max);
}
// Selects an item's index at random
$rand = rand(1,$max)-1;
print_r ($rand);
$result = mysql_query("select * from quotes limit $rand, 1") or die ('Error: '.mysql_error());
if (!$result or mysql_num_rows($result))
{
echo "Empty";
}
else{
while ($row = mysql_fetch_array($result)) {
$randomOutput = $row['cQuotes'];
echo '<p>' . $randomOutput . '</p>';
}
}
$result = mysql_query("SELECT * FROM quotes ORDER BY rand() LIMIT 1") or die ('Error: '.mysql_error());
if (!$result || mysql_num_rows($result) == 0)
echo "Empty";
else {
while ($row = mysql_fetch_array($result)) {
$randomOutput = $row['cQuotes'];
echo '<p>' . $randomOutput . '</p>';
}
}
// your script probably can't go on without this file?
require 'config.php';
// I prefer to always pass the connection resource to mysql_query/mysql_real_escape_string
// assume $mysql = mysql_connect....
$result = mysql_query("SELECT Count(*) AS rows FROM quotes", $mysql)
or die(mysql_error());
// there's only one row with only one column, so mysql_result() is fine
$rowcount = mysql_result($result, 0, 0);
$rand = rand(0,$rowcount-1);
$result = mysql_query("SELECT cQuotes FROM quotes LIMIT $rand, 1", $mysql)
or die ('Error: '.mysql_error());
// there's either one or zero records. Again, no need for a while loop
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if ( !$row ) {
echo "Empty";
}
else{
// do you have to treat $row['cQuotes'] with htmlspecialchars()?
echo '<p>', $row['cQuotes'], '</p>';
}
if ($result && mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$randomOutput = $row['cQuotes'];
echo '<p>' . $randomOutput . '</p>';
}
} else {
echo "Empty";
}