Okay i`m loading images from a MySql Database server.. but when i click on it, it opens in a new page not in the lightbox popup.
// Make the connect to MySQL or die
// and display an error.
$db = mysql_connect($host, $username, $password);
if (!$db) {
die('Could not connect: ' . mysql_error());
}
// Select your database
mysql_select_db ($database);
$categorie = $_GET['categorie'];
echo "<h3>" . $categorie . "</h3><br><br>";
$query = "SELECT * FROM afbeelding WHERE categorie = '" . $categorie . "'";
$resultaat = mysql_query($query,$db) or die(mysql_error());
echo "<table>";
while ($rij = mysql_fetch_array($resultaat)){
echo "<img src=" . $rij["afbeelding"] . " width=\"150px\" height=\"150px\" class='foto' />";
}
echo "</table>";
?>
Please some help
If you're loading you images after the page is loaded, you might want to call the function that initiates the lightbox after the images arrive.
You need to set correct CSS class for your <A>, otherwise lightbox JS will not take care of it.
Related
I am trying to display an image uploaded to my "upload" table in MySql. I've been reading a lot on how to do this but no luck.
"news_content" table is for uploading NEWS content to Mysql and has 6 columns: id,title, description, content_text, date, time
and "upload" table has 5 columns: id, name, size, image, date
In "news_content" table I upload the date and the time columns separately but the date column in "upload" table is a string concatenated with both date and time. For example if in "news_content" table the date is 2/3/2016 and the time is 5:30, in "upload" table the date is going to be 2/3/20165:30. I organized it that way in order to retrieve the image by its specific date and time that the related post uploaded.
I upload the image in news.php page with this following code:
news.php :
// Create connection
$connection = mysql_connect("localhost","root","p206405az");
// Check connection
if (!$connection) {
die("Connection failed: " . mysql_error());
}
//select a database to use
$db_select = mysql_select_db( "news" , $connection) ;
if (!$db_select) { die("Selection faild:" . mysql_error()) ;
}
//uploading the content of news and date and time
if(isset($_POST["submit"])){
$title = $_POST["title"] ;
$description = $_POST["description"] ;
$content_text = $_POST["content_text"] ;
$date = $_POST["date"] ;
$time = $_POST["time"] ;
//perform mysql query
$result = mysql_query("INSERT INTO news_content (title, description, content_text, date, time)
VALUES ('$title', '$description', '$content_text' ,'$date' , '$time' )") ;
if (!$result) {
die("Insert failed: " . mysql_error());
$submitMessage = "Problem with updating the post, please try again" ;
} else if ($result){
$submitMessage = "Your post succsessfully updated" ;
}
}
// uploading the image
if(isset($_POST['submit']) && $_FILES['image']['size'] > 0)
{
$fileName = $_FILES['image']['name'];
$tmpName = $_FILES['image']['tmp_name'];
$fileSize = $_FILES['image']['size'];
$fileType = $_FILES['image']['type'];
$filedate = "$date" . "$time" ;
$fp = fopen($tmpName, 'r');
$content2 = fread($fp, filesize($tmpName));
$content3 = addslashes($content2);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$query = "INSERT INTO upload (name, size, type, image, date ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content3', '$filedate')";
mysql_query($query) or die('Error2, query failed');
}
And I want to retrieve that image by getImage.php page to use it as source page later by this following code but it seems it can't retrieve the blob data:
P.S. The image is successfully uploaded but I can't retrieve it with specific date that I Posted lately
getImage.php :
// Create connection
$connection = mysql_connect("localhost","root","p206405az");
// Check connection
if (!$connection) {
die("Connection failed: " . mysql_error());
}
//select a database to use
$db_select = mysql_select_db( "news" , $connection) ;
if (!$db_select) { die("Selection faild:" . mysql_error()) ;
}
//perform mysql query
$result = mysql_query("SELECT * FROM news_content" , $connection);
if (!$result) {
die("read failed: " . mysql_error());
}
//useing returned data
while ($content = mysql_fetch_array($result)){
$date = $content["date"];
$time = $content["time"] ;
}
$filedate = "$date" . "$time" ;
$result2 = mysql_query("SELECT image FROM upload WHERE date='$filedate'" , $connection);
if (!$result2) {
die("read failed: " . mysql_error());
};
$row = mysql_fetch_assoc($result2);
mysql_close($connection);
header("Content-type: image/jpeg");
echo $row['image'];
And I want to display the image in index.php page with this following HTML code:
index.php :
<img src="getImage.php?date=<?php echo $filedate ; ?>" width="175" height="200" />
How can I retrieve that data in getImage.php page and then use that page az source page to display the image in index.php page?
Any help would be appreciated.
cant figure out the error unless php throws an error. but i would save the images as files instead of db and keep its name if news table's row, unless there are multiple images per news.
in case of multimple image per news, i would just match the image id to news id.
schema for news.db
id, title, description, content_text, date, time
schema for upload.db:
id, image, newsid
my image html link would have been:
<img src="getImage.php?id=<?php echo $newsid; ?>" width="175" height="200" />
and then my getimage.php would have been like this:
$connection = mysql_connect("localhost","root","p206405az");
if (!$connection) {
die("Connection failed: " . mysql_error());
}
$db_select = mysql_select_db( "images" , $connection) ;
if (!$db_select) {
die("Selection faild:" . mysql_error()) ;
}
$id=$_GET["id"];
$result = mysql_query("SELECT image FROM upload WHERE newsid='$id' LIMIT 1" , $connection);
if (!$result) {
die("read failed: " . mysql_error());
};
$row = mysql_fetch_assoc($result);
mysql_close($connection);
header("Content-type: image/jpeg");
print $row['image'];
To show errors:
add error_reporting(E_ALL); ini_set('display_errors', '1'); at top of your page;
temporarily comment the header line: header("Content-type: image/jpeg");;
retrieve image directly from browser address bar (see image below);
If you obtain a “500 Server Error” check your Apache error log (compile errors are not displayed even with error_reporting enabled);
I am writing a php file that takes values from a form and posts them to a mysql database. One of the table fields is a button link to a video that will play when clicked. It works great if I go into the database and manually add the link. However my PHP insert causes an error. Please have a look at this code:
$fileName = "video_".$id.".html";
$link = "<button class=\"count\">Watch Video</button>";
$con=mysqli_connect("localhost","videomanager","password","my_database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO video_list (date, title, description, link) VALUES('$_POST[date]','".mysqli_real_escape_string($_POST['sermon'])."','".mysqli_real_escape_string($_POST['description'])."','$link' )";
if (!mysqli_query($sql,$con))
{
die('Error: ' . mysqli_error());
}
echo "This video has been successfully added to the video database.";
mysqli_close($con);
If I look at $link by doing something like: echo $link; die(); it produces a page with the button and the code in the button looks good. Is it how I am trying to insert it? Thanks for your help!
you have a lot of errors
<?php
$fileName = "video_".$id.".html";
$link = "<button class=\"count\">Watch Video</button>";
$con = mysqli_connect("localhost","videomanager","password","my_database");
// Check connection
if ( mysqli_connect_errno() )
die('Failed to connect to MySQL: ' . mysqli_connect_error() );
// Check param is set
if( !isset($_POST['date'], $_POST['sermon'], $_POST['description']) )
die('Param Error');
// SQL Request
$sql = sprintf("INSERT INTO video_list (date, title, description, link) VALUES('%s','%s','%s','%s')",
mysqli_real_escape_string($con, $_POST['date']),
mysqli_real_escape_string($con, $_POST['sermon']),
mysqli_real_escape_string($con, $_POST['description']),
mysqli_real_escape_string($con, $link)
);
// SQL execute
$result = mysqli_query($con, $sql) or die('Error: ' . mysqli_error($con));
// Free result
mysqli_free_result($result);
// Close connection
mysqli_close($con);
echo "This video has been successfully added to the Kim Watt videos.";
I am trying to store images in mySQL database and then displaying on the other page. I have this function to store images in mySQL.
function upload() {
include "databaseConnection.php";
$maxsize = 10000000; //set to approx 10 MB
//check associated error code
if ($_FILES['userfile']['error'] == UPLOAD_ERR_OK) {
//check whether file is uploaded with HTTP POST
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
//checks size of uploaded image on server side
if ($_FILES['userfile']['size'] < $maxsize) {
// prepare the image for insertion
$imgData = addslashes(file_get_contents($_FILES['userfile']['tmp_name']));
// put the image in the db...
// database connection
mysql_connect($host, $user, $pass) OR DIE(mysql_error());
// select the db
mysql_select_db($db) OR DIE("Unable to select db" . mysql_error());
// our sql query
$sql = "INSERT INTO carsinfo
(carName, carPicture)
VALUES
('{$_FILES['userfile']['name']}', '{$imgData}');";
// insert the image
mysql_query($sql) or die("Error in Query: " . mysql_error());
$msg = '<p>Image successfully saved in database with id =' . mysql_insert_id() . ' </p>';
} else {
// if the file is not less than the maximum allowed, print an error
$msg = '<div>File exceeds the Maximum File limit</div>
<div>Maximum File limit is ' . $maxsize . ' bytes</div>
<div>File ' . $_FILES['userfile']['name'] . ' is ' . $_FILES['userfile']['size'] .
' bytes</div><hr />';
}
}
else
$msg = "File not uploaded successfully.";
}
else {
$msg = file_upload_error_message($_FILES['userfile']['error']);
}
return $msg;
}
And this code to show iamges.
<?php
include "databaseConnection.php";
// just so we know it is broken
error_reporting(E_ALL);
// some basic sanity checks
//connect to the db
$link = mysql_connect("$host", "$user", "$pass") or die("Could not connect: " . mysql_error());
// select our database
mysql_select_db("$db") or die(mysql_error());
// get the image from the db
$sql = "SELECT carPicture FROM carsinfo;";
// the result of the query
$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
$row = mysql_fetch_assoc($result);
// set the header for the image
echo $row['carPicture'];
header("Content-type: image/jpeg");
// close the db link
mysql_close($link);
?>
When this code is run, nothing is shown on the page, even if I write some HTML inside this code, empty page is shown.
I assume you are saving the image content as blob and if your sql is returning the correct data then you can display as
header("Content-type: image/jpeg");
echo $row['carPicture'];
You need to add the header first before the image.
or
echo '<img src="data:image/jpeg;base64,' . base64_encode($row['carPicture']) . '">';
do not write
include databaseConnection.php
nothing include anything.
I'm trying to conntect to a mysql database for the first time. Can you see what's not working correctly below?
I get an error on line 3.
What would my server address look like from Godaddy to my database? I found that address in my control panel.
Thanks for any help. Never programmed using PHP before.
<body>
<?php
$con = mysql_connect("MydbName.db.3924516.hostedresource.com ","Userid","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Gallerys");
echo "<table border='1'>
<tr>
<th>Thumb Url </th>
<th>Gallery Url</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['THUMBURL'] . "</td>";
echo "<td>" . $row['GALLERYURL'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
Try check back the authenication settings, you might use the wrong host, username or password.
<?php
$con = mysql_connect("usually it is localhost","your MYSQL username","your MYSQL password");
// Checking the login details.
// Example of default xampp login details: $con = mysql_connect("localhost","root","");
// Xampp MYSQL default does not have password.
if (!$con) // If the login details are wrong, it will should an error.
{
die('Could not connect: ' . mysql_error());
}
?>
Without the exact error it is hard to be sure but on line 3 you have an extra space after hostedresource.com. Try removing the space between the end of the hostname and the quatation mark. Like so:
$con = mysql_connect("MydbName.db.3924516.hostedresource.com","Userid","password");
I'm having problems with my PHP file that generates XML from MySQL database. The code is below
<?php
require("decibelone_dbinfo.php");
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// Opens a connection to a MySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM location WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker>';
echo '<name>' . parseToXML($row['name']) . '</name>';
echo '<address>' . parseToXML($row['address']) . '</address>';
echo '<latitude>' . $row['latitude'] . '</latitude>';
echo '<longitude>' . $row['longitude'] . '</longitude>';
echo '<description>' . $row['description'] . '</description>';
echo '<time>' . $row['time'] . '</time>';
echo '</marker>';
}
// End XML file
echo '</markers>';
?>
I'm not sure what the problem is, but I have changed all my character encodings (in mysql and under htaccess) to UTF8. The problem surfaced recently after a server maintenance by my host, and I did not change my file before and after it, so I doubt that it could be a problem with my code itself. Can anyone point me in the right direction? Thanks in advance!
You can use DomDocument class. I think what you are using is an old way. Please loo at following link.
http://php.net/manual/en/class.domdocument.php