"No data received" on GET request - php

I keep getting a "No data received" error, I know this is common with Google Chrome so I tried in IE and I get a connection problem error. Here is my script, I really don't see what is causing this error.
$getAlName = mysql_query("SELECT * FROM categories WHERE id=" . $cat);
$alName = mysql_feth_assoc($getAlName);
$images = mysql_query("SELECT * FROM images WHERE category=" . $alName['name']);
while($imgs = mysql_fetch_object($images)) {
$url = $imgs->url;
$id = $imgs->id;
echo ("<img src='" . $url . "'></img>\n");
}

You need to add single quotes around your strings:
"SELECT * FROM images WHERE category = '" . $alName['name']) . "'";
...and you also got a typo, use mysql_fetch_assoc instead of mysql_feth_assoc($getAlName);

//Make a subquery and you'll thank yourself later:
$q = "SELECT URL, ID FROM images WHERE category IN ".
"( SELECT * FROM categories WHERE id=" . $cat . ")";
echo $q; // just to test. no data received probably has to do with no output to
// the browser. This will output to the browser.
$images = mysql_query($q);
// this is the same.
while($imgs = mysql_fetch_object($images)) {
$url = $imgs->url;
$id = $imgs->id;
echo ("<img src='" . $url . "'></img>\n");
}

Related

How to Remove Slug from my Post , Post.php?Slug=postname-title

I have made a blog in PHP, on Post.php with If isset get I am checking Post ID and than getting slug of same ID from database and accessing the post with that slug.
The issue is its generating Url like: Post.php?Slug=postname-title
I want to make it Post.php?postname-title OR urls.com/postname-title
Please note I am getting post from database matching with slug If any how I do not set php to Get in url it is not getting value from the same slug.
Please guide if it will be done via .htaccess or any other way?
<?php
// add mysql_real_escape_string to slug, to prevent sql injection
$slug = $_GET["slug"];
$sql = "SELECT * FROM posts WHERE slug = '" . mysqli_real_escape_string($db, $_GET["slug"]) . "'";
$result = mysqli_query($db, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo "<h2>" . $row["title"] . "</h2>";
echo "<div id='post-details'>" . "Posted on " . $row["post_date"] . " -- " . "Last Update " . $row["last_edited"] .
"</div>";
if (loggedin()) { echo "<div id='postactions'>This Post:<a href='create_note.php?id=" . $row["id"] . "'>Add Notes</a>";
echo 'Delete Post';
echo 'Edit</div>'; }
$cat_id = $row["cat_id"];
$sql = "SELECT * FROM categories WHERE id = $cat_id";
$resultone = mysqli_query($db, $sql);
if (mysqli_num_rows($resultone) > 0) {
// output data of each row
while ($rowone = mysqli_fetch_assoc($resultone)) {
$cat_name = $rowone["name"];
echo "<div id='post-category'>" . "Category: <a href='all-categories.php?cat_id=$cat_id'>$cat_name</a>" . "</div>";
}
}
echo "<p>" . htmlspecialchars_decode($row["body"]) . "</p>";
$_SESSION["post_id"] = $row["id"];
$postid = $row["id"];
}
} else {
echo "0 results";
}
?>
If you want a simple .htaccess version, you could use the following rule:
RewriteRule ^post/(.+) Post.php?Slug=$1
This will change your URL from a pretty URL like: yourdomain.com/post/postname-title to yourdomain.com/Post.php?Slug=postname-title

Return data from mysql where value equals URI

I'm trying to return a row from my database however, when I replace the page='url' with $filePath = $_SERVER["REQUEST_URI"]; page='.$filePath.' it returns nothing.
I'm assuming the answer is simple however, I can't see to find the solution.
Full Code
$filePath = $_SERVER["REQUEST_URI"];
$query = "SELECT * FROM Meta WHERE page=' . $filePath . '";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) { ?>
<title><?php echo $row["title"]; ?></title>
<?php } ?>
try the query like this
$query = "SELECT * FROM Meta WHERE page = '" . $filePath . "' ";

How to properly optimise mysql select statement

