I have no idea why that code doesn't work...
no error message nothing...
thanks
<?php require_once('sqlscript.php'); ?>
<?php
$link = mysql_connect('mysql', 'user', 'pass');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db(database);
$sql = 'SELECT * FROM `videos` ORDER BY creation_date desc LIMIT 3';$result=mysql_query($sql);
$row = mysql_fetch_array($result);
while($row = mysql_fetch_array($result))
{
echo '<video width="320" height="240" controls> <source src="/upload/'.$row['path'].'"> Your browser does not support the video tag. </video>
<br />';
}
mysql_close();
?>
The column creation_date is not exist in the table videos that will result a syntax error that could be not appearing in the page because you turned off the error displaying
you have another note here but not related to your issue you called mysql_fetch_array($result); before calling it in the loop and that will result loosing the first row in your results of query
Is there anything in your database tables?
If there isn't, you would be able to run the script, and it would run without any output or errors.
The mysql query wouldn't fail, and the while loop would never run.
Try putting something like this that checks to see if you get any data returned from your query.
var_dump($result);
And depending on the output now, as it will print something, even if there isn't any data in your table, people can have an easier time finding problems.
Remove 2 times fetch_array, use instead fetch_assoc and stop using mysql_* since its deprecated.
Creation_date doesn't exist.
Try this:
<?php require_once('sqlscript.php'); ?>
<?php
$link = mysql_connect('mysql', 'user', 'pass');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db(database);
$sql = 'SELECT * FROM `videos` ORDER BY id desc LIMIT 3';
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo '<video width="320" height="240" controls> <source src="/upload/'.$row['path'].'"> Your browser does not support the video tag. </video>
<br />';
}
mysql_close();
?>
Related
I am new to PHP, I want to get image information from database when I click on a specific image.
This is the code I'm using, could you help me out?
Code:
<?php
$connection = mysql_connect("localhost","root","");
$select_db = mysql_select_db("fashion",$connection);
$winter = mysql_query("Select * from winter",$connection);
while($row = mysql_fetch_array($winter))
{
echo "<img src=\"winter images/" . $row['image_name']. "\" width=\"200\" //height=\"293\"/>";
}
?>
Did you try echo $row['image_name']; to see whether you are getting the image name or not. If you are getting it properly then try this
while($row = mysql_fetch_array($winter))
{
$imagePath='winter/'.$row['image_name'];
echo '<img src="'.$imagePath.'" width="200" height="293" >';
}
1. Why are you messing up so much with the image tag in your echo statement. if you put double quotes inside the single quotes then also it will work.
2. **Please stop using *mysql_ as it was deprecated before it was deprecated.
Since mysql_fetch_array only pulls regular array, replace it with mysql_fetch_assoc. This should work !!
<?php
$connection = mysql_connect("localhost","root","");
$select_db = mysql_select_db("fashion",$connection);
$winter = mysql_query("Select * from winter",$connection);
while($row = mysql_fetch_assoc($winter)){
echo '<img src= "folder/'.$row["image_name"].'" width="200" height="293">';
}
?>
Since mysql is deprecated, try using Mysqli or PDO
I am building a website that will consist of only images on a single page. There are 300 Images on this page. The home page. Each one of these images is a link and can be clicked. I have a database and this database contains a table where the image URL and the link/target URL exists for each image. There are 2,000 image in the database and only 300 images positions on the page.
Using PHP/MySQL how can I make it so that every time the page is loaded/refreshed new images are shown. So that it randomly (however each image needs to be different relative to each refresh. No duplicates on page) grabs 300 images out of the database to be displayed on the page. Kinda like a rotating advert would work. Each time the page is loaded a new advertisement is shown.
It doesn't have to show 300 different images each time the page is refreshed It just can't show duplicates at anyone time on the page.
Looking for a quick solution, ideas, or links to tutorials that I can follow. Thanks!
EDIT:
Here is the code I currently have so far..
<?php
// Connects to the database.
$connection = mysql_connect("localhost", "root", "");
if (!$connection) {
die("Connection Failed: " . mysql_error());
}
// Selects a database from the previously opened connection.
$select = mysql_select_db("affiliate_links", $connection);
if (!$select) {
die("Connection Failed: " . mysql_error());
}
// Performs query to table 'affiliates' in previously opened database.
$result = mysql_query("SELECT * FROM affiliates ORDER BY RAND() limit 1", $connection);
if (!$result) {
die("MySQL Query/Connection Failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
}
?>
** The following code is where I want each new image to appear on the page.
<img src="<?php echo $row['thumb_url'] ?>">
<img src="<?php echo $row['thumb_url'] ?>">
<img src="<?php echo $row['thumb_url'] ?>">
<img src="<?php echo $row['thumb_url'] ?>">
<img src="<?php echo $row['thumb_url'] ?>">
<img src="<?php echo $row['thumb_url'] ?>">
As is, the code above(the echo $row statements) does not work. The only way I have been able to pull the data from the data base is by putting the echo statements right under the 'while loop' like this.
while ($row = mysql_fetch_array($result)) {
echo $row['target_url']." ".$row['thumb_url']."<br />";
If I keep ORDER BY RAND() limit 1 in the code it will only show me 1 iteration. 1 row of the table. If I remove it I see all the links/image links where ever I echo it out right under the while loop.
So basically what I need now: Be able to echo out the $row OUTSIDE of the while loop and it be displayed inside the html to complete the links. And I need it to beable to display a new image during each iteration of the loop. OR if there is a better way to do it than a while loop. Thanks!
How about storing all the results from the database in an array that you can use anywhere you want. I also changed the SQL query to limit the results to 300 and select only the required columns.
<?php
// Connects to the database.
$connection = mysql_connect("localhost", "root", "");
if (!$connection) {
die("Connection Failed: " . mysql_error());
}
// Selects a database from the previously opened connection.
$select = mysql_select_db("affiliate_links", $connection);
if (!$select) {
die("Connection Failed: " . mysql_error());
}
// Performs query to table 'affiliates' in previously opened database.
$result = mysql_query("SELECT target_url, thumb_url FROM affiliates ORDER BY RAND() limit 300", $connection);
if (!$result) {
die("MySQL Query/Connection Failed: " . mysql_error());
}
$images_array = array();
while ($row = mysql_fetch_array($result)) {
$target_url = $row['target_url'];
$thumb_url = $row['thumb_url '];
// you might consider changing the next line to include a class on the images for formatting purposes
$image_link = '<img src = "' . $thumb_url . '">';
$images_array[] = $image_link;
}
// use this wherever you want to display your images
foreach ($images_array as $current){
echo $current . '<br>';
}
?>
EDIT - to add banner text in the middle change the loop to
$counter = 0;
while ($counter < 150){
echo $images_array[$counter];
$counter++;
}
// put banner text here
while ($counter < 300){
echo $images_array[$counter];
$counter++;
}
i have create an array to diusplay a random file name from my mysql database. unfortunatly it doesnt show correctly.
i need the explode to work based on file id to show the correct banner picturse for that tv series.
<?php include '../connect/dbseries.php' ?>
<?php include 'Sbarray.php' ?>
<?php
$names = explode ("|", $row['4']);
?>
<center><?php
while($row=mysql_fetch_array($result2)){
echo '<img src="../images/series/'. $names[array_rand($names,1)].'" width="800" height="150" style="padding:2px;">';
}
?>
</center>
my array page is
<?php $result2 = mysql_query("SELECT
ID,
pretty_name,
sortname,
Genre,
Bannerfilenames,
CurrentBannerFilename,
Posterfilenames,
PosterBannerFileName,
summary,
Fanart,
IMDB_ID
FROM online_series
order by sortname ASC;");
if (!$result2) {
echo 'Could not run query: ' . mysql_error();
exit;
}
?>
<?php $row = mysql_fetch_row($result2);
//setup array
$banner = $row['4'];
?>
that is all the code i have on the page. any help would be appreciated as it only shows images from row 1 instead of for each row/ tv series
i asume it has something to do with the explode command but cant figure out how to correct it.
thanks in advance
you can check with print_r($names) whether it works. If I understand your problem correctly, you want a random pic of each row. Now you do the variable names before you iterating through the results - so it uses always the first:
<?php $result2 = mysql_query("SELECT
ID,
pretty_name,
sortname,
Genre,
Bannerfilenames,
CurrentBannerFilename,
Posterfilenames,
PosterBannerFileName,
summary,
Fanart,
IMDB_ID
FROM online_series
order by sortname ASC;");
if (!$result2) {
echo 'Could not run query: ' . mysql_error();
exit;
}
?>
<center><?php
while($row=mysql_fetch_assoc($result2)){
$names = explode ("|", $row['Bannerfilenames']);
//for check whether explode works
print_r($names);
echo '<img src="../images/series/'. $names[array_rand($names,1)].'" width="800" height="150" style="padding:2px;">';
}
?>
I'm a PHP beginner and lately I've been having a problem with my source code.
Here it is:
<html>
<head>
<title>
Bot
</title>
<link type="text/css" rel="stylesheet" href="main.css" />
</head>
<body>
<form action="bot.php "method="post">
<lable>You:<input type="text" name="intrebare"></lable>
<input type="submit" name="introdu" value="Send">
</form>
</body>
</html>
<?php
//error_reporting(E_ALL & ~E_NOTICE);
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("robo") or die(mysql_error());
$intrebare=$_POST['intrebare'];
$query = "SELECT * FROM dialog where intrebare like '%$intrebare%'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
?>
<div id="history">
<?php
foreach($row as $rows){
echo "<b>The robot says: </b><br />";
echo $row['raspuns'];
}
?>
</div>
It returns the result 6x times.
This problem appeared when I've made that foreach because I wanted the results to stuck on the page one by one after every SQL query.
Can you please tell me what seems to be the problem? Thanks!
You are doing it wrong. ;-)
First of all you have to fetch your result with mysql_fetch_array in a loop like this:
while (true == ($row = mysql_fetch_array($result))) {
echo "<b>The robot says: </b><br />";
echo $row['raspuns'];
}
Second I want to tell you that all mysql_* functions are marked as deprecated. If you want to learn PHP you should try to learn how to connect to mysql using PDO.
mysql_fetch_array fetches one row per call. You'll want to do like this:
while ($row = mysql_fetch_array($result)) {
echo "<b>The robot says:</b><br>";
echo htmlentities($row['raspuns']);
}
and get rid of that first mysql_fetch_array.
(Notice that i am HTML-escaping the variable output. Unless you know what you're doing, you should not output raw data into a page.)
By the way, mysql_query is effectively deprecated. It is not at all recommended for new code. Take a look at mysqli (the replacement) or PDO (the new hotness). With the new mysqli (objecty) interface, the PHP part would look a bit like this:
<?php
//error_reporting(E_ALL & ~E_NOTICE);
$db = new mysqli('localhost', 'root', '', 'robo');
# turn into a wildcard
$intrebare='%' . $_POST['intrebare'] . '%';
$stmt = $db->prepare('SELECT * FROM dialog WHERE intrebare LIKE ?');
$stmt->bind_param('s', $intrebare);
$result = $stmt->execute();
echo '<div id="history">';
# 5.4 lets you do it like this;
# older versions, try `while ($row = $result->fetch_assoc())`
foreach ($result as $row) {
echo "<b>The robot says: </b><br />";
echo htmlentities($row['raspuns']);
}
?>
You're only getting one result (only one call to mysql_fetch_array()). There are six columns, I bet, in dialog.
...
$result = mysql_query($query) or die(mysql_error());
?>
<div id="history">
<?php
while($row = mysql_fetch_array($result))
{
echo "<b>The robot says: </b><br />";
echo htmlentities($row['raspuns']);
}
?>
</div>
Also, mysql_* functions are being deprecated. Switch to mysqli_* or PDO.
Use while to fetch all the data and check variable names
while($row = mysql_fetch_array($result)){
echo "<b>The robot says: </b><br />";
echo $row['raspuns']; // Here
}
You are trying it reversed way:
<?php
while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
echo '<strong>The robot says: </strong><br />', $row['raspuns'];
}
?>
Try now :)
Hey all I have a table caleld Box1, with a simple structure of: id(increment,primary) imageurl, link.
What I am looking to do, is on my site in the HTML file, display the imageurl into a imgsrc, so that the Image URL loads the image when going to the site
I cannot find specifically what I am looking for online and know I must be close somewhere, this is what I have so far in my coding for my .php file.... thanks.
<?php
$con = mysql_connect("localhost","data","data");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}else {
mysql_select_db("database1", $con);
$result = mysql_query("SELECT * FROM Box1 ORDER BY id DESC LIMIT 1");
while($row = mysql_fetch_array($result))
{
echo $row['image'];
// NEXT STEP IS TO DISPLAY THE the IMAGE EXAMPLE: '<img src code<?php echo 'image'; ?>' >
}
}
mysql_close($con)
?>
if you could guide me to an exmaple of how to do this, that would be amazing, im so confused how one would implement HTML with PHP, thanks!!
<?php
... connect to DB ...
$sql = "SELECT imgurl FROM Box1 WHERE id=XXX";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
?>
<img src="<?php echo $row['imgurl'] ?>" />
Also, it is helpful to become familiar with this operator for large chunks of html
echo <<<YOUR_TOKEN_NAME
<img src="$row['imgurl']" />
...misc html and php vars intermixed
YOUR_TOKEN_NAME;