help with retrieving database info

I can’t figure out why this doesn’t work.

“simple_table” consists of 4 fields: id, name, country, and cost. Can anyone suggest where the problem might be?? The database connection is correct… copied from another working php program. I’m sure it’s something very simple… just over looking it I guess.

[php]
// *********** start of database connection *************************
$db_hostname = “";
$db_username = "
”;
$db_password = “";
$database = "
”;

mysql_connect($db_hostname, $db_username, $db_password);
@mysql_select_db($database) or die(“Unable to select database”);
// *********** end of database connection **************************

$result = mysql_query(“SELECT * FROM simple_table”) or die(mysql_error());

while($row = mysql_fetch_array($result));
{
echo “inside while loop”; // for debugging… shows I’m in loop
echo $row[‘name’] . “
”;
}

mysql_close();
[/php]

What’s the error that you are getting?

The only thing that prints is,

inside while loop
. Nothing from the database is displayed. The table and field name s are correct.

How many rows are in the table and how many times does

"inside while loop"

prints?

Also try changing:

[php]echo $row[‘name’] . “
”;[/php]

to

[php]echo $row[“name”] . “
”;[/php]

Adding double quotes has no effect on the output.

"inside while loop"
appears only once.

My guess is that there is only 1 row in your database table and the name column is blank for that row.

No 170 records and all have names. I just can’t understand where my error is. Most of the time it’s simple mistakes like missing ; or ".

Any other ideas would be greatly appreciated.

You will get a lot better answers to your problem if you post the dump of your database.

Add this to the top of that php page

[php]<?php
error_reporting(E_ALL);
ini_set(“display_errors”, 1);[/php]

And let us know if anything changes.

Don’t know how to do what Kevin Rubio suggested.
I did add the code Topcoder suggested on lines 2 and 3, but got the same results…

inside while loop
was the only thing that displayed.

Not sure if this will help, but here is the MySQL table structure used in the problematic code and working code (at bottom of this post):

id_internet_sales -- int(6) payee -- varchar(75) email -- varchar(75) order_date -- varchar(50) order_date_no_time -- varchar(15) name -- varchar(75) address -- varchar(75) address2 -- varchar(50) city -- varchar(50) state -- varchar(2) zip -- varchar(10) country -- varchar(50) cost -- decimal(6,2) origination -- varchar(75) comments -- tinyblob date_stamp -- varchar(50)

This a copy of working code (except date order which I’m seeking help with on another post - date comparison and formatted output) that pulls the order_date_no_time, name, and cost from table - displayed in table format:
[php]
// database stuff ***********************************************
// assign database info to php variable
$db_hostname = “";
$db_username = "
”;
$db_password = “";
$database = "
”;

// connect to database
mysql_connect($db_hostname, $db_username, $db_password);
@mysql_select_db($database) or die(“Unable to select database”);
// ************************************************************************************

$startdate = $_POST[‘startdate’];
$month_s = $startdate[0];
$day_s = intval($startdate[1]);
$year_s = $startdate[2];

$enddate = $_POST[‘enddate’];
$month_e = $enddate[0];
$day_e = intval($enddate[1]);
$year_e = $enddate[2];

// functions are at top of code, but not included on forum to save space – they work
// converts number to text representation of month
$month_s = month_convert($month_s);
$month_e = month_convert($month_e);

$month = month_convert_long_name($startdate[0]); // use in report title

// recombine POST data
$space = " ";
$start_date_mix = $month_s . $space . $day_s . $space . $year_s;
$end_date_mix = $month_e . $space . $day_e . $space . $year_e;
echo " Range of dates: " . $start_date_mix . " - " . $end_date_mix . “

”;

// pull data from DB that’s between dates
$result = mysql_query(“SELECT * FROM internet_sales_dw WHERE order_date_no_time BETWEEN ‘$start_date_mix’ AND ‘$end_date_mix’ ORDER BY CAST(order_date_no_time AS SIGNED)”) or die(mysql_error());

$row = mysql_fetch_array($result);

$match = 0; // # matches
$total_sales = 0;
$count = 1; // # items

// set up table
echo “<table cellspacing=“1” cellpadding=“3”>