I have a web page that is divided into different sections. Each section has to show different results. These results are gotten from the database.
This is a sample data on SQLfiddle
http://sqlfiddle.com/#!9/ad98b/1
The following code is what comes to my mind but I'm afraid that it might somehow overload the server when this page is accessed multiple times by different people
$sectionA = $connect->query("SELECT * FROM Main_Section WHERE section = `A`
");
while ($row = $sectionA->fetch_array(MYSQLI_BOTH))
{
$id = $row["id"];
$name = $row["name"];
$sec_result_a = $sec_result_a.'<p>'.$id.'</p><h3>'.$name.'</h3>';
}
$sectionB = $connect->query("SELECT * FROM Main_Section WHERE section = `B` ");
while ($row = $sectionB->fetch_array(MYSQLI_BOTH))
{
$id = $row["id"];
$name = $row["name"];
$sec_result_b = $sec_result_b.'<p>'.$id.'</p><h3>'.$name.'</h3>';
}
$sectionC = $connect->query("SELECT * FROM Main_Section WHERE section = `C` ");
while ($row = $sectionC->fetch_array(MYSQLI_BOTH))
{
$id = $row["id"];
$name = $row["name"];
$sec_result_c= $sec_result_c.'<p>'.$id.'</p><h3>'.$name.'</h3>';
}
UP TO section Z
Is there a way I can Optimise this properly?
Unless there's more to the picture, why not just query everything, ordered by section, to have the A-Z:
SELECT * FROM Main_Section ORDER BY section
... and then process the results with one loop, which could look something like this:
$sections = $connect->query("SELECT * FROM Main_Section ORDER BY section");
while ($row = $sections->fetch_array())
{
echo $row['section'] . ' ' . '<p>' . $row['id'] . '</p><h3>' . $row['firstname'] . ' ' . $row['lastname'] . '</h3>';
}

How to set permalink of your blog post according to date and title of post?

I am having this website http://www.finalyearondesk.com . My blogs post link are set like this.. http://www.finalyearondesk.com/index.php?id=28 . I want it to set like this ... finalyearondesk.com/2011/09/22/how-to-recover-ubuntu-after-it-is-crashed/ .
I am using the following function to get these posts...
function get_content($id = '') {
if($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * from cms_content WHERE id = '$id'";
$return = '<p>Go back to Home page</p>';
echo $return;
else:
$sql = "select * from cms_content ORDER BY id DESC";
endif;
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) != 0):
while($row = mysql_fetch_assoc($res)) {
echo '<h1>' . $row['title'] . '</h1>';
echo '<p>' . "By: " . '<font color="orange">' . $row['author'] . '</font>' . ", Posted on: " . $row['date'] . '<p>';
echo '<p>' . $row['body'] . '</p><br />';
}
else:
echo '<p>We are really very sorry, this page does not exist!</p>';
endif;
}
And I am using this code to dispaly it on my index.php page...
<?php
if (isset($_GET['id'])) :
$obj -> get_content($_GET['id']);
else :
$obj -> get_content_summary();
endif;
?>
Any suggestions how to do this? And can we do this by using .htaccess?
The unfortunate thing about using mod_rewrite is that the data you are supplying in the form of a url is not the best way to query a database. But none the less you have year, month, day and title variables so you will need to rewrite your get_content function to query soomething like (depending on how you date is stored in the database.):
select * from cms_content
WHERE date='mktime(0,0,0,$month,$day,$year)'
AND title='$title'
ORDER BY id DESC
.htaccess would be something like:
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ index.php?year=$1&month=$2&day=$3&title=$4

PHP link to template page with database content

I am setting up a webpage for a student organization with bios for the officers along with pictures and whatnot.
the first page simply is html and css. it has a picture, name under it and a link to the full bio where it links to "bio.php?id=" and then the id in my SQL database for that person.
now i am trying to make the php page to allow a simple template php page using the user's id. unfortunately when i do everything that I think is right, I get an odd error.
here is my code
<html>
<body>
<?php
//connection to database
//specify database
$id= $GET['id'];
$sql = " SELECT * FROM Members_table WHERE Id='$id' ";
$result = mysql_query($sql) or print ("Can't select entry from table bloghomepage.<br />" . $sql . "<br />" . mysql_error());
WHILE($row = mysql_fetch_array($result)) {
$name = $row['Name'];
$position = $row['Position'];
$major = $row['Major'];
$hometown = $row['Hometown'];
$awards = $row['Awards'];
$bio = $row['Description'];
$act = $row['Activities'];
$pic = $row['Picture'];
$misc = $row['other'];
?>
<h1><?php print $name; ?></h1>
<p><?php print '<img src="' . $pic . '"'; ?>
<?php } ?>
</body>
</html>
This is what i see on my webpage:
" . $sql . "
" . mysql_error()); WHILE($row = mysql_fetch_array($result)) { $name = $row['Name']; $page_id= $id; $position = $row['Position']; $major = $row['Major']; $hometown = $row['Hometown']; $awards = $row['Awards']; $bio = $row['Description']; $act = $row['Activities']; $pic = $row['Picture']; $misc = $row['other']; ?>
and thats all. any ideas what i am doing wrong?
you just don't have PHP enabled on your host.
Hint: always see page source, not picture rendered by browser. It's HTML code being result of your PHP script, so, you have to check HTML code, not a picture rendered from it.
The PHP isn't being parsed, presumably because the necessary module/content handler isn't set up within your web server.
It's not directly related to the topic but you might want to cast the value of the GET parameter as an integer before reusing it in a query to prevent basic SQL injection
$id = intval( $_GET['id'] );

Categories