Simple query giving much frustration

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
URWhatUXpress
Forum Newbie
Posts: 11
Joined: Sat Aug 30, 2003 5:00 pm
Location: Grand Rapids, MI

Simple query giving much frustration

Post 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 }
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: Simple query giving much frustration

Post by JAM »

Would changing your while loop to this help?

Code: Select all

while ($row = mysql_fetch_array($result)) {
        $contacts .= $row[0];
        $contacts .=", ";
    }
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Re: Simple query giving much frustration

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply