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.
Related
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)
I was experimenting if I could use a mySQL database to store CSS settings. I set up a simple database "colors" with one table "color" that had simple structure tag and color columns. In that, one row is h1 => red.
<?php
//function to dynamically change CSS
$tag = 'h1';
$q = "SELECT * FROM `colors` WHERE `tag`='" . $tag . "'" ;
echo $q . "<br>";
$query = mysqli_query($link, $q);
if ($row = mysqli_fetch_assoc($query))
{
echo $row['color'];
} else
{
echo "error - no such tag";
}
?>
When I tried to convert to a function, the code does not work at all.
<?php
//function to dynamically change CSS
function getCSS($tag)
{
$tag = 'h1';
$q = "SELECT * FROM `colors` WHERE `tag`='" . $tag . "'" ;
echo $q . "<br>";
$query = mysqli_query($link, $q);
if ($row = mysqli_fetch_assoc($query))
{
echo $row['color'];
} else
{
echo "error - no such tag";
}
}
getCSS('h1');
?>
Help please?
My guess is that in
$query = mysqli_query($link, $q);
$link goes out of scope and is empty. You should pass it to the function as well.
For the record: using $tag without escaping could be an sql injection attack possibility.
in function, there is no $link, you shoud define it as a global variable.
At the start of your function add a reference to your global DB link:
function getCSS($tag) {
global $link;
...
This should work:
<?php
$link = mysqli_connect('server_host', 'user', 'password', 'database') OR die('Could not connect because: '.mysqli_connect_error());
//function to dynamically change CSS
function getCSS($link, $tag){
$q = 'SELECT * FROM colors WHERE tag = "' . $tag . '"' ;
$r = mysqli_query($link, $q);
if(mysqli_num_rows($r)>0){ // check if there are results
while($row = mysqli_fetch_assoc($r)){
//echo '<pre>';
//print_r($row); // print the result array for debugging
//echo '</pre>';
echo $row['color'] . '<br />';
}
return $row;
} else { // if no result is found
echo 'No such tag';
}
}
// test it:
echo '<br />if tag is h1<br />';
getCSS($link, 'h1');
echo '<br />if tag is h2<br />';
getCSS($link, 'h2');
?>
I am trying to create a page where people can upload a picture for everyone to view.
I am trying to store the pictures in a directory and reference the file name via a MySQL table.
The uploading I have managed to sort, it stores the file where I want and adds the file name to the table.
It's viewing the image I am having trouble with. The page just shows a blank space where the pic should be but displays the rest of the info from the table correctly, i.e. who uploaded it and when.
Here is my code:
$myObject = new convertToAgo;
//var for gallery output
$returnstr = "";
$sql = mysql_query("SELECT * FROM pictures ORDER BY creation_date DESC");
while($row = mysql_fetch_array($sql)){
$username = $row["creator_name"];
$date = $row["creation_date"];
$file = $row["file_name"];
$convertedTime = ($myObject -> convert_datetime($date));
$whenAdded = ($myObject -> makeAgo($convertedTime));
$picture = "pictures/$file";
$returnstr .='<img src =\"$picture\" width="400px" height="400px" border="1px" />
<div class="response_top_div">Added by: ' . $username . ' | ' . $whenAdded . ' </div> <br/><br/><br/>';
}
Your code should look like this
$picture = "pictures/".$file;
$returnstr .='<img src ="'.$picture.'"
or better
$returnstr .='<img src ="pictures/'.$file.'"
You're trying to reference variables within strings. Try this:
$myObject = new convertToAgo;
$returnstr = "";
$sql = mysql_query("SELECT * FROM pictures ORDER BY creation_date DESC");
while($row = mysql_fetch_array($sql)){
$username = $row["creator_name"];
$date = $row["creation_date"];
$file = $row["file_name"];
$convertedTime = ($myObject -> convert_datetime($date));
$whenAdded = ($myObject -> makeAgo($convertedTime));
$picture = "pictures/" . $file;
$returnstr .= '<img src ="' . $picture . '" width="400px" height="400px" border="1px"/>
<div class="response_top_div">Added by: ' . $username . ' | ' . $whenAdded . ' </div> <br/><br/><br/>';
}
If the $returnstr variable you have is accurate, then you are actually only adding the string $picture into the src. You need to concat in the variable:
$returnstr .='<img src ="'.$picture.'" width="400px" height="400px" border="1px" />' ...
only double qoutes can accept php variable so change the begining qoutes to double like.
$returnstr = "<img src='$you_variable' rel='test_image' />";
Hope this works.
I am trying to store URL Image paths into MySQL DB. But i am not getting any results with my code.
Why is it not storing anything?
Would it be better just to store the image name or the entire path?
target example: "www.example.com/medium/imagename.jpg"
Table Name: urlimage
id: autoincrement
image_name
Php code for inserting data into DB
<?php
$images = explode(',', $_GET['i']);
$path = Configuration::getUploadUrlPath('medium', 'target');
if(is_array($images)){
$objDb = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
$objDb->exec('SET CHARACTER SET utf8');
$sql = "INSERT INTO `urlImage` (`image_name`) VALUES ";
foreach ($images as $image) {
//echo '<div><p>' . $path . $image . '</p><img src="' . $path . $image . '" /></div>';
$value[] = "(".$path.$image.")"; // collect imagenames
}
$sql .= implode(',', $value).";"; //build query
$objDb->query($sql);
}
?>
Try this; it may work:
$value[] = "('".$path.$image."')";
you have error in $sql
it must be $value[] = "('".$path.$image."')";
debug with
echo "<pre>"; print_r($sql);echo "</pre>";
and if your using PDO function then call
prepare->execute
I've got a function that should return me a set of links based on an user id. What the function does momentarily is that it returns me just one link instead a set of links based on an user id. The function looks like this:
function retrieve_image_link($user_id)
{
$query = mysql_query("SELECT `image_link` FROM `imgit_images` WHERE user_id = '" . intval($user_id) . "'");
while ($row = mysql_fetch_assoc($query))
{
$link = $row['image_link'] . '<br />';
}
mysql_free_result($query);
return $link;
}
So this is the code that should get me multiple links instead of just one. Where is the problem that the query returns a string instead of an array?
Please help!
you need to apply links to an array like $array[] = "link";
function retrieve_image_link($user_id)
{
$query = mysql_query("SELECT `image_link` FROM `imgit_images` WHERE user_id = '" . intval($user_id) . "'");
while ($row = mysql_fetch_assoc($query))
{
//add bracktes to $link to return an array
$link[] = $row['image_link'] . '<br />';
}
mysql_free_result($query);
return $link;
}
or use .= opperator if you want all the links in one big string
function retrieve_image_link($user_id)
{
$query = mysql_query("SELECT `image_link` FROM `imgit_images` WHERE user_id = '" . intval($user_id) . "'");
while ($row = mysql_fetch_assoc($query))
{
//add . to = to append string to $link
$link .= $row['image_link'] . '<br />';
}
mysql_free_result($query);
return $link;
}
It seems the problem is here
$link = $row['image_link'] . '<br />';
At the end of the loop, $link should have last value instead of all the values. You have to append each link or create an array to push each link.
$link = $row['image_link'] . '<br />';
You're overwriting the value of $link in the loop. Add [] to make it an array:
function retrieve_image_link($user_id)
{
$query = mysql_query("SELECT `image_link` FROM `imgit_images` WHERE user_id = '" . intval($user_id) . "'");
$link = array( );
while ($row = mysql_fetch_assoc($query))
{
$link[] = $row['image_link'] . '<br />';
}
mysql_free_result($query);
return $link;
}
use .=
function retrieve_image_link($user_id)
{
$query = mysql_query("SELECT `image_link` FROM `imgit_images` WHERE user_id = '" . intval($user_id) . "'");
while ($row = mysql_fetch_assoc($query))
{
$link .= $row['image_link'] . '<br />';
}
mysql_free_result($query);
return $link;
}