Passing URL from MySql via PHP - 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>";

Related

How to fix comment section in PHP and MySQL?

The problem is I think in function viewNews(), so I made that function to get the articles/news from database. Well, I tried to put function showCommentsArea() inside viewNews() function, but it shows me same comments in every article, I want like separated comments for every article, like on Facebook, I mean, when you enter someone's post (in this case article), you can publish a comment only for that post (article).
I tried putting showCommentsArea() function inside viewNews() function, right after displaying timestamp of that article.
# Function for viewing news. #
function viewNews()
{
global $db;
$query = "SELECT * FROM newsmodule ORDER BY timestamp";
$result = #mysqli_query($db, $query);
if (!$result)
{
echo "Error selecting headline from database.";
exit();
}
if (mysqli_num_rows($result) > 0)
{
echo "<div style='margin-left: 0; width: 100%;' class='jumbotron'>";
while ($row = mysqli_fetch_object($result))
{
echo "<h1><br>" . $row->headline . "</h1>";
echo "<hr>";
echo "<p>" . $row->storyline . "</p>";
echo "<hr>";
echo "<h5 class='pull-right'>" . $row->username . "</h5>";
echo "<p>" . $row->timestamp . "</p>";
echo "<hr>";
echo showCommentArea();
echo "<a data-target='#postComment' class='text-white dropdown-toggle btn btn-danger' data-toggle='modal' type='button'>";
echo "Publish a Comment";
echo "</a>";
}
echo "</div>";
}
else
{
echo "No headlines in database.";
}
}
function showCommentArea()
{
global $db, $errors, $username;
if(count($errors) == 0)
{
$query = "SELECT comment, name FROM comments
JOIN newsmodule ON comments.articlecID=newsmodule.id WHERE newsmodule.id=comments.articlecID";
$result = #mysqli_query($db, $query);
if(!$result)
{
echo "SQL Query ERROR: !ERR_SQL_QUERY_01";
exit();
}
if (mysqli_num_rows($result) > 0) {
echo "<div>";
echo "<h4>";
echo "Comments:";
echo "</h4>";
echo "<br>";
while ($row = mysqli_fetch_object($result)) {
echo "<p class='text-danger' style='font-weight: bold;'>" . $row->name . "</p>";
echo "<p>" . $row->comment . "</p>";
}
echo "</div>";
}
}
}
So, I expect to have one individual comments section for one individual post, you see I tried to add it with JOINING tables, but it shows me entire table 'comments'

Grab and echo out multiple SQL rows with PHP

I'm wanting to fill a database with news article information such as PostID, PostTitle, PostDescription, PostContent and so on - I tried writing some code to constantly echo out the data in the SQL query but it only works for the first row that the query picks up
$SQL = "SELECT * FROM posts"; // get all info from posts
$result = mysql_query($SQL);
$row = mysql_fetch_array($result);
if (mysql_numrows($result) > 0) {
$row = mysql_fetch_array($result);
$Title = $row["PostTitle"];
$Description = $row["PostDescription"];
$Date = $row["PostDate"];
$AuthorID = $row["AuthorID"];
echo "<div id='preview'>";
echo "<div class='group selection'>";
echo "<div class='col span_1_of_3'>";
echo "<div id='previewmedia'>";
echo "</div>";
echo "</div>";
echo "<div class='col span_2_of_3'>";
echo "<h1>".$Title."</h1>";
echo "<p class='preview'>".$Description."</p>";
echo "<a href='bloglayout.php' class='orange readmore'>Read More...</a>";
echo "</div>";
echo "</div>";
echo "</div>";
}
I then thought about doing a SELECT query to then count the number of ID's in the table and then set that as $ID and then give each variable a number and then +1 after each loop but realised that wouldn't work so I'm kinda stuck on how to loop out several rows automatically from my database.
Also, I know I shouldn't be using mysql_numrows and whatnot in PHP and I should move to mysqli or PDO but this is for something where I've been asked to use mysql specifically.
You're only fetching one row. Try looping through your results.
$SQL = "SELECT * FROM posts"; // get all info from posts
$result = mysql_query($SQL);
while( $row = mysql_fetch_array($result);)
{
$Title = $row["PostTitle"];
$Description = $row["PostDescription"];
$Date = $row["PostDate"];
$AuthorID = $row["AuthorID"];
echo "<div id='preview'>";
echo "<div class='group selection'>";
echo "<div class='col span_1_of_3'>";
echo "<div id='previewmedia'>";
echo "</div>";
echo "</div>";
echo "<div class='col span_2_of_3'>";
echo "<h1>".$Title."</h1>";
echo "<p class='preview'>".$Description."</p>";
echo "<a href='bloglayout.php' class='orange readmore'>Read More...</a>";
echo "</div>";
echo "</div>";
echo "</div>";
}

