Jump to content

[SOLVED] Special Character _GET Problem - empty page


EternalSorrow

Recommended Posts

I'm currently working on a page which 'gets' the specified variables from the fields 'title' and 'author' from three tables.  The extra trick is some titles and some authors' names contain special characters, so I'm using the 'sprintf' method to avoid an error message.

 

For the three tables I'm retrieving information from the first two in the main query, and accessing the third table in a subquery.  The problem lies in the subquery for the third table, here called 'information.'

 

I'm trying to retrieve the fields 'datetime' and 'summary' from 'information' using array, but whenever I place the proper snippet into the main code the page becomes blank without even an error message.  When I remove the snippet the page reappears without any error messages, but I'm left without the information stored in the 'information' table.

 

Any ideas what the error could be in my code?  (Beware, it's a mess)

 

<?php

if (!is_numeric($_GET["author"]) && !empty($_GET["author"]) && $_GET["author"]!="")
{
$author = $_GET["author"];
}

if (!is_numeric($_GET["title"]) && !empty($_GET["title"]) && $_GET["title"]!="")
{
$title = $_GET["title"];
}

mysql_connect(localhost,user,pw);
@mysql_select_db(db) or die( "Unable to select database");

$query = sprintf("SELECT * FROM archives LEFT JOIN author ON author.id = archives.id 
WHERE `author`= '%s' AND archives.title = '%s' LIMIT 1 ", mysql_real_escape_string($author),  mysql_real_escape_string($title)) or die(mysql_error());
$result = mysql_query( $query ) or die(mysql_error());

while ($row = mysql_fetch_array($result))
{
extract($row);

$select_category = sprintf("SELECT DISTINCT category FROM archives LEFT JOIN author ON author.id = archives.id 
WHERE `title` = '%s' AND `author`= '%s' ORDER BY category asc", mysql_real_escape_string($title), mysql_real_escape_string($author));

$select_category = mysql_query($select_category) or die (mysql_error());

$cat = "";
while ($row2 = mysql_fetch_array($select_category)) {
   $cat .= "<a href=\"category.php?category={$row2[category]}\">{$row2[category]}</a>, ";
}

$cat = substr($cat,0,-2);

$select_date = sprintf("SELECT author, title, summary, date_format(information.datetime,'%%M %%D, %%Y') AS datetime FROM information WHERE `author`= '%s' AND title = '%s' ", mysql_real_escape_string($author), mysql_real_escape_string($title));
$select_results = mysql_query($select_date) or die (mysql_error());

$tableArray = mysql_fetch_array($select_results) or die (mysql_error());

echo '<h1>Info: '.$title.'</h1>

<div class="story_url"><a href="'.$url.'" target="_blank">Read</a></div>

Author: <a href="author.php?author='.$author.'">'.$author.'</a>
<br>Categories: '.$cat.'
<br>Rating: '.$rating.'
<br>Last Updated: '.$tableArray['datetime'].'

<p>Summary: '.$tableArray['summary'].'';

}
?>

Link to comment
Share on other sites

This is the line which I am forced to remove:

 

$select_date = sprintf("SELECT author, title, summary, date_format(information.datetime,'%%M %%D, %%Y') AS datetime FROM information WHERE `author`= '%s' AND title = '%s' ", mysql_real_escape_string($author), mysql_real_escape_string($title));
$select_results = mysql_query($select_date) or die (mysql_error());

$tableArray = mysql_fetch_array($select_results) or die (mysql_error());

 

If any of the die() is being set off, I have no idea why it wouldn't show the mysql_error, even on the page source.  Here's an example page of the problem.

Link to comment
Share on other sites

Ok so error reporting may not be on for your server. But you can also do a var_dump($select_results); because I think you will find that $select_results is not an array.

To turn on Error reporting post the following at the very top of your code.

 
<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
?>

Link to comment
Share on other sites

The Error Reporting merely complains about undefined constants in my database connection area and this line:

 

   $cat .= "<a href=\"category.php?category={$row2[category]}\">{$row2[category]}</a>, ";

 

for this transgression:

 

Notice: Use of undefined constant category - assumed 'category' in /home/animara/public_html/inufiction/info.php on line 46
Link to comment
Share on other sites

That fixed that error, but I'm still left with the page containing only the "Died at third die" on this line:

 

$tableArray = mysql_fetch_array($select_results) or die('Died at third die() :'.mysql_error());

 

I've tried removing that line and it fixes the problem, but then I have no idea how I'm supposed to display the information queried from that table.

Link to comment
Share on other sites

Try mysql_fetch_object() see if that fails. I am looking into this right now. Also can you remove the or die part and make sure you have error reporting on so that we can see any extra errors that might help to troubleshoot it.

 

Edit: It may also be returning false, which could mean that your input is incorrect. To check for FALSE, do this:

<?php
$tableArray = mysql_fetch_array($select_results);
if($tableArray===FALSE){
  echo 'Returning FALSE<br />';
}
?>

There are a hundred other ways you could perform this same check so you do not have to do it exactly this way. But just to troubleshoot.

 

Link to comment
Share on other sites

Just a side note nothing really to solve this thing, but it is always good to run the SQL queries in some sort of SQL program or from the SQL command line and see if the results are the kind of results you want to see if they don't work straight away in php. Just a note that might be worth checking.

Link to comment
Share on other sites

Just a side note nothing really to solve this thing, but it is always good to run the SQL queries in some sort of SQL program or from the SQL command line and see if the results are the kind of results you want to see if they don't work straight away in php. Just a note that might be worth checking.

 

This code actually worked perfectly before I realized the sprintf has to be initialized, and I have used it several times without having any difficulty.  It's just the special characters which have thrown it for a loop.

Link to comment
Share on other sites

If you have all error reporting on and all mysql errors should be thrown in die()'s and nothing appears.. I don't know.. maybe try echoing allqueries before you run them in the code and see if they have correct query inside? Also I noticed that in the lines you have been adding and removing you have one field assigned in the query like this: date_format(information.datetime,'%%M %%D, %%Y') AS datetime, is not there too many percentage signs ? Should not that be like: date_format(information.datetime,'%M %D, %Y') AS datetime.

Link to comment
Share on other sites

Without those extra percentage signs the page would throw this error message:

 

Warning: sprintf() [function.sprintf]: Too few arguments in /home/animara/public_html/inufiction/info.php on line 46

Query was empty

 

It has something to do with the date_format conforming with PHP rather than the mysql.

Link to comment
Share on other sites

Well, I found the problem and it is stupid.  Apparently for the troubled query I had to switch the last part of the query line so that the mysql_real_escape_string for 'title' came before the mysql_real_escape_string for 'author'.

 

So switch this:

 

$select_date = sprintf("SELECT author, title, summary, date_format(information.datetime, '%%M %%D, %%Y') AS datetime FROM information WHERE `author`= '%s' AND title = '%s' ", mysql_real_escape_string($author), mysql_real_escape_string($title));

 

to this:

 

$select_date = sprintf("SELECT author, title, summary, date_format(information.datetime, '%%M %%D, %%Y') AS datetime FROM information WHERE `author`= '%s' AND title = '%s' ", mysql_real_escape_string($title), mysql_real_escape_string($author));

 

Whether that's because the other queries follow that sequence or some other reason, I honestly I have no idea.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.