Jump to content

mysql query by ID error (upgraded from php4 to php5)


DarkJamie

Recommended Posts

I'm hoping there is a simple solution for this problem. Yesterday I upgraded from PHP4 to PHP 5, as I want to use ImageMagick for my photo gallery. Now that I've upgraded, my blog queries only partially work. The following are two snippets of code querying the same db.

 

This is my list page. It displays a list of titles with entry dates. This page works perfectly, and displays everything correctly.

<?php
//Generate the query

$query = "SELECT id, title, content, author, UNIX_TIMESTAMP(entry_date) as date FROM news ORDER BY date DESC LIMIT 3";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

// get resultset as object
if (mysql_num_rows($result) > 0) {
	while($send = mysql_fetch_object($result)) {

	// print details

	echo "<a href='r.php?id=$send->id'><b class='style2'>$send->title</b></a> ";

		?>
		<font class="style3">
		<?php
		echo date("g:i A - F j, Y", $send->date +7200);
		?>
		</font><br>
		<?php

	echo "<font class='style7'><i>Written by: </i><b>$send->author</b> </font><br>";
	echo "<br>";
	echo nl2br($send->content);
	echo "<br><br>";

	}
}
?> 

 

This is my results page. When executed, this page returns no result and no error messages.

<?php
     include("includes/dark_db_access.php");
       db_login();

	//Generate the query to display news content
	$query = "SELECT title, content, author, UNIX_TIMESTAMP(entry_date) as date FROM news WHERE id = '$id'";
	$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

	// get resultset as object
	$send = mysql_fetch_object($result);

	// print details
	if($send) {
		echo "<b class='style2'>$send->title</b> ";
?>

				<font class="style3">
				<?php
				echo date("g:i A - F j, Y", $send->date +7200);
				?>
				</font>
				<?php
				echo "<br><i>Written by: </i><b>$send->author</b> ";
				echo "<br><br>";
				echo nl2br($send->content);
				echo "<br><br><font size=2 color=red><a href=my_sent.php>Main menu.</a></font>";
				}
				?>

 

Since both queries are written in the same syntax, and the php code to display them is the same, I can not figure out why the results page (by id) fails to display anything. Unfortunately, since I'm hosted with GoDaddy, their geniuses, in their infinite wisdom, thought it best that economy users not have access to error logs, so I can't check them. Altho my brain's logic keeps telling me that if the list page works, and the results page is written the same way, it should work too. Mind you, until I switched to php5 yesterday, all of my db queries were working perfectly.

 

Any ideas would be greatly appreciated.

Link to comment
Share on other sites

I see where you're going with that, but before the version change, I didn't need to set the $id value for it to work. I'm still trying to wrap my brain around that logic, but it worked fine. I was able to add entries, edit them, delete them, and list them in any fashion i wanted with just the code I had. Now I'm sure the version change shouldn't have made a difference, since nothing on my site is actually dependent on either version yet, but once I implemented the change, the add_entry, edit_entry, delete_entry and results by id all stopped working.

 

Having given thought to this logic, I went back and switched BACK to php4 and everything is working again, so I guess my question should be, with something as simple as a query by ID, how will I need to rework the code to work in php5?

Link to comment
Share on other sites

The problem is register_globals and has nothing to do with php5. Register_globals were turned OFF BY DEFAULT in April of the year 2002 (php 4.2) because they allow hackers to set session variables by simply putting GET parameters on the end of URL's when they visit a site. Register_globals being on finally generates a depreciated error in php5.3 and they have been completely removed in php6.

 

You need to correct your code so that it works for all php configurations by using the actual source of the external data. In your case $_GET['id']

 

Edit: All $_POST, $_GET, $_COOKIE, $_FILES, $_SESSION, $_SERVER, and $_ENV... variables that you code is expecting to be copied into program variables would need to be corrected. 

 

If you are using session variables with session_register(), session_is_registered(), or session_unregister() functions, you will need to make addition changes in your code in order to remove those functions.

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.