REALLY weird db problem

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

Moderator: General Moderators

JasonTC
Forum Commoner
Posts: 92
Joined: Wed Nov 02, 2005 11:05 am
Location: Grand Rapids, MI

REALLY weird db problem

Post by JasonTC »

I have a database with a table called email_addresses. This table has only one column: email. There are only two records in the table.

I'm performing the following statements:

Code: Select all

global $db_connection;
	$sql = "SELECT email FROM email_addresses;";
	$result = odbc_exec($db_connection, $sql);
	
	// send a message to each e-mail address in the table
	while ($row = odbc_fetch_row($result))
	{
		$email = $row['email'];
		echo "mail to: <b>\$email</b>: $email<br>"; // for debugging purposes
		send_message($email, $subject, $message); // special function I wrote
	}
There's more to my script but I don't think it should really matter. The output of my script is:

Code: Select all

mail to: $email:
mail to: $email:
Obviously, $row = odbc_fetch_row($result) is evaluated as true exactly twice, which is the number of records in my table. The weird thing is that no records are being displayed - I've double and triple-checked the name of the column "email", too. What's the deal?

Thanks,
Jason
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

note: this could be me being special..

did you mean fetch_row as opposed to fetch_row
??
JasonTC
Forum Commoner
Posts: 92
Joined: Wed Nov 02, 2005 11:05 am
Location: Grand Rapids, MI

Post by JasonTC »

did you mean fetch_row as opposed to fetch_row
Is that what you meant to put? ("fetch_row" = "fetch_row")
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

LMAO, i meant fetch_array instead of fetch_row. my mistake.
JasonTC
Forum Commoner
Posts: 92
Joined: Wed Nov 02, 2005 11:05 am
Location: Grand Rapids, MI

Post by JasonTC »

fetch_array yields no results whatsoever. Check out this weird and possibly related problem: viewtopic.php?t=41230
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

any chance of you using mysql?:-D

edit: or try using fetch_object and then change $row['email'] to $row->email
JasonTC
Forum Commoner
Posts: 92
Joined: Wed Nov 02, 2005 11:05 am
Location: Grand Rapids, MI

Post by JasonTC »

Not mysql, just regular or whatever. Microsoft Server, Access database.

fetch_object with $row->email also yielded no results.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

and there is information in the database for it to be pulling out??? :: scratching his chin::
JasonTC
Forum Commoner
Posts: 92
Joined: Wed Nov 02, 2005 11:05 am
Location: Grand Rapids, MI

Post by JasonTC »

I'm gonna end this here and post a new thing on the database forum.
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

Code: Select all

//add after the $result query:+
echo mysql_error();
echo"<pre>";
var_dump($result);

//add after the while($row.. stuff

var_dump($row);
see what happens :)
JasonTC
Forum Commoner
Posts: 92
Joined: Wed Nov 02, 2005 11:05 am
Location: Grand Rapids, MI

Post by JasonTC »

All right, well that gives me something, at least:

Code: Select all

resource(3) of type (odbc result)
But what does that mean?
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

JasonTC wrote:All right, well that gives me something, at least:

Code: Select all

resource(3) of type (odbc result)
But what does that mean?
OK so that means your database connection worked, and you have a result set from the db query -
what happens when you do the var_dump($row) ?

Code: Select all

while ($row = odbc_fetch_row($result))
    { 
        echo "<pre>";    
        var_dump($row);
.... your code continues...
if that gives you no data (not *EVEN* a NULL or array(0)) , that would mean your while() loop never executes


**** Ohhhh DUH.. on me . I didnt even NOTICE ***
I was about to say that you would want to look closely at your while() loop, so I did, and finally realized:

mysql_fetch_row() fetches a *SINGLE* row
you want either mysql_fetch_array($result) or mysql_fetch_assoc($result)

try either of those with the var_dump($row); stuff above and see what happens (or try all three fetch methods)
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

JasonTC wrote:I'm gonna end this here and post a new thing on the database forum.
No need. Moved!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

JasonTC wrote:

Code: Select all

// send a message to each e-mail address in the table
    while ($row = odbc_fetch_row($result))
    {
        $email = $row['email'];
        echo "mail to: <b>\$email</b>: $email<br>"; // for debugging purposes
        send_message($email, $subject, $message); // special function I wrote
    }
Have you tried removing the "" from in front of the "$email". I think if you escape the dollar sign the var will be treated as literal (or at least the dollar sign would be escaped rendering it as an actual "$" instead of your variable.

Just my $0.02.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

the first time u see $email he is just setting up to output the variable email is "insert e-mail"
or literally $email : "insert e-mail"
Post Reply