I'm trying to create an RSS feed for my site, I've got 99% of it working, however I'm currently having a problem. When I go to input the description field it presents me with an encoding error. The description field in the database is text so I presumed it would work.
<?php
// Login to Databse
$link = mysqli_connect("DB_HOST", "DB_USER", "DB_PASS", "DB_NAME");
// Attempt select query execution
$sql = "SELECT * FROM posts";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
// RSS XML Header
header( "Content-type: text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>";
echo "<rss version='2.0'>";
echo "<channel>";
echo "<link>https://" . $_SERVER['SERVER_NAME'] . "</link>";
echo "<language>en</language>";
// RSS Add Content
while($row = mysqli_fetch_array($result)){
echo "<item>";
echo "<title>" . $row['title'] . "</title>";
echo "<link>https://" . $_SERVER['SERVER_NAME'] . "/" . $row['self'] . "-" . $row['id'] . "</link>";
echo "<description>" . $row['description'] . "</description>";
echo "<enclosure url='https://" . $_SERVER['SERVER_NAME'] . "/public/upload/cover/" . $row['image'] . "' length='174093' type='image/webp'/>";
echo "</item>";
}
// End RSS Feed
echo "</channel>";
echo "</rss>";
// End Feed
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
My data types are varchar's and int's, even if I change the text to varchar I get the same error:
error on line 1 at column 596: Encoding error
I'm hoping I'm just been stupid but any help would be greatly appreciated.
Related
Ive included a block of code below detailling how im placing sections of data from a table in my database in their own divs. However, im new to PHP and cant find out how to also output the "cup_id" from the database into their respective divs here: echo $cup["cup_name"] . "<br />"; Thanks for all the help in advance!
So in short how do i get this to work: echo $cup["cup_id", "cup_name"] . "<br />";
<?php
require_once("action/dbcon.php"); // Get the database connection
$get_cup = "SELECT * FROM cups";
$show_cup = mysqli_query($conn, $get_cup);
if (!$show_cup) {
echo "Could not load cup. " . "(" . mysqli_error($conn) . ")";
}
while ($cup = mysqli_fetch_assoc($show_cup)) {
echo '<div class="cup-info">';
echo $cup["cup_name"] . "<br />";
echo '</div>';
}
?>
Do you want to concatenate strings? use the dot operator:
echo $cup["cup_id"] . $cup["cup_name"];
And if you want to print it in another div, make this:
echo '<div class="cup-info">';
echo $cup["cup_id"] . "<br />";
echo '</div>';
echo '<div class="cup-info">';
echo $cup["cup_name"] . "<br />";
echo '</div>';
if you want the name and id to be in the same raw then you can follow this method
<?php
require_once("action/dbcon.php"); // Get the database connection
$get_cup = "SELECT * FROM cups";
$show_cup = mysqli_query($conn, $get_cup);
if (!$show_cup) {
echo "Could not load cup. " . "(" . mysqli_error($conn) . ")";
}
while ($cup = mysqli_fetch_assoc($show_cup)) {
echo '<div class="cup-info">';
echo $cup["cup_id"] . $cup["cup_name"]."<br />";
echo '</div>';
}
?>
if you want the name and id to be in different raw then you can follow this method
<?php
require_once("action/dbcon.php"); // Get the database connection
$get_cup = "SELECT * FROM cups";
$show_cup = mysqli_query($conn, $get_cup);
if (!$show_cup) {
echo "Could not load cup. " . "(" . mysqli_error($conn) . ")";
}
while ($cup = mysqli_fetch_assoc($show_cup)) {
echo '<div class="cup-info">';
echo $cup["cup_id"]."<br />";
echo '</div>';
echo '<div class="cup-info">';
echo $cup["cup_name"]."<br />";
echo '</div>';
}
?>
I have this PHP code, which fetches data from my SQL database called "comments".
This code prints out every comment in the table:
<?php
$sql = "SELECT id,name,email,number,text FROM comments";
$result = $conn->query($sql);
if($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<strong>ID:</strong><br> " . $row["id"] . "<br>";
echo "<strong>Navn:</strong><br> " . $row["name"] . "<br>";
echo "<strong>Email:</strong><br> " . $row["email"] . "<br>";
echo "<strong>Nummer:</strong><br> " . $row["number"] . "<br>";
echo "<strong>Melding:</strong><br> " . $row["text"] . "<br><br><br>";
}
echo '<div class = "white_line_comments"></div>';
} else {
echo "0 results";
}
This has worked fine so far, everything prints as it's supposed to.
Then I decided I wanted a way to give each individual comment some sort of identification to make them unique. I tried putting each single comment into its own div, using the SQLtable row id as id for the div.
However, when I try to access my webpage now, it tells me the website doesn't work (HTTP Error 500).
<?php
$sql = "SELECT id,name,email,number,text FROM comments";
$result = $conn->query($sql);
if($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
<div class='$row[' id'].'>";
echo "<strong>ID:</strong><br> " . $row["id"] . "<br>";
echo "<strong>Navn:</strong><br> " . $row["name"] . "<br>";
echo "<strong>Email:</strong><br> " . $row["email"] . "<br>";
echo "<strong>Nummer:</strong><br> " . $row["number"] . "<br>";
echo "<strong>Melding:</strong><br> " . $row["text"] . "<br><br><br>";
echo '</div>';
}
echo '
<div class="white_line_comments"></div>';
} else {
echo "0 results";
}
Any ideas on this? I guess I must've done something wrong when including the div, but I can't figure out what!
You have an error in this line after starting while loop:
echo "<div class ='$row['id'].'>";
It should be
echo "<div class ='". $row['id'] ."'>";
You should also configure your web server/hosting/localhost to throw a PHP error.
Read this if you are on localhost or your own server:
How can I make PHP display the error instead of giving me 500 Internal Server Error
Read this if you are using shared hosting: How do I displaying details of a PHP internal server error?
echo "<div class ='$row['id'].'>";
First line in while loop should look like this
echo "<div class = '".$row['id']."'>";
or
echo "<div class = '$row['id']'>"
You have mixed different apostrophe, try this:
echo "<div class ='" . $row['id'] . "'>";
I am having a problem displaying a JOIN statement. When I add
WHERE id = " . $team_id;
The information that is on the database will not display, but when I remove that line the information will correctly join and display on the "teaminfo.php " page, but it will display all of the data instead of the data that is unique to that id. Also when I remove the JOIN the the data that is unique to the id will display. Can anyone tell me whats wrong here. Any help will be great. Than you.
teaminfo.php
<html>
<head>
<title>Team Info page</title>
</head>
<body>
<?php
include 'connect.php';
$team_id = $_GET['id'];
// SQL query
$query = " SELECT *
FROM pitscouting
JOIN fieldscouting
ON pteam_number = fteam_number
WHERE id = " . $team_id;
if ($result = mysqli_query($mysqli, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
// Write the data of the team
echo "<br />";
echo "Pit scouting";
echo "<dt>Team:</dt><dd>" . $row["pteam_number"] . " " . $row["pteam_name"] . "</dd>";
echo "<dt>Auto:</dt><dd>" . $row["pauto"] . "</dd>";
echo "<dt>Drive:</dt><dd>" . $row["pdrive"] . "</dd>";
echo "<dt>Objetcs With No Problem?</dt><dd>" . $row["pobjNoProblem"] . "</dd>";
echo "<dt>Objects They have a problem with?</dt><dd>" . $row["pobjWithProblem"] . "</dd>";
echo "<dt>Can they shoot? If yes from where and how acc</dt><dd>" . $row["pshoot"] . "</dd>";
echo "<dt>Extra Notes about their robot?</dt><dd>" . $row["pdrive"] . "</dd>";
echo"<br />";
echo "Field Scouting ";
echo "<dt>Team Number:</dt><dd>" . $row["fteam_number"] . "</dd>";
echo "<dt>Auto:</dt><dd>" . $row["fauto"] . "</dd>";
echo "<dt>Drive:</dt><dd>" . $row["fdrive"] . "</dd>";
echo "<dt>Objetcs With No Problem?</dt><dd>" . $row["fobjNoProblem"] . "</dd>";
echo "<dt>Objects They have a problem with?</dt><dd>" . $row["fobjWithProblem"] . "</dd>";
echo "<dt>Shots taken</dt><dd>" . $row["fshots_taken"] . "</dd>";
echo "<dt>Shorts made</dt><dd>" . $row["fshots_made"] . "</dd>";
echo "<dt>Extra Notes</dt><dd>" . $row["fnotes"] . "</dd>";
}
mysqli_free_result($result);
}
// Close the database connection
mysqli_close($mysqli);
?>
<p>Return to the list</p>
</body>
</html>
Palmetto.php
<?php
include 'connect.php';
// SQL query
$query = "SELECT * FROM pitscouting ORDER BY pteam_number";
if($result = mysqli_query($mysqli, $query)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
$name = $row['pteam_number'] . " " . $row['pteam_name'];
// Create a link to teaminfo.php with the id-value in the URL
$strLink = "<a href = 'teaminfo.php?id= " . $row['id'] . "'>" . $name . "</a>";
// List link
echo "<li>" . $strLink . "</li>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $query. " . mysqli_error($mysqli);
}
// Close connection
mysqli_close($mysqli);
?>
If your tables both have an ID field you will have to specify which table you want to get the data from.
WHERE pitscouting.id = " . $team_id;
or
WHERE fieldscouting.id = " . $team_id;
Please do mention the sql injection in you're code
$team_id = $_GET['id'];
// SQL query
$query = " SELECT *
FROM pitscouting
JOIN fieldscouting
ON pteam_number = fteam_number
WHERE id = " . $team_id;
please take a look at prepared statements, to prevent sql injections in youre code
Try putting an alias.
$team_id = $_GET['id'];
// SQL query
$query = " SELECT *
FROM pitscouting p
JOIN fieldscouting f
ON p.pteam_number = f.fteam_number
WHERE p1.id = " . $team_id;
Okay so my code works pretty well so far, it all goes through, my only problem is that when I try and print the unordered list and it's contents I get nothing. When I view my source code I have <ul> </ul>. There's a space, so surely something is happening.
This is my code, I have commented it slightly but what's happening is obvious:
$uname = mysqli_real_escape_string($link, $_SESSION['Username']); //Get username ready
$sql = mysqli_query($link, "SELECT * FROM users WHERE Username = '" . $uname . "'"); //SQL Query result
if(!$sql)
{
echo "Error retrieving User ID. Please try again. MySQL Error: " . mysqli_error($link);
}
elseif($row = mysqli_fetch_assoc($sql))
{
$uid = $row['UserID']; //Obtain UserID
}
else
{
echo "Error: " . mysqli_error($link) . "<br />" . $uname . " / " . $sql . " / " . $uid;
}
mysqli_free_result($sql);
$sql = mysqli_query($link, "SELECT * FROM auditions"); //Get everything from the auditions table
if(!$sql)
{
echo "Error retrieving auditions. Please try again later. Error: " . mysqli_error($link);
}
elseif($row = mysqli_fetch_assoc($sql))
{
if(mysqli_num_rows($sql)==0)
{
echo "Sorry, there are currently no open auditions. Please try back at a later date.";
}
else
{
echo "<ul>";
while($row = mysqli_fetch_assoc($sql))
{
echo "<li><a href='auditions.php?id=" . $row['AudID'] . "'>" . $row['AudName'] . "</a></li>";
}
echo "</ul>";
}
}
else
{
echo "Error: " . mysqli_error($link);
}
Where am I going wrong? The only thing it doesn't do is actually pick up any results and I've put some data into the table so there are entries! Otherwise it would say there aren't any. I've reversed this so it shows the message if there aren't 0 entries and that works. What am I doing wrong guys?
Thanks in advance.
You are fetching the result twice. Instead, only fetch the result in the while loop:
<?php
$sql = mysqli_query($link, "SELECT * FROM auditions"); //Get everything from the auditions table
if(!$sql)
{
echo "Error retrieving auditions. Please try again later. Error: " . mysqli_error($link);
}
else{
if(mysqli_num_rows($sql)==0)
{
echo "Sorry, there are currently no open auditions. Please try back at a later date.";
}
else
{
echo "<ul>";
while($row = mysqli_fetch_assoc($sql))
{
echo "<li><a href='auditions.php?id=" . $row['AudID'] . "'>" . $row['AudName'] . "</a></li>";
}
echo "</ul>";
}
}
?>
See this link for more information regarding mysql_fetch_assoc
Can anyone help me in by telling me how can I output my XML result via PHP? I have an sql database and written the function in PHP to parse the XML and I have debugged the page in Firefox (using the NET tab) which is bringing up a the correct response corresponding to my SQL statement however, I can't actually see the data, its just a blank page.
Here is the php file to write the XML:
<?php
include("classes/database_connection.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 ('www.numyspace.co.uk', '*********', '************');
if (!$connection) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('********', $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the ticket table
$query = "SELECT * FROM ticket";
$result = mysql_query($query);
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<ticket>';
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<tickets ';
echo 'ticketID="' . parseToXML($row['ticketID']) . '" ';
echo 'locationID="' . parseToXML($row['locationID']) . '" ';
echo 'venue="' . parseToXML($row['venue']) . '" ';
echo 'tPrice="' . parseToXML($row['tPrice']) . '" ';
echo 'date="' . parseToXML($row['date']) . '" ';
echo 'availability="' . parseToXML($row['availability']) . '" ';
echo 'time="' . parseToXML($row['time']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo '/>';
}
// End XML file
echo '</ticket>';
?>
Thank you and any help is much appreciated.
EDIT: The page's markup:
<ticket><tickets ticketID="1" locationID="1" venue="The Cluny" tPrice="15" date="2012-04-17" availability="200" time="20:00:00" lat="54.978252" lng="-1.617780" /><tickets ticketID="2" locationID="1"..../></ticket>
Ensure you have the XML declaration at the top of your XML.
echo "<?xml version='1.0' ?>";