Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

While loop not working

Status
Not open for further replies.

brentnewbury

Programmer
May 1, 2001
30
GB
Hi all,

I have a script that is ment to output, all comments relating to a news article, to the browser. The problem is; it only outputs the first record.

I have my database stuff in a class. I don't believe it is that to be causing the problem.

Here is my script:

-----
Code:
$comments = $DB_connect->query("SELECT user_id,comments,comments_timestamp FROM comments WHERE news_id='".$news_id."'");
while ( $comment = $DB_connect->fetch_array($comments) ) {
			
	/* put info into readable variables */
	$user_id = $comment['user_id'];
	$comment = $comment['comments'];
			
	/* get user information, based on user_id */
	get_user_info($comment['user_id']);
			
	/* get the website information and settings */
	get_site_info("comments");
			
	/*output the comment to the browser */
	eval("do_output(\"".get_template("comments",1,1)."\",1);");
}
-----

(NOTE: the [tt]do_output[/tt] function just parses the HTML (which is also retrieved from the database) and then sends it to the browser)

Thank you in advance,

Brent Newbury
 
I got an error:
[tt]PHP Warning: Supplied argument is not a valid MySQL result resource in ***********\global.php on line 103[/tt]

After my 8 months in scripting in PHP, I still don't know what the heck that means or how to fix it.
 
That means that the query did not return any results, or that it isn't set as a MySQL result. Does the query function return a MySQL result resource? //Daniel
 
Would you like me to email you the class and function scripts???
 
I have just finished the class, and it works, it all works.

It is going to be the class for a new product to come out called 'Adv.News'.

This is copyrighted material, so please change a few things : )

Code:
<?php
/*****************************************************
 *                                                   *
 *                     Adv.News                      *
 *                                                   *
 *               Version: 1.0.0 (alpha)              *
 *            Filename: (admin/)db_mysql.php         *
 *                                                   *
 *              Created By: *************            *
 *           Modified On: **th ******* 2002          *
 *         Copyright(c), 2002. Advance-Online        *
 *                                                   *
 *****************************************************/
 
/*****************************************************
 *
 *  NOTICE:
 *
 *  This script contains the class that will be used
 *  to connect, and interface, with a MySQL database.
 *  It is used throughout this site. Please do NOT 
 *  edit it unless you know what you are doing.
 *
 *****************************************************/

class db_connect {
	
	/* database vars */
	var $server = 	&quot;&quot;;
	var $username = &quot;&quot;;
	var $password = &quot;&quot;;
	var $database = &quot;&quot;;
	
	var $use_pconnect = &quot;&quot;;
	
	/* class vars */
	var $connect_id =  &quot;&quot;;
	var $resource_id = &quot;&quot;;
	var $record = array();

	/* class constructor */
	function connect($server = &quot;localhost&quot;,$username = &quot;&quot;,$password = &quot;&quot;,$database,$user_pconnect = &quot;1&quot;) {
		global $use_pconnect;
		
		if ( $use_pconnect == &quot;1&quot; ) {
			if ( $password == &quot;&quot; ) {
				$this->connect_id = mysql_pconnect($this->server,$this->username);
			} else {
				$this->connect_id = mysql_pconnect($this->server,$this->username,$this->password);
			}
		} else {
			if ( $password == &quot;&quot; ) {
				$this->connect_id = mysql_connect($this->server,$this->username);
			} else {
				$this->connect_id = mysql_connect($this->server,$this->username,$this->password);
			}
		}
		$this->select_database($database);
    	}

    	function select_database($database = &quot;-1&quot;) {
    		if ( $database != &quot;-1&quot; ) {
    			$this->database = $database;
    		}
        	mysql_select_db($this->database,$this->connect_id);
	}

	/* set and execute a query */
	function query($query_string) {
		if ( isset($query_string) ) {
	        	$this->resource_id = mysql_query($query_string,$this->connect_id);
	    	}
	    	return $this->resource_id;
	}

	/* fetch result array */
	function fetch_array($resource_id) {
		if ( isset($resource_id) ) {
			$this->resource_id = $resource_id;
		}
		if ( isset($this->resource_id) ) {
			$this->record = mysql_fetch_array($this->resource_id);
		}
		return $this->record;
	}

	/* does a query and returns the first row */
	function query_first($query_string) {
		if ( isset($query_string) ) {
			$resource_id = $this->query($query_string);
    			$this->record = $this->fetch_array($resource_id);
			
    			/* this query uses alot of memery space, execute function 'free_result()' */
			$this->free_result($this->resource_id);
		}
		return $this->record;
	}

	/* checks for the existance of data in a table */
	function check_exist($data_field, $table, $where_clause) {
		if ( isset($data_field) && isset($where_clause) && isset($table) ) {
			$check_exist = $this->query_first(&quot;SELECT &quot;.$data_field.&quot; FROM &quot;.$table.&quot; WHERE &quot;.$where_clause.&quot;&quot;,$this->resource_id);
			if ( $check_exist == &quot;&quot; ) {
				$check_exist = &quot;FALSE&quot;;
			} else {
				$check_exist = &quot;TRUE&quot;;
			}
			/* this function need to return a result, not a query, so free result */
			$this->free_result($this->resource_id);
		}
    		return $check_exist;
	}

	/* free's the memory allocated to result */
	function free_result($resource_id=-1) {
		return @mysql_free_result($this->resource_id);
	}

	/* close connection (mock destructor) */
	function close() {
		mysql_close($this->connect_id);
    	}
}
?>
 
Is that your global.php file or another file?

Also, add the following after your mysql_query line in your original post:

$numrecs = mysql_num_rows($comments);
echo &quot;Records: $numrecs&quot;;

making your top lines look like:
$comments = $DB_connect->query(&quot;SELECT user_id,comments,comments_timestamp FROM comments WHERE news_id='$news_id'&quot;);
$numrecs = mysql_num_rows($comments);
echo &quot;Records: $numrecs&quot;;

And see if you are at least getting some records returned from your query.
 
Thnx but...

I am making a news braodcasting and administraion program, that will be free for personal & educational use (anyone with a domain name will have to pay a small free around $15 for a two year license).

Your suggestion would dramaticly slow down the proccessing time (and the server admins (the hosting company) would not like thier bandwidth being sapped!) :)

But it is a good suggestion. I could pass a variable to do this, if needed.

Remember to edit the class, or it will be a breach of Copyright laws (both in the US and the UK (where I live)).

Thnx again for your contribution.

Brent Newbury,
(Project Leader, Advance-Online)
 
My suggestion was a temporary suggestion to assist you in resolving your current problem, not as a permanent addition to your code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top