Re: var_dump() results

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Europus wrote:
Jochem Maas wrote:

side note: where does $table come from?

I didn't want to reveal the real names of my server/host, table,
login or password to the list, so I tried setting $host, $login,
$passwd, and $table variables at the top of the script. That way
it's 4 lines to omit when posting code, instead of parsing through
to edit each instance. That didn't work so I've since restored the
initial values, probably I stepped on a reserved word. I'll check
on that, I like the idea of setting those to variables elsewhere.

The reason your variables didn't work was that you had them enclosed in single quotes. Within single quotes, no variables are parsed. Two options to fix this. Use double quotes, or get rid of the quotes completely since you are using variables. The latter suggestion is probably the most preferred way of working with variables.

here is the code from your original email. I have modified it to work like one would expect it too.

<?php

# note:
# don't use *_pconnect().  I have found it to be a resource hog
# Remove single quotes around variables
# notice the or at the end of the first line
//connect to db
$link = mysql_connect($host, $login, $passwd) or
		die('Unable to connect : ' . mysql_error());

# note
# When working with boolean results & if conditions, just put the call
# in the if condition
# Remove single quotes around $table variable
# notice the or at the end of the first line
# I used curly brackets around my variable, just so the PHP parse will
# never get confused.
// make $table the current db
mysql_select_db($table, $link) or
	die ("Unable to select {$table}: " . mysql_error());

# I used curly brackets around my variable, just so the PHP parse will
# never get confused.
//get column data
$sql    = "SELECT column2, column3 FROM {$table}";


# notice the or at the end of the first line
$result = mysql_query($sql) or
	die('SQL Query Error['.mysql_errno($link).'] '.
				mysql_error($link));

# I have always like using while loops with my db retrieving, just seems
# to me to work better
# Keep looping through the result set, until mysql_fetch_assoc() returns
# false.  Once it returns false, the while loop will exit.
# Change to using *_fetch_assoc() this way it will only return to me the
# column named variables from the result set row.  Not the indexed names
//loop through to display results
while ( $row = mysql_fetch_assoc($result) ) {
	echo '<br /><br />'.print_r($row, 1);
}

?>

Give this a try and see what happens. Should do everything you want it to do and maybe more.

Jim


//get column data
    $sql    = "SELECT column2, column3 FROM $table";
    $result = mysql_query($sql);

if (!$result) {
    echo "query failed.\n";
} else {
    while ($row = mysql_fetch_array($result))
        var_dump($row);
}
    $row = mysql_fetch_row($result);

Now that, that change makes what looks to be 2100 rows of data flood
the screen, thank you. Now I can move on to processing that data
in the next step of my script.

It looks like, my take-away lesson is that I was invoking var_dump()
too late, I should have been performing that check right after running
mysql_fetch_array(), because later operations step on the data and
that's why I was getting unexpected returns.

Ulex


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux