Reputation: 383
Having code to fetch image from classic mysql this way which work greatly:
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("front");
$submit=$_GET['str'] ;
$sql = mysql_query("SELECT * FROM searcengine WHERE pagecontent LIKE '%$_GET[$submit]%' ");
while($row=mysql_fetch_array($sql)) {
echo "<img src=image_2.php?pagecontent=".$row['pagecontent']." />";
}
?>
and image_2.php:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$conn = mysql_connect("localhost","root","");
if(!$conn)
{
echo mysql_error();
}
$db = mysql_select_db("front",$conn);
if(!$db)
{
echo mysql_error();
}
$pagecontent = $_GET['pagecontent'];
$q = "SELECT pageurl FROM searchengine where pagecontent='$pagecontent'";
$sql = mysql_query("$q",$conn);
if($sql)
{
$row = mysql_fetch_array($sql);
header("Content-type: image/jpeg");
echo $row['pageurl'];
}
else
{
echo mysql_error();
}
?>
Now,i updated with latest mysqli code as follow:
<?php
$con=mysqli_connect("localhost","root","","front");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$submit=$_GET['str'] ;
$sql="SELECT * FROM 'searchengine' WHERE 'pagecontent' = '%$submit%' ";
if ($result=mysqli_query($con,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_assoc($result)) {
echo "<img src=image_2.php?pagecontent=".$row['pagecontent']." />";
}
}
mysqli_close($con);
?>
and image2.php=
<?php
$con=mysqli_connect("localhost","root","","front");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$pagecontent = $_GET['pagecontent'];
if (!empty($pagecontent)) {
$sql= "SELECT 'pageurl' FROM 'searchengine' where 'pagecontent'='$pagecontent'";
if ($result=mysqli_query($con,$sql))
{
while ($row = mysqli_fetch_assoc($result));
header("Content-type: image/jpeg");
echo $row['pageurl'];
}
?>
But in this case,i.e. image not displaying.Pls help what wrong am i?
Upvotes: 0
Views: 179
Reputation: 38
Why to go open img in new file,try this:
<?php define('DB_SERVER', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'front');
$conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
if (!$conn){ die('Failed to connect to server: ' . mysql_error());
exit;}
$submit=$_GET['str'];
$stmt = $conn->prepare("SELECT pageurl FROM searchengine WHERE pagecontent='%$submit%'");
$stmt->bind_param("i", $submit;
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($pageurl );
$stmt->fetch();
header("Content-Type: image/jpeg");
echo $pageurl ;?>
Remember: I replaced *
from what are you searching as pageurl
in your post.
Even,not work post your sql and form.
Upvotes: 1