Jump to content

How to refresh a bd query


sirio

Recommended Posts

Hi all.

 

In the main page i've got a block which shows the x last posts. The problem is that if you want to check for new posts, you have to refresh the whole page. But I wanted to refresh ONLY that part using AJAX, by using a "Refresh" button.

 

How could i do it? is it very difficult?

Link to comment
https://forums.phpfreaks.com/topic/38046-how-to-refresh-a-bd-query/
Share on other sites

Well, I've already do it, but i have a problem: I have two files:

 

block-Forums.php:

 

if( eregi( "block-Foros.php",$PHP_SELF ) )
{
Header("Location: index.php");
die();
}

global $prefix, $user_prefix,  $db;

// Ajax function
$content  .= "
<script language=\"javascript\" type=\"text/javascript\">
function getPage(Last_New_Topics){
var xmlhttp=false; //Clear our fetching variable
        try {
                xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e) {
                try {
                        xmlhttp = new
                        ActiveXObject('Microsoft.XMLHTTP');
            } catch (E) {
                xmlhttp = false;
                        }
        }
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                xmlhttp = new XMLHttpRequest();
        }
        var file = 'test/1/text.php?n=';
    xmlhttp.open('GET', file + Last_New_Topics, true);
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) {
                var content = xmlhttp.responseText;
                if( content ){
                      document.getElementById('content').innerHTML = content;
                }
        }
        }
        xmlhttp.send(null)
return;
}
</script>";

[...]

 

and text.php:

 

<?php
mysql_connect("localhost", "root", ""); 
mysql_select_db("lq");
$n = $_GET["n"];

$result = mysql_query("SELECT t.topic_id, t.topic_first_post_id, t.forum_id, t.topic_last_post_id,
	t.topic_title, t.topic_poster, t.topic_views, t.topic_replies, t.topic_moved_id, p.poster_id,
	p.post_time, u.username, l.username, u.user_id, tx.post_text, p.post_username, f.post_username
	FROM nuke_bbtopics AS t
	LEFT JOIN nuke_bbposts AS p ON (p.post_id = t.topic_last_post_id)
	LEFT JOIN nuke_bbposts AS f ON (f.post_id = t.topic_first_post_id)
	LEFT JOIN nuke_bbposts_text AS tx ON (p.post_id = tx.post_id)
	LEFT JOIN nuke_users AS u ON (u.user_id=p.poster_id)
	LEFT JOIN nuke_users AS l ON (l.user_id=t.topic_poster)
	WHERE t.topic_moved_id=0
	ORDER BY t.topic_last_post_id DESC LIMIT 0, $n
	");
while( list( $topic_id, $postf_id, $forum_id, $topic_last_post_id, $topic_title, $topic_poster, $topic_views, $topic_replies, $topic_moved_id, $poster_id, $post_time, $post1, $post2, $user_id, $post_text, $guest_name, $guest_name2) = $row = mysql_fetch_row($result))
{
[...]

 

In the second file, I have to make a connection, but i want to make it through the "$db" variable, to avoid these lines:

mysql_connect("localhost", "root", ""); 
mysql_select_db("lq");

 

How could i do it?

No, i mean through a link button ("click here to refresh"). I've already done that part, but the problem now is the connection.

 

In the 2nd file, i have this:

 

mysql_connect("localhost", "root", "");

mysql_select_db("lq");

 

But this is an unsecure method to connect.

I've tried to pass the variable $db to the 2nd file using GET method, but my browser doesn't show me anything.

I also tried to pass another variable ($prefix) to the second file and I didn't have any problem.

 

How could i pass the $db variable???

 

(sorry if you don't understand me but i'm spanish)

Well, i think i've solved the problem.

 

I've added these lines at the beginning of the 2nd file:

 

require("../config.php");
require("../includes/sql_layer.php");
$db = sql_connect($dbhost, $dbuname, $dbpass, $dbname);

 

And I've deleted these other lines:

 

mysql_connect("localhost", "root", "");
mysql_select_db("lq");

 

Perfect!!!!  ;D

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.