For loop retrieves same mysql row

I’m trying to retrieve the first 4 entries with a for loop, but it seems to repeat 1 entry 4 times.
This is my code:
$con=mysqli_connect("localhost","root","root","test");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM activiteiten");
while($row = mysqli_fetch_array($result)) {
for ($result = 1; $result <= 4; $result++) {
echo "<div class='agenda_item' style='background-color:#F39'>";
echo "<img src='images/soosavond_small.jpg' class='soospict_small' />";
echo "<div class='agenda_content'>";
echo "<p class='datum'>" . $row['datum'] . "</p>";
echo "<p class='onderwerp'>" . $row['naam_activiteit'] . "</p>";
echo "<p class='details'>" . $row['beschrijving'] . "</p>";
echo "</div>";
echo "<a href='#'' class='pijlklein'>MEER INFO</a>";
echo "</div>";
}
}
mysqli_close($con);
You don't need a for loop. I've modified code, to retrive only first 4 items.
Look at the cahnged query LIMIT 4.
<?php
$con=mysqli_connect("localhost","root","root","test");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM activiteiten LIMIT 4");
while($row = mysqli_fetch_array($result)) {
echo "<div class='agenda_item' style='background-color:#F39'>";
echo "<img src='images/soosavond_small.jpg' class='soospict_small' />";
echo "<div class='agenda_content'>";
echo "<p class='datum'>" . $row['datum'] . "</p>";
echo "<p class='onderwerp'>" . $row['naam_activiteit'] . "</p>";
echo "<p class='details'>" . $row['beschrijving'] . "</p>";
echo "</div>";
echo "<a href='#'' class='pijlklein'>MEER INFO</a>";
echo "</div>";
}
mysqli_close($con);
?>
You have a for loop inside the while loop. You can remove the for loop and use LIMIT from MySQL (which is better as fetching all entries by PHP (better performance), if you need only the first 4 entries):
SELECT * FROM activiteiten LIMIT 4
If you still want to do it with php:
$results = 0;
while($row = mysqli_fetch_array($result) && $results < 4) {
echo "<div class='agenda_item' style='background-color:#F39'>";
...
$results++;
}
Your code—as presented—mixes things up in ways that behave the way you see. The for loop is not needed. And you should set a LIMIT in your MySQL query like this:
$con=mysqli_connect("localhost","root","root","test");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM activiteiten LIMIT 0,4");
while($row = mysqli_fetch_array($result)) {
echo "<div class='agenda_item' style='background-color:#F39'>";
echo "<img src='images/soosavond_small.jpg' class='soospict_small' />";
echo "<div class='agenda_content'>";
echo "<p class='datum'>" . $row['datum'] . "</p>";
echo "<p class='onderwerp'>" . $row['naam_activiteit'] . "</p>";
echo "<p class='details'>" . $row['beschrijving'] . "</p>";
echo "</div>";
echo "<a href='#'' class='pijlklein'>MEER INFO</a>";
echo "</div>";
}
mysqli_close($con);
The problem with your original code is mixing for with while ends up in the scenario you showed as explained in the PHP manual entry for while:
The meaning of a while statement is simple. It tells PHP to execute
the nested statement(s) repeatedly, as long as the while expression
evaluates to TRUE.
So you have a while iterating over the results, but then you also have for loop nested in there. 100% unnecessary.
The way I adjusted it is to just removed that for loop, but also add LIMIT 0,4 to the MySQL query. That basically tells MySQL to fetch 4 rows beginning from the first row which is referred to as 0.

how to set empty space when records is not into data base

