Page 1 of 1

Simple query giving much frustration

Posted: Sat Sep 06, 2003 3:55 pm
by URWhatUXpress
You would think that a simple query to pull email addreses from a table would not be giving me such a hassle, but I have no idea what is going wrong.

Here is the code so far:

Code: Select all

if (isset($mails)) //if form has been filled out
				{
					$connection = mysql_connect("$hostName", "$username", "$password") or die("Failure to communicate with database");
					mysql_select_db("$dbase", $connection);
					
					//run query for all email addresses
					$result = mysql_query("SELECT 'email' FROM hope", $connection);
					
					$contacts = "";
					
					while ($row = mysql_fetch_row($result));
						{
							for ($i=0; $i<mysql_num_fields($result); $i++)
								$contacts .= "$row&#1111;$i]";
								$contacts .=", ";
						&#125;		
						
				&#125;
The table has two entries, and spelling is correct with everything. I just have no clue about what is happening.

If it helps, when I echo $contacts I get the comma and a space; when I echo mysql_num_fields($result) I get 1.

Thanks for any ideas and suggestions.

{ d }

Re: Simple query giving much frustration

Posted: Sun Sep 07, 2003 3:23 am
by JAM
Would changing your while loop to this help?

Code: Select all

while ($row = mysql_fetch_array($result)) {
        $contacts .= $row[0];
        $contacts .=", ";
    }

Re: Simple query giving much frustration

Posted: Sun Sep 07, 2003 1:46 pm
by m3rajk
URWhatUXpress wrote:You would think that a simple query to pull email addreses from a table would not be giving me such a hassle, but I have no idea what is going wrong.

Here is the code so far:

Code: Select all

if (isset($mails)) //if form has been filled out
				&#123;
					$connection = mysql_connect("$hostName", "$username", "$password") or die("Failure to communicate with database");
					mysql_select_db("$dbase", $connection);
					
					//run query for all email addresses
					$result = mysql_query("SELECT 'email' FROM hope", $connection);
					
					$contacts = "";
					
					while ($row = mysql_fetch_row($result));
						&#123;
							for ($i=0; $i<mysql_num_fields($result); $i++)
								$contacts .= "$row&#1111;$i]";
								$contacts .=", ";
						&#125;		
						
				&#125;
The table has two entries, and spelling is correct with everything. I just have no clue about what is happening.

If it helps, when I echo $contacts I get the comma and a space; when I echo mysql_num_fields($result) I get 1.

Thanks for any ideas and suggestions.

{ d }
are you sure you're getting the db you want?
my the db choice in an if statement
if you get it blah blah blah else error. the mysql_select_db("$dbase", $connection); returns 0 if there's an error and 1 if you get it

Posted: Mon Sep 08, 2003 5:06 am
by twigletmac
A bit of a code rewrite with some comments (based in part on the suggestions by JAM and m3rajk):

Code: Select all

<?php

if (isset($mails)) {

	// don't surround variable names with quotes, use $username 
	// not "$username"
	$connection = mysql_connect($hostName, $username, $password) or die('Failure to communicate with database');
	mysql_select_db($dbase, $connection) or die(mysql_error());

	// be careful not to use single quotes to surround field names, you
	// need to use backticks (`) instead, but they are not strictly
	// necessary
	$sql = "SELECT email FROM hope";

	$result = mysql_query($sql, $connection) or die(mysql_error().'<p>'.$sql.'</p>');

	// to test the number of results from a query use mysql_num_rows()
	// not mysql_num_fields() - mysql_num_fields() only tells you how
	// many columns there are (in this case 1, the email column) not how
	// many rows of results there are.
	if (mysql_num_rows($result) > 0) {
			
		// there should not have been a semi-colon after the while loop 
		// condition
		while ($row = mysql_fetch_assoc($result)) {

			// try putting the e-mails into an array to make it easier
			// to separate them with commas later

			// don't surround variable names with quotes, do $row['email']
			// instead of "$row[email]"

			$contacts[] = $row['email'];
		}
		
		// now you can implode() the contact array to get a comma
		// separated list of contacts:
		$contact_list = implode(', ', $contacts);
	} else {

		// if there are no contacts returned then the contact list
		// will be empty
		$contact_list = '';
	}
}

?>
Mac