mysqli not displaying image from database - php

Im changing everything to mysqli and because im just learning its pretty hard. What am i doing wrong? my images appear broken and prints the file name, I know this is a simple question but ive tried searching the internet with no result. sorry im learning
<?php
include_once("db_conex.php");
$query = "SELECT * FROM employees ORDER BY price ASC";
$query = mysqli_real_escape_string($db,$query);
if($result = mysqli_query($db,$query)){
while($row = mysqli_fetch_object($result)){
echo "<br /><br />";
echo '<img src="/upload/ " border=0>', $row->photo;
echo "<br /><br />";
echo '<b> City: </b>', $row->city;
echo '<b> Price: </b>', $row->price;
echo '<b> Bath: </b>', $row->bath;
echo '<b> Bath: </b>', $row->bed;
echo '<b> Description: </b>', $row->description;
echo '<b> Link: </b>', $row->link;
}
mysqli_free_result($result);
}
//Close Connection
mysqli_close($db);
?>

Your problem is here:
echo '<img src="/upload/ " border=0>', $row->photo;
You are literally just printing an empty (probably non-existant) image, followed by the filename. You probably meant:
echo '<img src="/upload/' . $row->photo . '" border=0>';

Related

Trying to query MySQL database in PHP. Can INSERT but not SELECT for some reason

EDIT: SOLVED. Thanks to the guys in the comments who recommended turning PHP error messages on, showed me exactly where things were going wrong. For those that are curious, it was as simple as this: two function calls had "mysql_" instead of "mysqli_". Also thanks to the user that pointed out that prepared SQL queries must use ->execute() instead of mysqli_query().
I'm working on a community bulletin board. I've gotten it to a point where the page for posting a listing works and submits it to the database, but when I try to search for the listings I'm having some trouble with the SQL query. Here's the code that I have:
<?php
if(isset($_GET['submit'])){
$location = $_GET['location'];
echo "Looking for listings in: " . $location;
echo "<br />";
require_once('mysqli_connect.php');
$query = "
SELECT email_address
, phone_number
, date
, listing_content
FROM listings
WHERE location='?'
";
echo $query;
$stmt = mysqli_prepare($dbc, $query);
mysqli_stmt_bind_param($stmt, "s", $location);
$results = mysqli_query($stmt);
$numListings = mysql_num_rows($results);
echo $numListings . " listing(s) found";
echo "<hr>";
if ($numListings == 0){
echo "Feel free to press the button above to post the first listing in " .
$location;
} else {
while($row = mysql_fetch_array($results)){
echo "Location: " . $location;
echo "<br />";
echo "Date: " . $row[date];
echo "<br />";
echo "Email address: " . $row[email_address];
echo "<br />";
if($row[phone_number] != NULL){
echo "Phone number: " . $row[phone_number];
echo "<br />";
}
echo $row[listing_content];
echo "<br />";
echo "<hr>";
}
}
mysqli_stmt_close($stmt);
mysqli_close($dbc);
}
?>
And this is the output I'm getting, where everything below the button "Make new post" is generated by the PHP:
It looks like it's crashing, at some point after the line echo $query; because it prints that out but it doesn't print some of the other statements after that. Either it's crashing or the SQL query is somehow messing up without crashing it or throwing an error.
This is what the database looks like:

How to represent a link dynamically in php?

<?php
$stmt = $con->prepare("SELECT * FROM user_tbl WHERE name = ?");
$stmt->bind_param('s', $_REQUEST['name']);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo '<br /><br />Name: ' .$row['name'];
echo '<br /><br />Contact Number: ' .$row['cont'];
echo '<br /><br />Email ID: ' .$row['email'];
echo "<br /><br /><a href='viewdtl.php' target='_blank'>View details</a>";
echo "<br /><hr />";
}
}
else
{
echo "0 records found";
}
$stmt->close();
?>
This code runs just fine but with two problems.. The else part 0 records found always shows which i want to print after the search get placed and the link .. i can't click on it.. how can i print the link here ?

how to add "no record found" in this approach using mysql_query

