Please help - Need alternative

Hi, I’m making a site which runs off joomla for the news section.

I’m trying to query the database to get an image for the current article, this image resides in it’s own column within the jos_content table called ‘articleimage’.

I’ve had this working on my testing server but when it’s live it just wont work and I really don’t understand why.

This is my code:


<?php
		// Make a MySQL Connection
		mysql_connect("server", "username", "password") or die(mysql_error());
		mysql_select_db("heritage") or die(mysql_error());
		
		$artid = JRequest::getVar('id');
		
		// Get all the data from the "jos_contnet" table
		$result = mysql_query('SELECT articleimage FROM jos_content WHERE (id="$artid") ORDER by rand() LIMIT 1') 
		or die('Error:'.mysql_error());  

		// keeps getting the next row until there are no more to get
		while($row = mysql_fetch_array($result)) {
			// Print out the contents of each row
			echo '<div class="news_image"><img src="';
			echo $row['articleimage'];
			echo '" /></div>';
		} 
	?> 	

(I’ve taken out passwords and the like).

Now it’s really strange that this shouldn’t work, as a few lines above I have this:

<?php
			// Make a MySQL Connection
			mysql_connect("server", "username", "password") or die(mysql_error());
			mysql_select_db("heritage") or die(mysql_error());

			// Get all the data from the "jos_contnet" table
			$result = mysql_query("SELECT id, title, introtext, created FROM jos_content WHERE (sectionid=1) ORDER by rand() LIMIT 3") 
			or die(mysql_error());  

			// keeps getting the next row until there are no more to get
			while($row = mysql_fetch_array($result)) {
				// Print out the contents of each row
				echo '<li><a href="http://atsserv01/phptest/Copy of ATS Heritage/News/index.php?option=com_content&id=';
				echo $row['id'];
				echo '" />';
				echo JHTML::_('date', $this->article->created, JText::_('DATE_FORMAT_LC1'));
				echo " - ";
				echo $row['title'];
				echo "</li>";
			} 

		?>

And this works fine.

Can anyone help me shed some light on why this isn’t working or possibly suggest an alternative method.

Thanks
James

Then your result set contains no rows. Your query doesn’t match anything in the table.

Please elaborate on “just won’t work”. That’s not the kind of problem description someone can easily help you with.

Do you get an error?
What is the exact error?
Do you get output but the wrong output?
Have you checked if $artid contains what you think it should contain?
That the query executes and returns the rows you think it should return?

No error, just nothing is output.
The rest of the page renders fine.

If I echo $artid I am given a number so that’s all working fine.

If I change the while statement to this:


while($row = mysql_fetch_array($result) or die 'Error:'.mysql_error())) {
			// Print out the contents of each row
			echo '<div class="news_image"><img src="';
			echo $row['articleimage'];
			echo '" /></div>';
		} 

I am thrown an error but it doesn’t give an error number.

:stuck_out_tongue:

…and that error would be?

No error number, it just says error

or die 'Error:'.mysql_error()))

I get the ‘Error:’ bit but no mysql error following it.

In Joomla, please put the error reporting level Maximum (Admin->Global Configuration->Server(Tab)->Error Reporting) for a moment and see if that shows some errors for you. Try running the query outside PHP (phpmyadmin) and see if that returns some records and are they what you’ve expected or not.

Setting Error reporting to maximum didn’t bring up anything else.

If I go into phpMyAdmin I can see that the correct url is in the correct column.

Now working using the following :


<?php
		// Make a MySQL Connection
		mysql_connect("host", "user", "password") or die(mysql_error());
		mysql_select_db("heritage") or die(mysql_error());
		
		$artid = JRequest::getVar('id');
		
		// Get all the data from the "jos_contnet" table
		$result = mysql_query("SELECT articleimage FROM jos_content WHERE (id='$artid') LIMIT 1") 
		or die('Error:'.mysql_error());  

		// keeps getting the next row until there are no more to get
		while($row = mysql_fetch_array($result)) {
			// Print out the contents of each row
			echo '<div class="news_image"><img src="';
			echo $row['articleimage'];
			echo '" /></div>';
		} 
	?>

All I did was swapped the use of single and double quotes

If id is numeric, you shouldn’t be using any kind of quotes. Single quotes enclose literal strings in SQL. The parentheses aren’t necessary either.

Then your result set contains no rows. Your query doesn’t match anything in the table.

No, one can’t say that. It will throw an error always, even if rows were found.
It’s just wrong place to place that code :slight_smile:

I’d suggest error_reporting(E_ALL) in case of some mistype.

He already turned up the error reporting level, and said nothing was output except the error, so this is the logical conclusion. If mysql_fetch_array returns false on the first call against a result set you haven’t otherwise touched, then it contained no rows. That is part of the definition of the return value of that function.

But you cannot tell if it was first call or not.
“mysql_fetch_array($result) or die ‘Error:’” will always throw an error.
That’s what I mean.

The reason is still unclear for me though, why it was not working and then started to work.
display_errors may be?

what I want to know is why it worked at all on the testing server