I am trying to display an image using php. The image is stored in MYSQL table. I am able to retrieve the information from mysql in my php code, but i am having trouble displaying the image.
$db_link = mysql_connect($host_name, $user_name, $password) or die("Could not connect to $host_name");
mysql_select_db("gameportal") or die("Could not select database $db_name");
$query = "select * from gamesinfo;";
$result = mysql_query($query, $db_link) or die("Could not select");
$Row = mysql_fetch_row($result);
echo "$Row[0]<br>";
echo "$Row[1]<br>";
echo "<img src="$Row[7]" class="body" alt="" /> <br>";//image
echo "$Row[5]<br>";
Row 7 contains the location of the image (in this case a weblink). When i try to display the webpage, the page is blank, nothing shows, but when i remove that line with the pic, the webpage shows with the remaining info. What am i doing wrong?
This is the culprit:
echo "<img src="$Row[7]" class="body" alt="" /> <br>";
You use unquoted double quotes inside double quotes ;-). Try
echo "<img src='$Row[7]' class='body' alt='' /> <br>";
EDIT
The point is not the double quotes inside double quotes, but unquoted double quotes inside double quotes - this should work as well:
echo "<img src=\"$Row[7]\" class=\"body\" alt=\"\" /> <br>";
I missed this the first time but:
<?php
$db_link = mysql_connect($host_name, $user_name, $password) or die("Could not connect to $host_name");
mysql_select_db("gameportal")bor die("Could not select database $db_name");
You've got bor instead of or. Make sure to turn PHP errors on.
Try this
Images is a directory where the images are stored.
$dir="images/";
**
echo "<img src='$dir/$row[image]' width=100px height=100px>";
**
It works fine.
Not an exact answer.... but a small piece of advice. I have written an answer because I don't have the reputation for commenting. You can turn on error reporting with this line at the top of php script.
error_reporting(-1);
Such kinds of errors would be displayed on screen and you would be able to debug yourself. When your work is done you can simply do this...
error_reporting(0);
Refer this link: PHP error reporting
Related
I'm struggling with an update statement using php. The problem occurs when the string in the variable $fltr contains more than one word.
$fltr ="Two Words or More" does not work
$fltr="OneWordOnly" works fine.
require_once('includes/init.php');
$Login = new Login( $db );
if(empty($_SESSION['logged']))
{
$Login->_logout();
}
if(isset($_SESSION['uid']))
{
$userid=(int)$_SESSION['uid'];
}
$fltr = $_GET['filter'];
$fltr= pg_escape_string($fltr);
$deakt="D";
$updSQL="update class_products set FTP ='".$deakt."' where title ='".$fltr."' and owner =".$userid;
$db->query($updSQL) or die ("Error in query: $updSQL " . mysql_error());
$txt='<textarea style="margin-left:250px;margin-top:110px; font-family:verdana:font-size:14px;color:#000;">Search filter {$fltr} is deactivated</textarea>';
$class_tpl->assign('txt', $txt);
//now display the template
$class_tpl->display('cbUpdate.tpl.php');
Normally I would print the $updSQL to see the what the problem is, but I am using this in a LightBox variant and having problems debugging.
Any help is highly appreciated.
My guess is the value is urlencoded, and you need to urldecode it, before escaping.
I am brand new to php/mysql, so please excuse my level of knowledge here, and feel free to direct me in a better direction, if what I am doing is out of date.
I am pulling in information from a database to fill in a landing page. The layout starts with an image on the left and a headline to the right. Here, I am using the query to retrieve a page headline text:
<?php
$result = mysql_query("SELECT banner_headline FROM low_engagement WHERE thread_segment = 'a3'", $connection);
if(!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo $row["banner_headline"];
}
?>
This works great, but now I want to duplicate that headline text inside the img alt tag. What is the best way to duplicate this queries information inside the alt tag? Is there any abbreviated code I can use for this, or is it better to just copy this code inside the alt tag and run it twice?
Thanks for any insight!
You are, as the comment says, using deprecated functions, but to answer your question, you should declare a variable to hold the value once your retrieve it from the database so that you can use it whenever your want.
<?php
$result = mysql_query("SELECT banner_headline FROM low_engagement WHERE thread_segment = 'a3'", $connection);
if(!$result) {
die("Database query failed: " . mysql_error());
}
$bannerHeadline = "";
while ($row = mysql_fetch_array($result)) {
$bannerHeadline = $row["banner_headline"];
}
echo $bannerHeadline; //use this wherever you want
?>
It is hard to help without knowing more. You are pumping the results into an array, are you expecting to only return one result or many banner_headline results? If you will only ever get one result then all you need to do is something like this:
PHP:
$result = mysql_query("
SELECT `banner_headline`
FROM `low_engagement`
WHERE `thread_segment` = 'a3'", $connection) or die(mysql_error());
// This will get the zero index, meaning first result only
$alt = mysql_result($result,0,"banner_headline");
HTML:
<html>
<body>
<!--- Rest of code -->
<img src="" alt="<?php echo $alt ?>">
On a side note, you should stop using mysql-* functions, they are deprecated.
You should look into PDO or mysqli
So I have the following PHP code
<?php
error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
$poscote = $_POST['postcode'];
mysql_real_escape_string($poscote);
//! Checks for direct access to page
if (empty($_POST)) {
header('location:index.php?nothingentered');
die();
}
require_once('../Connections/PropSuite.php');
mysql_select_db($database_Takeaway, $Takeaway);
$query_PC = "SELECT * FROM Postcodes WHERE pc = '$postcode'";
$PC = mysql_query($query_PC, $Takeaway) or die(mysql_error());
$row_PC = mysql_fetch_assoc($PC);
if( mysql_errno() != 0){
// mysql error
// note: message like this should never appear to user, should be only stored in log
echo "Mysql error: " . htmlspecialchars( mysql_error());
die();
}
else {
echo $row_PC['oc'];
}
?>
This is to process a form with the following code
<form action="search_postcode.php" method="post">
<input type="text" name="postcode" />
<button>Go</button>
</form>
Strangely its just showing a blank screen, no errors, nothing I have checked through and cannot seem to find a solution.
Many thanks in advance for your help.
As your $postcode variable is undefined, you are looking in your database for a row where pc is an error message.
That query could very well finish without errors, but it probably produces 0 rows, so you don't have an error, nor do you have a result. In that case you output nothing, so you will see a blank screen.
You probably want:
$postcode = mysql_real_escape_string($poscote);
instead of:
mysql_real_escape_string($poscote);
and put it below the database connection section.
Also, you should switch to PDO (or mysqli) and prepared statements to avoid sql injection problems and because the mysql_* functions are deprecated. Note that your mysql_real_escape_string does not do anything (except removing the contents of your variable...) when you don't have a database connection open.
In addition to the other answers, and without mentioning that you should be using PDO or mysqli, you could be having a character encoding issue. Try doing something like this:
define('DB_CHARSET', 'ISO-8859-1');
define('REPLACE_FLAGS', ENT_COMPAT | ENT_XHTML);
...
echo "Mysql error: " . htmlentities(mysql_error(), REPLACE_FLAGS, DB_CHARSET);
Replace the value of DB_CHARSET with whatever encoding your database is using. If you try to use htmlentities() with an invalid character it will produce an empty string.
As of php.net, to enable php errors using the ini_set, you have to do it like this
ini_set('display_errors', '1')
This is taken from this link
i am relatively new to php. I have a problem with displaying images in my browser ( google chrome) after retrieving blob data from mysql database.
Basically the following code works when the slashes are added in front of the echo at the bottom. However i have followed other online tutorials and the tutor has been able to display their images without the use of slashes whilst i am unable to get the image up. I just wondered what the standard rule is? Another thing to add - when i do fail to get the images up in the browser i get instead a ting thumbnail. I would really appreciate if anybody could tell me how to reliably display images. The site i wish to create is just about images. So its kind of fundamental. Thanks a lot in advance for your time.
<?php
$conn = mysql_connect ("localhost","root","arctic123");
$db = mysql_select_db ("user_images", $conn);
if(!$db) {
echo mysql_error();
}
$q = "SELECT * FROM images";
$r = mysql_query ("$q",$conn);
if($r) {
while($row = mysql_fetch_array($r)) {
//echo $row ['username'];
//echo "<br>";
header ("Content-type: image/jpeg");
echo $row ['logo'];
//echo "<br>";
}
}else{
echo mysql_error();
}
?>
You can't have header after any output in your code: http://php.net/manual/en/function.header.php
Best practice is to upload images to a directory and just store the image's path/file name in the database. Also makes it easier to manipulate the image, e.g. create different sizes and thumbnails with PHP. And it takes about four times less the disk space...
Hope you are not storing your images on MySQL, please if you do, desist from that detrimental act because it is going to make your database unnecessarily heavy...
I would recommend not storing your images in a database. While it is technically possible, it has serious performance concerns. Best practice would be to designate a folder for the images, then access them directly. If you'd like the filtering and sorting ablities of MySQL, then replace the BLOB column that currently stores the image data with a VARCHAR containing the file name.
<?php
$conn = mysql_connect ("localhost","root","arctic123");
$db = mysql_select_db ("user_images", $conn);
$imgdir = "/path/to/image/directory/";
if(!$db) {
echo mysql_error();
}
$q = "SELECT * FROM images";
$r = mysql_query ("$q",$conn);
if($r) {
while($row = mysql_fetch_array($r)) {
echo $row['username'];
echo "<br>";
echo "<img src='" . $imgdir . $row['logo'] . "' />";
echo "<br>";
}
}else{
echo mysql_error();
}
?>
I did wrote piece of php code to show 'product' name from a database.But nothing shows up after executing,Database is ok,i double checked database,table name,other fieldsI couldn't figure out the error so please help.
code
<?php
mysql_connect('localhost','root');
mysql_select_db('cybersoft');
$no=1;
$res=mysql_query("select product from test where 'serial'=$no ");
while($rowa=mysql_fetch_array($res))
{
echo $rowa[1];
}
?>
First of all, mysql_connect() takes three parameters.
mysql_connect('localhost', 'root', 'mypassword');
and to see an error you can use mysql_error() function
mysql_query('some query') or die(mysql_error());
Change single quote (') to ` or just remove. Quotes are used for string types.
select product from test where `serial`= $no
Remove your quotes around serial, they are not needed. This should work:
$res=mysql_query("select product from test where serial=$no");
If result is still empty, double check that there is a row with serial = 1.
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Error: ' . mysql_error());
}
mysql_select_db('cybersoft');
$res=mysql_query("select product from test where serial=" . $no);
Remove the single quotes around "serial", or make them backticks instead.
Also, you likely aren't seeing anything when you run the script because you don't have error reporting turned on. Do that in php.ini or programatically:
error_reporting(E_ALL);
Try this
mysql_connect('localhost','root');
mysql_select_db('cybersoft');
$no=1;
$res=mysql_query("select product from test where serial='$no' ");
while($rowa=mysql_fetch_array($res))
{
echo $rowa[1];
}
?>
Try this
$res=mysql_query("select product from test where serial=$no");