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.
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.
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.
<?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
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.