I have setup an image table in my database to store my images as blob type. My problem is that i do not know how to display the images in the db from my web search page. Anytime i enter a searh query, it will display the keyword & the image name but it will not display the image itself. rather it displays long sql codes.
Here are my php codes for Imagesearch.php;
<style type="text/css">
body {
background-color: #FFF;
}
</style>
<?php
//get data
$button = $_GET['submit'];
$search = $_GET['search'];
$x = "";
$construct = "";
if (!$button){
echo "You didint submit a keyword.";
}
else{
if (strlen($search)<=2) {
echo "Search term too short.";
}
else {
echo "You searched for <b>$search</b><hr size='1'>";
//connect to database
mysql_connect("localhost","root","");
mysql_select_db("searchengine");
//explode our search term
$search_exploded = explode(" ",$search);
foreach($search_exploded as $search_each) {
//constuct query
$x++;
if ($x==1) {
$construct .= "keywords LIKE '%$search_each%'";
}
else {
$construct .= " OR keywords LIKE '%$search_each%'";
}
}
//echo out construct
$construct = "SELECT * FROM images WHERE $construct";
$run = mysql_query($construct) or die(mysql_error());
$foundnum = mysql_num_rows($run);
if ($foundnum==0) {
echo "No results found.";
}
else {
echo "$foundnum results found!<p>";
while ($runrows = mysql_fetch_assoc($run)) {
//get data
$name = $runrows['name'];
$image = $runrows['image'];
echo "
<b>$name</b><br>
$image<br>
";
}
}
}
}
You should have used Google... Link
You need a separate php script that will return the image itself, and then you will need to call that from your PHP script.
The other PHP script needs to set the Content-type header to the proper mime type.
You should make another script, at another URL, that accepts an ID through a get parameter to output the image. Could then do something along the lines of:
<?php
// ..your MySQL stuff
function error() {
echo "There was an error";
}
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$id = (int) $_GET['id'];
$sql = "SELECT * FROM images WHERE id = '$id'";
$query = mysql_query($sql, $conn);
if (mysql_num_rows() == 1) {
$data = mysql_fetch_assoc($query);
// Change this to the correct image type for your stored data
header("Content-type: image/gif");
echo $data['image'];
} else {
error();
}
} else {
error();
}
You'd then echo:
echo "<b>$name</b> <img src='theimagescript.php?id={$id}' alt='Image of $name' />";
Related
I have the codes to search autocomplete from MySQL and the match query can be clicked and direct me to a new page. How can i have the result of the clicked queries to be displayed in the new page w/out using any URL from database because I need to avoid using lots of HTML files. Thank you.
<p id="searchresults">
<?php
// PHP5 Implementation - uses MySQLi.
// mysqli('localhost', 'yourUserbookTitle', 'yourPassword', 'yourDatabase');
$db = new mysqli('localhost', 'root', '', 'book');
if(!$db) {
// Show error if we cannot connect.
echo 'ERROR: Could not connect to the database.';
} else {
// Is there a posted query string?
if(isset($_POST['queryString'])) {
$queryString = $db->real_escape_string($_POST['queryString']);
// Is the string length greater than 0?
if(strlen($queryString) >0) {
$query = $db->query("SELECT * FROM bookinfo WHERE bookTitle LIKE '%" . $queryString . "%'");
if($query) {
// While there are results loop through them - fetching an Object.
// Store the category id
$bookTitle = 0;
while ($result = $query ->fetch_object()) {
if($result->bookTitle != $bookTitle) { // check if the category changed
echo '<span class="category">'.$result->bookTitle.'</span>';
$bookTitle = $result->bookTitle;
}
echo '<a href="'.$result->url.'">';
echo '<img src="search_images/'.$result->bookimage.'" alt="" />';
$bookTitle = $result->bookTitle;
if(strlen($bookTitle) > 35) {
$bookTitle = substr($bookTitle, 0, 35) . "...";
}
echo '<span class="searchheading">'.$bookTitle.'</span>';
$author = $result->author;
if(strlen($author) > 80) {
$author = substr($author, 0, 80) . "...";
}
echo '<span>'.$author.'</span></a>';
}
echo '<span class="seperator">Nothing interesting here? Try the sitemap.</span><br class="break" />';
} else {
echo 'ERROR: There was a problem with the query.';
}
} else {
// Dont do anything.
} // There is a queryString.
} else {
echo 'There should be no direct access to this script!';
}
}
?>
</p>
I don't know if I get your problem right, but if you need to display a new page with the selected book data/review/comments, I would add :
echo '<span>'.$author.'</span></a>'; /* after this line */
echo '<span>Read more > click here</span>'; /* where $bookID id the ID column in your DB */
Then, on the new page, retrieve data from DB according to $bookID value and display it...
I am going out of my mind as it is. I am trying to pass a variable from a page that shows all albums thumbnail image and name to a page that will display all the pictures in that gallery using that passed variable, but the variable is empty in the url on the target page. I have seen similar cases on the web and on this site and I've applied the suggestions but it's still the same. Here is the code that lists the thumbnail and passes the variable(id).
<?php
include ("config.php");
$conn = mysqli_connect(DB_DSN,DB_USERNAME,DB_PASSWORD,dbname);
$albums = mysqli_query($conn,"SELECT * FROM albums");
if (mysqli_num_rows($albums) == 0) {
echo "You have no album to display. Please upload an album using the form above to get started. ";
}
else{
echo "Albums created so far:<br><br>";
echo "<table rows = '4'><tr>";
while ($thumb = mysqli_fetch_array($albums)) {
echo '<td><a href ="view.php?id="'.$thumb['id'].'"/><img src = "'.$thumb['thumbnail'].'"/><br>'.$thumb['album_name'].'<br>'.$thumb['id'].'</a></td>';
}
echo "</tr></table>";
}
?>
The code for getting the passed variable is as follows:
<?php
include("config.php");
$conn = mysqli_connect(DB_DSN,DB_USERNAME,DB_PASSWORD);
$db = mysqli_select_db($conn,dbname);
if (isset($_GET['id'])) {
$album_id = $_GET['id'];
$pic = "SELECT * FROM photos WHERE album_id ='$album_id'";
$picQuery = mysqli_query($conn,$pic);
if (!$picQuery) {
exit();
}
if (mysqli_num_rows($picQuery) == 0) {
echo "Sorry, no Pictures to display for this album";
}
else{
echo "Pictures in the gallery:<br><br>";
while ($result = mysqli_fetch_assoc($picQuery)) {
echo "<img src='".$result['photo_path']."'/>";
}
}
}
?>
Please help as i have spent the last two days trying to get it right.
First, your's code is weak against sql injections:
$album_id = $_GET['id']; // here
$pic = "SELECT * FROM photos WHERE album_id ='$album_id'";
Use either $album_id = intval($_GET['id']) or prepared statements functionality.
Second, add debug lines to your code, like:
<?php
include("config.php");
if (isset($_GET['id'])) {
$album_id = intval($_GET['id']);
var_dump($album_id); // should print actual passed id
$conn = mysqli_connect(DB_DSN,DB_USERNAME,DB_PASSWORD);
var_dump($conn _id); // should print conn resource value
$db = mysqli_select_db($conn, dbname);
var_dump($db); // should print 'true' if db select is ok
$pic = "SELECT * FROM photos WHERE album_id ='$album_id'";
$picQuery = mysqli_query($conn, $pic);
var_dump($picQuery); // should print query resource value
if (!$picQuery) {
exit();
}
if (mysqli_num_rows($picQuery) == 0) {
echo "Sorry, no Pictures to display for this album";
} else {
echo "Pictures in the gallery:<br><br>";
while (($result = mysqli_fetch_assoc($picQuery)) !== false) {
var_dump($result ); // should print fetched assoc array
echo "<img src='".$result['photo_path']."'/>";
}
}
}
Notice $album_id = intval($_GET['id']) and while (($result = mysqli_fetch_assoc($picQuery)) !== false) parts
Then follow link view.php?id=<existing-album-id> and observe debug result. On which step debug output differs from expected - there problem is.
The search function works like a charm, but I'm not sure how to add certain "extras" to it. When there are results to display, I would also like it to say:
"Your search returned x results."
Followed by the results.
When there are no results to display, I would like it to say:
"Your search returned no results. Please try again."
And when the user does not input anything into the search form, I would like it to say:
"You did not enter a search term."
I'm a PHP beginner, and I'm not sure how to implement this into my current code; I've tried a bunch of different ways, and it either gives me errors or returns a blank page when there are no results.
Any direction or help would be great. Thank you.
Here is my current code:
<?php
//STEP 1 Connect To Database
$connect = mysql_connect("localhost","tarb89_admin","leccums");
if (!$connect)
{
die("MySQL could not connect!");
}
$DB = mysql_select_db('tarb89_characters');
if(!$DB)
{
die("MySQL could not select Database!");
}
//STEP 2 Check Valid Information
if(isset($_GET['search']))
{
//STEP 3 Declair Variables
$Search = $_GET['search'];
$result = mysql_query("SELECT * FROM characters WHERE name LIKE '%$Search%' ");
while($row = mysql_fetch_assoc($result))
{
$name = $row['name'];
$id = $row['id'];
$breed = $row['breed'];
$gender = $row['gender'];
$genetics = $row['genetics'];
$profile = $row['profile'];
$player = $row['player'];
$color = $row['color'];
$markings = $row['markings'];
$traits = $row['traits'];
$defects = $row['defects'];
$extras = $row['extras'];
echo " <h3>$name</h3>
<table width='700px' cellpadding='5' cellspacing='0'>
<tr>
<td><p>
<b>ID Number: </b>$id<br>
<b>Breed: </b>$breed<br>
<b>Gender: </b>$gender<br>
<b>Genetics: </b>$genetics<br>
<b>Profile:</b> <a href='$profile'>$profile</a><br>
<b>Player:</b> $player</p></td>
<td><p>
<b>Color:</b> $color<br>
<b>Markings:</b> $markings<br>
<b>Traits:</b> $traits<br>
<b>Defects:</b> $defects<br>
<b>Extras:</b> $extras</p></td>
</table>";
}
}
?>
Use mysql_num_rows() to return number of rows
if($search!="") {
// Your Query
$num = mysql_num_rows($result);
if($num >= 1)
{
echo "Your search returned $num results";
// your code
}
else
{
echo "Your search returned no results. Please try again";
}
} else {
echo "You did not enter a search term";
}
YOUR CODE
if(isset($_GET['search']))
{
//STEP 3 Declair Variables
$Search = $_GET['search'];
$result = mysql_query("SELECT * FROM characters WHERE name LIKE '%$Search%' ");
$num = mysql_num_rows($result);
if($num >= 1)
{
echo "Your search returned $num results";
while($row = mysql_fetch_assoc($result))
{
$name = $row['name'];
$id = $row['id'];
$breed = $row['breed'];
$gender = $row['gender'];
$genetics = $row['genetics'];
$profile = $row['profile'];
$player = $row['player'];
$color = $row['color'];
$markings = $row['markings'];
$traits = $row['traits'];
$defects = $row['defects'];
$extras = $row['extras'];
echo " <h3>$name</h3>
<table width='700px' cellpadding='5' cellspacing='0'>
<tr>
<td><p>
<b>ID Number: </b>$id<br>
<b>Breed: </b>$breed<br>
<b>Gender: </b>$gender<br>
<b>Genetics: </b>$genetics<br>
<b>Profile:</b> <a href='$profile'>$profile</a><br>
<b>Player:</b> $player</p></td>
<td><p>
<b>Color:</b> $color<br>
<b>Markings:</b> $markings<br>
<b>Traits:</b> $traits<br>
<b>Defects:</b> $defects<br>
<b>Extras:</b> $extras</p></td>
</table>";
}
}
else
{
echo "Your search returned no results. Please try again";
}
} else {
echo "You did not enter a search term";
}
Note: mysql_* functions are deprecated, Move on mysqli_* functions asap
im having a simple mysql/php problem. so i am adding in Image titles for my website, and the code is displayed below. It works, but when you dont put a image, it shows up as blank. I need it to show up as 'No image title' (bc i will use this for image description to). It basically gets the image name, then takes the title from that row.
So how do i do it? :/ im still very new to PHP.
<?php
if (isset($imgtitleset))
{
$sql = "SELECT image_title FROM images WHERE image_name = '$image_main'";
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result))
{
$imgtitle= $row["image_title"];
echo "$imgtitle";
}
}
else {
echo 'no image title';
}
?>
Change the while loop like so:
while ($row = mysql_fetch_array($result)) {
$imgtitle= $row["image_title"];
if($imgtitle != '') {
echo $imgtitle;
} else {
echo 'no image title';
}
}
Also, I'm not sure what the $imgtitleset variable is for, but you can probably get rid of the if statement checking to see whether it's set.
Edit: the whole thing should probably look like this:
<?php
$sql = "SELECT image_title FROM images WHERE image_name = '$image_main'";
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result)) {
$imgtitle= $row["image_title"];
if($imgtitle != '') {
echo $imgtitle;
} else {
echo 'no image title';
}
}
?>
This all depends on what $imgtitleset is equal to. It is clearly set against something:
while ($row = mysql_fetch_array($result)) {
$imgtitle = $row["image_title"];
if (isset($imgtitle))
echo "$imgtitle";
else
echo 'no image title';
}
This would mean if nothing was found in the database then it will echo the no image title. However like I said, this could depend on what $imgtitleset is, maybe post the code for that?
If you only expect the select to return a single row, then use if rather than while and return the error on else:
<?php
if (isset($imgtitleset))
{
$sql = "SELECT image_title FROM images WHERE image_name = '$image_main'";
$result = mysql_query ($sql);
if ($row = mysql_fetch_array($result))
{
$imgtitle= $row["image_title"];
echo "$imgtitle";
}
else {
echo 'no image title';
}
}
?>
I need a little help of my brilliant friends.
Actually i m new to development so that i have no much idea how can i show my page in words like www.testsite.com/index.php?pname=**Home** except of www.testsite.com/index.php?pid=**1**
i have the following code for showing page in number
if (!$_GET['pid']) {
$pid = '1';
} else {
$pid = ereg_replace("[^0-9]", "", $_GET['pid']); }
and the sql code
$sqlCommand = "SELECT id, link FROM main_page WHERE showing='1' ORDER BY id ASC";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$menu='';
while ($row = mysqli_fetch_array($query)) {
$pid = $row["id"];
$link = $row["link"];
if ($linklabel){
$menu .=''. $link .'';
}}
i want to show and href name of page not id how can i do that.
help plz
Your example will fail if I enter *1*2*3*
you should be searching for the contents of
**(contents)**
and nothing else.
That will get you the name and the number.
Here is my example
$string = "**123naasdme456**";
preg_match("/[^\*+](?P<val>\w+)[^\*+]/",$string,$matches);
echo $matches[0];
will echo 123naasdme456
and here it is implemented into your code
function getReal($urlVar)
{
if(preg_match("/[^\*+](?P<val>\w+)[^\*+]/",$urlVar,$matches))
{
return $matches[0];
}
return false; // or default value
}
$pid = getReal($_GET['pid']);
$name = getReal($_GET['pname']);
You should add an extra field in your database (table main_page) with the name name or something similar. Then you could:
if (!$_GET['pname']) {
$pid = 'home';
} else {
$pid = mysql_real_escape_string($pid);
$sql = mysql_query("SELECT name FROM main_page WHERE name = '$pid'");
if (mysql_num_rows($sql) == 1))
{
echo "Content";
} else {
echo "404 error. Couldn't find the page you were looking for.";
}
}
URL Rewriting. http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/