<td colspan=“6” bgcolor=”#999999" ><font color=“white”>" . $month . " Monthly Internet Sales Report
";
echo “<td align=“center”> # Order Date Name <td align=“right”>Charge ($) ”;

// check for march
do
{
echo “

<td align=“right” width=30>” . $count . "  ";
echo “” . $row[‘order_date_no_time’] . "  ";
echo “” . $row[‘name’] . "  ";
echo “<td width=90 align=“right”>” . $row[‘cost’] . “”;
$total_sales = $total_sales + $row['cost'];
$match++;   // used later to show a match was made 
$count++;	// number rows

if ($count == 60 || $count == 120)
{
	// deal with multipage table headers
}

} while($row = mysql_fetch_array($result)); // end DO-WHLE

if ($match == 0)
echo “

<td colspan=“6”>No matches.”;
else
{
echo “ <td colspan=“2” align=“right”>Total”;
echo “<td align=“right”>” . $total_sales . “
”;
}

mysql_close();

?>
[/php]

Thanks for all your time and help!

I’m at a loss on this one…

Maybe [member=43746]ErnieAlex[/member] [member=57551]fastsol[/member] [member=57087]Strider64[/member] [member=62829]scottlpool2003[/member] [member=44080]richei[/member] @astonecipher might have a suggestion.

Thanks Topcoder for all your help.

As I said about the “working” code previously, I have a post about comparing and formatting dates in chronological order. Could you, or someone else, offer any help with this?

http://www.phphelp.com/forum/beginners-learning-php/date-comparison-and-formatted-output/new/#new

First thing I see between this topic and the other is the issue of trying to select date values on a table with a varchar structure. That won’t work, cause it’s not actually comparing between real date formats but rather string values which are not the same in any respect. I don’t understand what the order_date_no_time table actually holds for values. But you need to change the order_date table to a datetime format so it can accurately determine things by real dates.

Also you’ll need to make sure that when you compare the dates with the new datetime format that you use a date string like yyyy-mm-dd or it won’t calculate properly either.

Thanks fastsol for the date help! No one responded on that thread.

Any idea why I can’t pull “name” from the table in the original problematic code? The reason I posted the 2nd code (date problem) was to show I could retrieve the “name” there but not in the 1st code.

Try this code and post back the printed output.

Also, never use the @ to suppress errors in your code, all it will do is make it harder for you to figure out why things are not working. So I have removed it from the mysql_select_db. I also switch to mysql_fetch_assoc instead of fetch_array, most people only need want the assoc version but neglect to set it up that way. With fetch_array you get an associative array AND number indices array, which typically is not needed.

The print_r($row); will display the array of $row that is being returned from the query, which you can then see if the “name” is coming across or not.

Lastly, there is no reason to use mysql_close(); in your script. PHP will close the connection automatically when the page finishes loading. It’s also a bad practice to use it cause if you did that in the middle of the page and you tried to run another query after it, it would fail cause there is not connection to the db any longer.
[php]
// *********** start of database connection *************************
$db_hostname = “";
$db_username = "
”;
$db_password = “";
$database = "
”;

mysql_connect($db_hostname, $db_username, $db_password);
mysql_select_db($database) or die(“Unable to select database”);
// *********** end of database connection **************************

$result = mysql_query(“SELECT * FROM simple_table”) or die(mysql_error());

while($row = mysql_fetch_assoc($result));
{
print_r($row);
}
[/php]

Nothing printed, only white screen. I added a line to echo some text and it did, so at least I knew the code was executed.

Oh duh, should have caught this earlier, but you don’t see it very often. Get rid of the ; at the end of the while loop line
[php]while($row = mysql_fetch_assoc($result));[/php]
The loop isn’t running. The echo is working cause it’s a direct echo, but the $row never has any values so it won’t show anything. I believe that certainly should have thrown an error for undefined index on $row. Do you have error_reporting turned on? Put this at the top of your pages during development to see the php errors.
[php]
ini_set(‘display_errors’,1);
error_reporting(E_ALL);
[/php]

[member=57551]fastsol[/member]

Ugh, I can’t believe I over-looked that also…

We all overlooked it. :o

YEA!!! Thank you isn’t enough, but that’s all I can offer each of you for the help!

Sponsor our Newsletter | Privacy Policy | Terms of Service