Welcome to Geeklog, Anonymous Saturday, December 21 2024 @ 11:56 am EST

Geeklog Forums

Problem with PHP/MySQL in PHP Blocks


Nathan Burke

Anonymous
confused
I'm trying to use PHP/MySQL to grab news headlines from another database and display them in a block. I've actually been able to get the data out, but on the display I get an error.

Here's my first code, which gets the right info, but the display doesn't work. I get my news items, but then get a SQL error, which stops the rest of the page from displaying:
Text Formatted Code

function phpblock_custom_getnews()
{
$db = mysql_connect("localhost", "******", "******");

mysql_select_db("nnews", $db);

$result = mysql_query("SELECT * FROM items WHERE date2<=curdate() ORDER BY date2 DESC, sort DESC LIMIT 5", $db);

while ($myrow = mysql_fetch_array($result))

        {
//Start the if statement- if it isn't a blog entry
        $blog=$myrow["blog"];
$author=$myrow["author"];      
$short_story=$myrow["short_story"];
if($blog=='No'){
       
$author=$myrow["author"];
echo "<a name='#".$myrow["ID"]."'></a><i><font size=2>".$myrow["its"]."</i><p>";
echo "<b><a href=".$myrow["link1"]." target='_hdl' style='color:#08305A'>".$myrow["headline"]."</b></a><br>";
if ($author<>""){
echo "<b>".$myrow["author"]."</b><br>";
}
echo "<font size=2><b>".$myrow["source"]." ".$myrow["date1"]."</b> ".$myrow["story"]." <a href=".$myrow["link1"].">...Full Story</a>";
        echo "<br><br>";
echo "<center><img src=/bulletins/sep.gif></center>";
}       }}

 

Using a code example from another post in the PHP Block forum, I tried this, which also won't work:
Text Formatted Code
function phpblock_custom_getnews()
mysql_select_db("nnews", $db);
{
    $result = DB_query("SELECT * FROM items WHERE date2<=curdate() AND items.blog='No' ORDER BY date2 DESC, sort DESC LIMIT 5");
    while ($A = DB_fetchArray($result)){
        $retval .= '<p>' . $A['headline'] . '</p>';
    }
    return $retval;
}
 


So, I'm trying to figure out what's going wrong here. Is there something I'm missing in the second example that allows you to get data from a different database?

The problem originally started because I was trying to use PHP/MySQL data in a code block and was told to make the block a PHP block. I did that, but now I can't get the data to display correctly.

Thanks so much.
-nathan
 Quote

Nathan Burke

Anonymous
The other idea I had was to use a PHP include, and have the file include the working PHP code, but that seems to break the whole thing too. Is there a way to do it this way? I have the PHP file that I know works....can I somehow just include that file in a block? That could solve my whole issue........
 Quote

Status: offline

Dirk

Site Admin
Admin
Registered: 01/12/02
Posts: 13073
Location:Stuttgart, Germany
First of all, don't use "echo" but return the content that is to be displayed in the block (as in your second example). The second piece of code is not valid PHP code, though, since it has the mysql_select_db call before the opening curly brackets for the function ...

I'd also suggest that you use Geeklog's database functions, so that you don't have to call mysql_select_db etc. any more (unless you're trying to access another database).

Well, and any SQL errors you may get from that code are obviously your problem, since it's your own code that's causing the error ...

bye, Dirk
 Quote

Nathan Burke

Anonymous
Dirk,

Since I still can't get this to work at all even though my php code works by itself outside of geeklog, I just have 2 questions:

1. I have to extract data from a different mysql database (NOT geeklog). Is there anything special I need to do to include this in a block? (I've included a line to specify the database I'm pulling from)

2. If I have a PHP file outside of geeklog that is grabbing what I want from the other database, is there any way I can just include it in a code block? I've tried:
function phpblock_custom_getnews() {
@include '/var/www/html/justnews3.php';
return $content;
}

And that gets the contents of the justnews3.php file, but then gives me an error and doesn't display the rest of the page. However, if I just look at the justnews3.php file itself, there are no problems with it.


Thanks again for your help.
 Quote

Nathan Burke

Anonymous
When I check the error log when I use the PHP include in lib-custom.php, I get:

Text Formatted Code
Wed 08 Feb 2006 15:22:49 EST - 1146: Table 'nnews.gl_pollvoters' doesn't exist. SQL in question: DELETE FROM gl_pollvoters WHERE date < unix_timestamp() - 604800
Wed 08 Feb 2006 15:23:59 EST - 1146: Table 'nnews.gl_userindex' doesn't exist. SQL in question: SELECT noboxes FROM gl_userindex WHERE uid = 2
 




So it seems like it's the fact that I'm accessing another database that is throwing this off.
 Quote

Status: offline

Dirk

Site Admin
Admin
Registered: 01/12/02
Posts: 13073
Location:Stuttgart, Germany
Yep, I was about to post the same thing. In your block, you're connecting to another database. Then, when Geeklog is done with your block, it will try to access its own database again, but it's now connected to the wrong database ...

As long as both databases are on the same MySQL server and are using the same user(name) and password, then doing mysql_select_db for Geeklog's database at the end of your code should work. Otherwise, things could get interesting ...

bye, Dirk
 Quote

All times are EST. The time is now 11:56 am.

  • Normal Topic
  • Sticky Topic
  • Locked Topic
  • New Post
  • Sticky Topic W/ New Post
  • Locked Topic W/ New Post
  •  View Anonymous Posts
  •  Able to post
  •  Filtered HTML Allowed
  •  Censored Content