i am created html form ,when i click on submit button i get records from data base,but if the data is not in to the data base its print
http://bukkyolu.com/mp3/upload/
that not come form database its just static value that i am print in statement bellow
http://bukkyolu.com/mp3/upload/".$row['2']."'> ,
if the .$row['2'].is not in to database
i want empty space not default link that printed
<?php
$category=$_POST["category"];
$month=$_POST["month"];
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "bukkyolu_mp3";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");
$query = "SELECT title,Amp3,Bmp3,Cmp3 FROM mp3_data WHERE`category` = '$category' and `month` = '$month' ";
$result1 = mysql_query($query);
$row = mysql_fetch_array($result1);
//echo $row['s_id'];
echo "<table border='1' align='center' >";
while ($row = mysql_fetch_row($result1)){
// echo "<td><a href='#' onclick='someFunction()'>" .$row['0']. "</a> </td>";
echo "<tr><th>Title:</th><td>".$row['0']."</td></tr>";
echo "<tr><th>African:</th><td><a href 'http://bukkyolu.com/mp3/upload/".$row['1']."'>http://bukkyolu.com/mp3/upload/".$row['0']."</a></td></tr>";
echo "<tr><th>British:</th><td><a href 'http://bukkyolu.com/mp3/upload/".$row['2']."'>http://bukkyolu.com/mp3/upload/".$row['0']."</a></td></tr>";
echo "<tr><th>Caribbean:</th><td><a href 'http://bukkyolu.com/mp3/upload/".$row['3']."'>http://bukkyolu.com/mp3/upload/".$row['3']."</a></td></tr>";
echo "<tr></tr>";
echo "<tr></tr>";
}
echo "</table>";
?>
Check for the value as below.
if(isset($row['0']) && $row['0'] != "")
{
echo "<tr><th>African:</th><td><a href 'http://bukkyolu.com/mp3/upload/".$row['1']."'>http://bukkyolu.com/mp3/upload/".$row['0']."</a></td></tr>";
}
else
{
echo "<tr><th>African:</th><td>...</td></tr>";
}
Do the same for other.
OR
Create function that will more like a good programming habit.
function checkForValue($value)
{
$response = "&nbsp";// you can also add ... or anything
if(isset($value) && $value != "")
{
$response = $value;
}
return $response;
}
and call it as .
echo "<tr><th>Title:</th><td>".checkForValue($row['0'])."</td></tr>";
try this:
isset($row['0'])?$row['0']:''
while ($row = mysql_fetch_row($result1)){
// echo "<td><a href='#' onclick='someFunction()'>" .$row['0']. "</a> </td>";
echo "<tr><th>Title:</th><td>".isset($row['0'])?$row['0']."</td></tr>";
echo "<tr><th>African:</th><td><a href 'http://bukkyolu.com/mp3/upload/".isset($row['1'])?$row['1']:''."'>http://bukkyolu.com/mp3/upload/".isset($row['0'])?$row['0']:''."</a></td></tr>";
echo "<tr><th>British:</th><td><a href 'http://bukkyolu.com/mp3/upload/".isset($row['2'])?$row['2']:''."'>http://bukkyolu.com/mp3/upload/".$row['0']."</a></td></tr>";
echo "<tr><th>Caribbean:</th><td><a href 'http://bukkyolu.com/mp3/upload/".isset($row['3'])?$row['3']:''."'>http://bukkyolu.com/mp3/upload/".isset($row['3'])$row['3']:''."</a></td></tr>";
echo "<tr></tr>";
echo "<tr></tr>";
}
echo "</table>";
etc in this way you can set space if value does not exits
hope it will help!
Add additional condition that if your query has no results found.
It will just do else with no mysql_fetch_rows because mysql_fetch_rows needed outputs to be fetch..
if(count($result1) > 0){
while ($row = mysql_fetch_row($result1)){
// echo "<td><a href='#' onclick='someFunction()'>" .$row['0']. "</a> </td>";
echo "<tr><th>Title:</th><td>".(!empty($row['0'])? $row['0']: ' '."</td></tr>";
echo "<tr><th>African:</th><td><a href '".(!empty($row['0']))? 'http://bukkyolu.com/mp3/upload/'.$row['0'] : ' '."'>".(!empty($row['0']))? 'http://bukkyolu.com/mp3/upload/'.$row['0'] : ' '."</a></td></tr>";
echo "<tr><th>African:</th><td><a href '".(!empty($row['0']))? 'http://bukkyolu.com/mp3/upload/'.$row['2'] : ' '."'>".(!empty($row['0']))? 'http://bukkyolu.com/mp3/upload/'.$row['0'] : ' '."</a></td></tr>";
echo "<tr><th>British:</th><td><a href '".(!empty($row['0']))? 'http://bukkyolu.com/mp3/upload/'.$row['3'] : ' '."'>".(!empty($row['0']))? 'http://bukkyolu.com/mp3/upload/'.$row['3'] : ' '."</a></td></tr>";
echo "<tr></tr>";
echo "<tr></tr>";
}
}
else{
echo "<tr><th>Title:</th><td> </td></tr>";
echo "<tr><th>African:</th><td><a href 'javascript:void(0);'> </a></td></tr>";
echo "<tr><th>African:</th><td><a href 'javascript:void(0);'> </a></td></tr>";
echo "<tr><th>British:</th><td><a href 'javascript:void(0);'> </a></td></tr>";
echo "<tr></tr>";
echo "<tr></tr>";
}
try to include ternary condition in each of your result
so that if the fetch data is empty it will display the other condition as ' '

mysqli not displaying image from database

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>';

Categories