Picture references from MySql - php

i am trying to buid a Php site that show data from my MySQl database.
and i think im almost there, everything works except the Pictures.
i cant get my php site to show the pictures with the picture reference from Sql database.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
$con=mysqli_connect("localhost","root","","db1");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<head>
<link rel="stylesheet" href="styles.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>untitled</title>
</head>
<body>
<div class="content">
<?php
$sql = "SELECT id, Producent, Model, kategori FROM tb1";
$result = mysqli_query($con,"SELECT * FROM `tb1");
echo "<table>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>";?> <img scr="<?php echo $row["Billedurl"]; ?>"/> <?php echo " </td>";
echo "<td>" .$row["Producent"] .$row["Model"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<!-- end .content --></div>
<!-- end .container --></div>
<div class="footer"><br>
<!-- end .footer --></div>
</body>
</html>

You need to select all the columns you are going to use, so if you need Billedurl, you should change:
$sql = "SELECT id, Producent, Model, kategori FROM tb1";
to:
$sql = "SELECT id, Producent, Model, kategori, Billedurl FROM tb1";
Now the value of that column will be available in $row["Billedurl"].
Edit: It seems that now you have the correct value in your html, but the path to the image is not correct as it is a relative path.
You should prefix your variable with the correct folder so that the image is found by the browser. That can be as simple as just using an absolute path but that depends on where the pic/ directory is located.
So if your variable contains pic/l_jabra_evolve80.jpg" and the pic/ folder is on the root of the web-server, you can do something like:
# before the loop
$imagePrefix = '/';
# in the loop
... <img scr="<?php echo $imagePrefix . $row["Billedurl"]; ?>"/> ...
Now the browser will try to fetch the image from /pic/l_jabra_evolve80.jpg.

You need to do little bit of debugging:
first change little bit in your query
$sql = "select * FROM tb1";
$result = mysqli_query($con, $sql);
Then try to check print_r($result) and check if you are getting everything here or not.

Related

php while loop runs in shell but not on the browser

I have made a very simple PHP7 page that shows the values of temperature sensors in a table. I extract this data from a MySQL database. The PHP7 is running with apache2 on a raspberrypi 3.
This all works well and I am able to get the code to run on the CLI and it generates HTML just fine. But when I run the page in the browser it only shows what comes before and after the while loop. All the other PHP/HTML shows fine. the file name is tempsense.php
<html>
<head>
<title>
TEMP
</title>
</head>
<body>
HOME
<?php
$con = mysqli_connect("localhost","root","*********");
$db = mysqli_select_db($con,"connected_sensors");
$query = "SELECT * FROM sensors";
$result = mysqli_query($con, $query);
echo "<table border='1'>";
echo "<tr>";
echo "<th>Sensor ID</th><th>Sensor Name</th><th>Temp F</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>", $row['sensor_id'], "</td><td>", $row['sensor_name'], "</td><td>", $row['sensor_value'], "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
The source from the browser is
<html>
<head>
<title>
TEMP
</title>
</head>
<body>
HOME
<table border='1'><tr><th>Sensor ID</th><th>Sensor Name</th><th>Temp F</th></tr></table> </body>
and from the PHP CLI
<html>
<head>
<title>
TEMP
</title>
</head>
<body>
HOME
<table border='1'><tr><th>Sensor ID</th><th>Sensor Name</th><th>Temp F</th></tr><tr><td>28-0517c15db7ff</td><td>Device_0</td><td>79.5866</td></tr><tr><td>28-0117b3e303ff</td><td>Device_1</td><td>79.1366</td></tr></table> </body>
I know this is probably dumb but I'm loosing my mind.
Thanks in advance.

php echo name and file paths from database to display videos with href

My knowledge of php is very limited, although I am aware that mysql functions are depreciated but it doesn't matter for the purpose of this project.
I have a table (enisatquestion) with training questions, and file paths to 14 videos stored on my pc which I want to display using localhost.
My table structure and an example of one of the rows in my table are as follows:
Columns are:
eNISATID (Auto-increment)
eNISATQuestion (Training question)
eNISATVideo (File path to video)
An example of a Row:
1
Can you login?
http://localhost\Tna\eNISAT\LoginTutorial.wmv
Here is my code, I am getting an error;
mysql_fetch_assoc() expects at least 1 parameter, 0 given in C:\wamp\www\Tna\eNISATVids.php on line 20
Can anyone please help me with this, I have researched a lot of ways to display this, but I am struggling with it as I have never worked with videos in php before. The videos are also wmv format. Or can anyone give me a more suitable example
Many Thanks
<?php
session_start();
include_once 'dbconnect.php';
if(!isset($_SESSION['user']))
{
header("Location: index.php");
}
//maintain SESSION user_id
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
//Select video name and question
$query = "SELECT eNISATQuestion, eNISATVideo FROM enisatquestion";
$result = mysql_query($query);
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
$enisatquestion = "<table >";
while ( $row = mysql_fetch_assoc($result) ) {
$enisatquestion .= "<tr><td><a href='{$row['eNISATVideo']}'>{$row['eNISATQuestion']}</a></td></tr>";
}
$enisatquestion .= "</table>";
echo $enisatquestion;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome - <?php echo $userRow['username']; ?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<div id="left">
<label>NHSCT eNISAT Tutorials</label>
</div>
<div id="right">
<div id="content">
Welcome <?php echo $userRow['forename']; ?> Sign Out
</div>
</div>
</div>
</body>
</html>
The answer is simple:
$query = "SELECT eNISATName, eNISATVideo FROM enisatquestion";
$result = mysql_query($query);
$enisatquestion = "<table>";
while ( $r = mysql_fetch_assoc($result) ) {
$enisatquestion .= "<tr><td><a href='{$r['eNISATVideo']}'>{$r['eNISATName']}</a></td></tr>";
}
$enisatquestion .= "</table>";
echo $enisatquestion;
?>
Notice the new $result variable that is passed to mysql_fetch_assoc.

Make search output error if no results are found

I am trying to make an error message appear if when a user searches, no results are found; however I am having an issue getting the error message to display.
Here is my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PHP Test File</title>
<link href="phptestcss.css" rel="stylesheet" type="text/css" />
<link href="lightbox-style/lightbox.css" rel="stylesheet" type="text/css" media="screen"/>
<script src="js/prototype.js" type="text/javascript"></script>
<script src="js/scriptaculous.js?load=effects,builder" type="text/javascript"></script>
<script src="js/lightbox.js" type="text/javascript"></script>
</head>
<body>
<?php include 'header.html'; ?>
<?php
include 'databaseconnect.php';
//- Queries the table to get content from rows -
$QuerySelect = "SELECT thumbnail,image,title FROM homepage WHERE title LIKE '%".mysql_real_escape_string($_POST['search'])."%' ";
//----------------- QUERY the TABLE, store THE CONTENT IN A PHP VARIABLE -----------------
$result = mysql_query($QuerySelect);
//-------------- STORE THE CONTENT WITH AN ID = 1 IN A ARRAY -------------- -------------------
include 'logo.html';
?>
<div class="searchcontainer">
<form method="post" action="index.php">
<input type="text" class="input" name="search" size="40" placeholder="Search...">
<input type="submit" class="button" name="Submit" value="Search" id="Submit" >
</form>
</div>
<?php
$numrows = mysql_num_rows ($result );
if($numrows = 0 and isset($_POST['search'])){
echo '<div class="errorcontainer">';
echo '<div class="erroricon">';
echo "!";
echo '</div>';
echo "Your search returned no results, try searching for a website category e.g Sports or Cars";
echo '</div>';
}
else{
while ($row =mysql_fetch_object ($result)) {
//- Main Content -
echo '<div class="wrapper">';
?>
<?php
//--------- GET THE POST HTML VARIABLES. PUT INTO PHP VARIABLES -----------------------------
$search = $_POST['search' ];
//--------------- IF THE ROW COUNT IS NOT = 0, WHILE THERE ARE ROWS WITH CONTENT, OUTPUT HTML ----------
?>
<?php
echo '<div class="imagecontainer">';
echo "<img src=\"".$row->thumbnail."\" width=\"300\" height=\"300\"border=\"0\"/>";
echo '<div class="titlecontainer">';
echo "".$row->title."";
echo'</div>';
echo'</div>';
echo'</div>';
}
}
?>
</body>
</html>
At the minute, the content displays when you search for something that exists, however if you search for something that doesn't exist - the error message that I have set does not display, and I am unsure why.
You have misplaced following code
$numrows = mysql_num_rows ($result );
should be before following line of code
if($numrows = 0 and isset($_POST['search'])){
Try this one it should work,I have not included the HTML tags but the structure structure should be right.
$querySelect = "SELECT thumbnail,image,title FROM homepage WHERE title LIKE '%".mysql_real_escape_string($_POST['search'])."%' ";
$result = mysql_query($querySelect);
$numrows = mysql_num_rows($result); //this should be on before the if statement
if($numrows == 0 && isset($_POST['search'])){ //replaced and with &&
echo '<div class="errorcontainer">';
echo '<div class="erroricon">';
echo "!";
echo '</div>';
echo "Your search returned no results, try searching for a website category e.g Sports or Cars";
echo '</div>';
} else{//The rest of the content.}

Using PDO to make a MySQL search query and display list of results

As the title suggests, I am trying to display a list of search results using PDO and MySQL...I have a table of recipes, which have recipe_id, name & description. I would like to have a search box which can find a key word IN the name or description, i.e. "salad" or "carrots", and return a list of all matching recipes by displaying only their names. Before I switched to using PDO, I had the following code which did exactly what I needed:
<?php
include ("dbconnect.php");
if (!isset($_POST['search'])) {
header("Location:index.php");
}
$search_sql="SELECT * FROM Recipe WHERE name LIKE '%".$_POST['search']."%' OR description LIKE '%".$_POST['search']."%'";
$search_query=mysql_query($search_sql);
if (mysql_num_rows($search_query)!=0) {
$search_rs=mysql_fetch_assoc($search_query); }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h3>Search results</h3>
<?php
if (mysql_num_rows($search_query)!=0) {
do { ?>
<p><?php echo $search_rs['name']; ?></p>
<?php }
while ($search_rs=mysql_fetch_assoc($search_query));
}
else {
echo "No results found";
}
?>
</body>
</html>
However, I am having difficulties doing the same with PDO...I have come up with the following code so far, but I suspect I am doing it wrong and furthermore I do not know how to display the actual results...I would be very grateful if anyone can provide some assistance, and please excuse my insufficient knowledge on the matter, I am still a newbie.
<?php
include ("dbconnect.php");
if (!isset($_POST['search'])) {
header("Location:index.php");
}
// keep track post values
$name = "%".$_POST['search']."%";
$description = "%".$_POST['search']."%";
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql ='SELECT * FROM recipe WHERE name LIKE ? OR description LIKE ?';
$q = $pdo->prepare($sql);
$q->execute(array($name,$description));
$data = $q->fetchAll(PDO::FETCH_ASSOC);
Database::disconnect();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
<title>Untitled Document</title>
</head>
<body>
<div class="container">
<div class="row">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Search Results</th>
</tr>
</thead>
<tbody>
<?php
if ($data != null) {
foreach($data as $row)
{
echo '<tr>';
echo '<td>'. $row['name'] . '</td>';
echo '</tr>';
}
}else
{
echo '<td>'. "No results found" .'</td>';
}?>
</tbody>
</table>
</div>
</div>
</body>
</html>
You need to put the % on your parameters:
// keep track post values
$name = "%".$_POST['search']."%";
$description = "%".$_POST['search']."%";
Note, this in general is going to perform horribly as starting your like with a % will kill any index you have on name or description. As you're data grows, you'll start to see the slowdown.
Instead, you can take a look at full text searching options: http://blog.marceloaltmann.com/en-using-the-mysql-fulltext-index-search-pt-utilizando-mysql-fulltext/

arranging images in xampp database

i have this code in php. i want my images displayed to be horizontally displayed. those images are saved in xampp. can anybody help me? this is my code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
mysql_connect("localhost","root","");
mysql_select_db("dbLetters");
$res=mysql_query("select * from tbLetters");
echo "<table>";
while ($row = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>";?> <img src = "<?php echo $row["images"]; ?>" height="200" width="200"> <? php echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
your response will be a big help. thanks. :))
Unless you use a data url, you actually need to set up a separate script to pull that will provide the image to the browser.
This will make your image url something like image.php?id=<image_id>. The image.php script will fetch the image and provide the image to the browser.
Its not usually a good idea to store image data in mysql.

Categories