How to add no record found if the result is empty. this approach search the last record of the customer if he is new then the result is empty. but I want to add a word "no record found"
$when = ' <span style="font-weight: 700;">When:</span>';
$app1 = $_GET["app_id"];
$query = mysql_query("SELECT * FROM request WHERE app_id = $app1 ORDER BY id DESC LIMIT 1,1") or die(mysql_error());
while ($row = mysql_fetch_assoc($query)) {
echo $ctr= '(','1-1508-0010'. ')';
echo ' ';
echo '<span style="text-transform: uppercase; font-weight: 500;">'.$row['request'].'</span>';
echo ': ';
echo $row['description'];
echo '<br>';
echo $when,' ';
echo $row['datereq'];
echo ' ';
echo '<br>';
}
echo '<br>';
Try like this
if(mysql_num_rows($query)==0)
{
// no record
}
N:B:
mysql_ is officially deprecated (as of PHP 5.5. Will be removed in PHP 7.). try to use mysqli_ instead

Display data from database depending on the how many I have entered

In my database and more specific in the table named portfolio a user with a speficic username can store more than one data. The columns of the database are id,username,portfolio and portfolio_description
and I want to echo in my web page all the data for this username for example username=nbourlai. Until now I can echo only the first row for each username what is the loop that i can use in order to echo as many time is the id number.
Below is the current result and i want to create Portfolio and Portfolio description to be displayed with the appropriate values as many time as the highest id number is for the specific user.
Here is my code...
$portfolio = queryMysql("SELECT portfolio,portfolio_description FROM portfolio WHERE username='nbourlai'");
$row = mysql_fetch_row($portfolio);
echo "<h2>Portfolio</h2>";
echo "Portfolio: ";
echo stripslashes($row[0]) . "<br/>Portfolio Description: ";
echo stripslashes($row[1]) . "<br clear=left /><br />";
Can you help me please?
Are you looking for while loop like this?
$portfolio = queryMysql("SELECT portfolio,portfolio_description FROM portfolio WHERE username='nbourlai'");
echo "<h2>Portfolio</h2>";
while($row = mysql_fetch_row($portfolio)){
echo "Portfolio: ";
echo stripslashes($row[0]) . "<br/>Portfolio Description: ";
echo stripslashes($row[1]) . "<br clear=left /><br />";
}
or, like this?
$portfolio = queryMysql("SELECT username,portfolio,portfolio_description FROM portfolio ORDER BY username");
$username = '';
while($row = mysql_fetch_row($portfolio)){
if($username != $row[0]){
echo "<h1>".$row[0]."</h1>";
echo "<h2>Portfolio</h2>";
echo "Portfolio: ";
echo stripslashes($row[1]) . "<br/>Portfolio Description: ";
echo stripslashes($row[2]) . "<br clear=left /><br />";
}else{
echo "Portfolio: ";
echo stripslashes($row[1]) . "<br/>Portfolio Description: ";
echo stripslashes($row[2]) . "<br clear=left /><br />";
}
$username = $row[0];
}

Passing URL from MySql via PHP

I'm trying to work out how to pass a URL that's stored in a MySQL database and attach it to a image.
The script i have so far is this:
<?php
$con = mysqli_connect("**********","*********","******","********");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$selectedOption = $_POST["mySelect"];
$result = mysqli_query($con,
sprintf("SELECT * FROM `SouthYorkshire` WHERE `EstProv` = '%s'",
preg_replace("/[^A-Za-z ]/", '', $selectedOption)
)
); // pattern based on your html select options
echo "<div id=\"Results\">";
while($row = mysqli_fetch_array($result)) {
echo "<div class=\"ClubName\">";
echo $row['EstName'];
echo "</div><br>";
echo "<div class=\"Location\">";
echo $row['EstAddress2'];
echo "</div>";
echo "<br>";
echo "<div id=\"website\"><img src=\"photos/visit-website-button.png\" width=\"75\" height=\"25\" /></div>";
}
echo date("Y") . " " ."Search is Powered by PHP.";
echo "</div>";
mysqli_close($con);
the bit i'm trying to change is :
echo "<div id=\"website\"><img src=\"photos/visit-website-button.png\" width=\"75\" height=\"25\" /></div>"
I tried to use the same above with $row['EstWebsite']
But am not having any success, any suggestions would be great.
Many Thanks
Doesn't matter where the URL comes from, in the end it's just a string of data, and you're just going to be building some HTML with it, so
echo '<div id="website"><img blah blah blah>";
^^^^^^^^^^^^^^^^^^^^^^^-add this ^^^^--add this
You can use the following:
echo "<div id=\"website\"><img src=\"" . $row['ImageColumnName'] ."\" width=\"75\" height=\"25\" /></div>";

Categories