How to display an image in PHP? [duplicate] - php

This question already has answers here:
PHP display image BLOB from MySQL [duplicate]
(2 answers)
How to retrieve images from MySQL database and display in an html tag
(4 answers)
Closed 4 years ago.
So, I want to make search results with image in PHP. For example, when you search for a product on a website, you get the search results with images, like this:
So I've created a database with one record only (type: BLOB):
Also, I have a PHP code for display images:
<?php
$server = mysqli_connect("localhost","root","") or die("Não consigo ligar à BD");
mysqli_select_db($server,"pesquisa") or die("Não encontro a BD");
$query = mysqli_query($server, "SELECT * FROM produtos");
while($row = mysqli_fetch_array($query)) {
echo '<img height="300" width="300" src="data:image;base64, '.$row[2].' ">';
}
mysqli_close($server);
?>
But when I run it on a browser, the result is this:
Is this code wrong?

Related

Displaying items in dynamic URL page in php from SQL database [duplicate]

This question already has answers here:
How to print a MySQL database table in PHP using PDO [closed]
(2 answers)
How can I get an unknown username given an ID?
(2 answers)
Closed 2 years ago.
I have made dynamic links in PHP, to display individual bikes, using the bike ID in my website. This is my code that makes each link individual on my index page:
$bike_id = $bike["bike_id"];
echo '<a href="./item.php?id='.$bike_id.'"><input type="button" value="See More" />';
Now the issue I face is trying to display each bike item pages unique values.
for example, if you selected "Fast bike" on index.php, you'd be taken to a page that had "fast bikes" description and price.
In the database, I have the columns:
"Bike_name",
"Bike_Price",
"Bike_Description
and on each the bikes item page, I want to display these values, but I don't know how to do it.
Here is what I have so far but it's not working:
<?php
require_once(__DIR__.'/includes/db.php'); //connect to the database
$item_id = $_GET['id']; //get the ID of the item
$item_data = mysql_query("SELECT bike_name"); //get the value of the column bike_name
echo "<h2>".$item_data['bike_name']."</h2>"; //display the value of the column bike_name
?>
I was advsed to do this:
// Select item from database
// Store results in $item_data
// echo $item_data[`item_name`];
My connection (I'm not showing my passwords or usernmane)
try {
$Conn = new PDO("mysql:host=".$db_config['db_host'].";dbname=".$db_config['db_name'],$db_config['db_user'],$db_config['db_pass']);
$Conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$Conn->setAttribute(PDO::ATTR_PERSISTENT, true);
$Conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch (PDOException $e) {
echo $e->getMessage();
exit();
}

Values aren't being entered into the database [duplicate]

This question already has answers here:
How can I get useful error messages in PHP?
(41 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 2 years ago.
I am currently trying to develop my A-level computer science project so I can document it, however I am stuck on how to get some of my code to be entered into my database - it was working but I added a few bits to it and it stopped working when I was pressing the 'submit' link, the code that connects and adds to my database is shown:
if(isset($_POST['quiz'])) {
$name = $_POST['name'];
$question = $_POST['question'];
$answer = $_POST['answer'];
$dbc = mysqli_connect("","","","") or die('Error connecting to MySQL server')
$query = "INSERT INTO Quiz(name, question, answer) VALUES('$name', '$question', '$answer')";
$result = mysqli_query($db, $query) or die('Error querying database.');
mysqli_close($db);
}
echo "1 record added";
?>
Obviously I have all the details in the $dbc to connect to my database, I am a beginner with PHP and I am trying to get my head around why it isn't working, any help would be appreciated.

Can't Display Longblob images with php in cpanel [duplicate]

This question already has an answer here:
Displaying BLOB image from Mysql database into dynamic div in html
(1 answer)
Closed 3 years ago.
$con= mysqli_connect("host", "user", "password", "database");
session_start();
if(isset($_SESSION['username'])){
//echo 'Hi! '.$_SESSION['username'];
}
if(isset($_GET["name"])){
$n= "SELECT * FROM `users` WHERE `user_name`='".$_GET['name']."' "[0];
$r= mysqli_query($con, $n);
if($r){
while($row= mysqli_fetch_assoc($r)){
echo $row['Full_name'];
$image= $row['profile'];
echo $image;
}
}
}
Here is my Code. When I run it it just shows scrambled lines(!!)
What can I do to display the images which are saved as Longblob in mysql database.
This is a web-based project, coded in cpanel hosting. I tried some solutions but they didn't work. I think the problem happens in the links
I tried this solution:
<img src="data:image/jpeg;base64,<?php echo base64_encode( $image ); ?>" />
Don't use blob to store images. Use varchar to save URL of the image and upload that images to specific folder. MySQL is not designed to store images.

Why is my or die() function not working [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I am trying to create a products page in a admin panel, where the administrator can input their products. I am trying to add the product to the database and use a or die output if anything goes wrong. But it seems like every time I type in or die, I receive a error code before evening running the code (last part of code).
What is the reason for this? Please help.
<?php
// parse from data
if (isset($_POST['product_name'])) {
$pid = mysql_real_escape_string($_POST['thisID']);
$Product_Name = mysql_real_escape_string($_POST['Product_Name']);
$desc = mysql_real_escape_string($_POST['Product_Desc']);
// See if that product name is an identical match to another product in the system
$sql = mysql_query("UPDATE products SET product_name='$Product_Name'LIMIT 1");
$productMatch= mysql_mum_rows($sql);
if ($productMatch>0){
echo "Sorry you tried to place a duplicate product name";
exit();
}
//add products to database
$sql = mysql_query("INSERT INTO Product(Product_Name,Product_Desc,date_added)
VALUES('$Product_Name','$desc',now())") or die(mysql_error())
<?php
//blocks gravs
?>
You forgot to terminate(;) your code in die part and you open again a php code but you forgot to close the part last php code.so to correct it might be like this.
<?php
//..Some of your code here
$sql = mysql_query("INSERT INTO Product(Product_Name,Product_Desc,date_added)
VALUES('$Product_Name','$desc',now())") or die(mysql_error());
//blocks gravs
?>

Showing utf8_general_ci data (non-eng) on php [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Non English characters appear as question marks on my php page - appear fine in database
(2 answers)
Closed 7 years ago.
I have saved my data on MYSQL database using "utf8_general_ci". I wrote it on Bangla font.
But when I am trying to show data using php it is showing "????..." instead of showing the charecters.
<?php
$con = mysqli_connect("localhost","appsaadr_edu","pass","appsaadr_edu");
$result = mysqli_query($con,"SELECT * FROM lnews ORDER BY id DESC");
echo "{\"news\":";
$arr = array();
while($rows = mysqli_fetch_array($result)){
$test['id'] = $rows['id'];
$test['title'] = $rows['title'];
$test['text'] = $rows['text'];
$test['time'] = $rows['time'];
array_push($arr,$test);
}
echo json_encode($arr);
echo "}";
?>
What to do now?
Make sure the file is encoded in UTF-8 without BOM
Add : mysqli_set_charset($con,"utf8"); between the lines 3 and 5
Put all the $test['XXX'] like this utf8_encode($test['XXX'])
Make sure to add the data into the database also through utf8_encode before

Categories