muliple inserts

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

It returns 0

Code: Select all

function viewServices($category) {
	$query = "SELECT * FROM checkboxes_final WHERE cid='$cid' AND check_cat='$category'";
	$result = mysql_query($query) or die (mysql_error() . 'Cannot Retrieve Information');

	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		$check_text = $row['check_text'];
		echo $check_text;		
	}	
	return $check_text;
	echo mysql_num_rows($result);
	
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Once a function hits a return, it will terminate.. try putting the mysql_num_rows(..) before the return
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

Above "return $check_text;" is zero and in the loop returns nothing...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I didn't realize this, but you need to pass $cid to the function. This is whats causing your query to return 0 rows.. assuming there are actually rows in your table.

it helps to have error_reporting(E_ALL); to catch uninitialized variables.
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

muliple inserts - SOLVED

Post by psurrena »

HERE IT IS!

I use $cid=$_GET['cid'] in the beginning but as you said - I wasn't using it in the function. That was the first problem. The second problem was using reversing my use of semicolons and quotes in the SQL query. Now it works!

Thank you so much for all your help!

Code: Select all

<?php
	function viewServices($cid, $category) {
		
		$query = "SELECT check_text FROM checkboxes_final WHERE cid='$cid' AND check_cat='$category'";
		$result = mysql_query($query) or die (mysql_error() . 'Cannot Retrieve Information');

		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			$check_text = $row['check_text'];
		}	
		echo "Rows: " . mysql_num_rows($result);
		return $check_text;
}

$final = viewServices($cid, 'membership_database');
echo $final;
	
	echo '</ul>';
?>
Post Reply