Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

displaying data from MySQL on web page

Status
Not open for further replies.

arunrr

Programmer
Oct 2, 2009
103
US
I am doing the following in my main page but am not able to get the DB values to display. What am i missing? Thanks in advance...

<TR valign="middle" align="center">
<?php
mysql_connect("localhost","user1","password1");
mysql_select_db(user1_MYDB);
$table=survey_001;
$query="SELECT Choice FROM $table WHERE Choice='A'";
$result=mysql_query($query);
$numa=mysql_num_rows($result);
$query="SELECT Choice FROM $table WHERE Choice='B'";
$result=mysql_query($query);
$numb=mysql_num_rows($result);
mysql_close();
?>
<TD width="50%"><h3><?php echo $numa; ?></h3></TD>
<TD width="50%"><h3><?php echo $numb; ?></h3></TD>
</TR>
 
Hi

Excepting the ugly coding style I see no evident problem there.

Please make sure you have debugging turned on while testing:
Code:
[url=http://php.net/ini_set/]ini_set[/url]('[url=http://php.net/errorfunc.configuration/#ini.display-errors]display_errors[/url]','on');
[url=http://php.net/error_reporting/]error_reporting[/url]([url=http://php.net/errorfunc.constants/#errorfunc.constants.errorlevels.e-all]E_ALL[/url]);

Feherke.
 
Your problem is likely the query...

try replacing this:
Code:
 $query="SELECT Choice FROM $table WHERE Choice='A'";

with this:
Code:
 $query="SELECT Choice FROM ".$table." WHERE Choice='A'";

you might need to surround the table ref with single quotes, also:
Code:
 $query="SELECT Choice FROM '".$table."' WHERE Choice='A'";

---
Jason Kofoed
jasonkofoed.com

 
$table=survey_001;
As it is it should have been producing at least a
Notice: Use of undefined constant survey_001 - assumed 'survey_001' in page.php on line 5
You should be surrounding it with quotes:

$table="survey_001";

With that said all you would be getting displayed from your code is the number of rows returned from your queries.

If that is not happening perhaps adding some error checking to may yield something a bit more descriptive.
Code:
mysql_connect("localhost","user1","password1")[red]or die(mysql_error())[/red];;
     mysql_select_db(user1_MYDB)[red]or die(mysql_error())[/red];
     $table=survey_001;
     $query="SELECT Choice FROM $table WHERE Choice='A'";
     $result=mysql_query($query)[red]or die(mysql_error())[/red];;
     $numa=mysql_num_rows($result);
     $query="SELECT Choice FROM $table WHERE Choice='B'";
     $result=mysql_query($query)[red]or die(mysql_error())[/red];;
     $numb=mysql_num_rows($result);
     mysql_close();


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Failing the suggestions, you could:
Tell us where it looks to be failing or even better what is displayed /not displated e.g. you get 0 displayed when you expect a value.
echo the queries
echo the response codes from the executions
Standard questions woud be:
do other queries/php pages run ok?
does the DB exist ?
does the table exist ?
 
Thanks to all...

Phil, lol to your 'Unknown' statement. Good humor in an otherwise dry day!!

DB exists, table exists, etc. I was able to verify DB insertion from the 'mysql>' prompt.

Prob is now resolved.
Believe it or not, a minor error in the .htaccess entry.

Thanks again...
 
Glad you go a chuckle out of it. You got to love Win98's cryptic error messages.

And Glad you got it sorted out.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi

Now that it works, you should pay abit of attention to what Phil mentioned :
Phil said:
You should be surrounding it with quotes:

$table="survey_001";
That also applies for :
Code:
[COLOR=darkgoldenrod]mysql_select_db[/color][teal]([/teal][green][i][highlight]'[/highlight]user1_MYDB[highlight]'[/highlight][/i][/green][teal]);[/teal]
If all you are displaying is the count ( and your code looks so ), then better not request the entire list of Choices. If want their count, ask their [tt]count(*)[/tt] :
Code:
     [navy]$query[/navy][teal]=[/teal][green][i]"SELECT [highlight]count(*) count[/highlight] FROM $table WHERE Choice='A'"[/i][/green][teal];[/teal]
     [navy]$result[/navy][teal]=[/teal][COLOR=darkgoldenrod]mysql_query[/color][teal]([/teal][navy]$query[/navy][teal]);[/teal]
     [highlight][navy]$row[/navy][teal]=[/teal][COLOR=darkgoldenrod]mysql_fetch_assoc[/color][teal]([/teal][navy]$result[/navy][teal]);[/teal][/highlight]
     [navy]$numa[/navy][teal]=[/teal][highlight][navy]$row[/navy][teal][[/teal][green][i]'count'[/i][/green][teal]][/teal][/highlight][teal];[/teal]
     [navy]$query[/navy][teal]=[/teal][green][i]"SELECT [highlight]count(*) count[/highlight] FROM $table WHERE Choice='B'"[/i][/green][teal];[/teal]
     [navy]$result[/navy][teal]=[/teal][COLOR=darkgoldenrod]mysql_query[/color][teal]([/teal][navy]$query[/navy][teal]);[/teal]
     [highlight][navy]$row[/navy][teal]=[/teal][COLOR=darkgoldenrod]mysql_fetch_assoc[/color][teal]([/teal][navy]$result[/navy][teal]);[/teal][/highlight]
     [navy]$numb[/navy][teal]=[/teal][highlight][navy]$row[/navy][teal][[/teal][green][i]'count'[/i][/green][teal]][/teal][/highlight][teal];[/teal]
However my style is to write it like this :
Code:
     [navy]$query[/navy][teal]=[/teal][green][i]"select sum(case when choice='A' then 1 else 0 end) a,sum(case when choice='B' then 1 else 0 end) b from $table"[/i][/green][teal];[/teal]
     [navy]$result[/navy][teal]=[/teal][COLOR=darkgoldenrod]mysql_query[/color][teal]([/teal][navy]$query[/navy][teal]);[/teal]
     [navy]$row[/navy][teal]=[/teal][COLOR=darkgoldenrod]mysql_fetch_assoc[/color][teal]([/teal][navy]$result[/navy][teal]);[/teal]

[gray]/* ... */[/gray]

   [teal]<[/teal]TD width[teal]=[/teal][green][i]"50%"[/i][/green][teal]><[/teal]h3[teal]><?php[/teal] [b]echo[/b] [navy]$row[/navy][teal][[/teal][green][i]'a'[/i][/green][teal]];[/teal] [teal]?></[/teal]h3[teal]></[/teal]TD[teal]>[/teal]
   [teal]<[/teal]TD width[teal]=[/teal][green][i]"50%"[/i][/green][teal]><[/teal]h3[teal]><?php[/teal] [b]echo[/b] [navy]$row[/navy][teal][[/teal][green][i]'b'[/i][/green][teal]];[/teal] [teal]?></[/teal]h3[teal]></[/teal]TD[teal]>[/teal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top