Web Analytics Made Easy -
StatCounter if (mysql_num_rows($result)>0) displays code on html? - CodingForum

Announcement

Collapse
No announcement yet.

if (mysql_num_rows($result)>0) displays code on html?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • if (mysql_num_rows($result)>0) displays code on html?

    Hello,

    I am trying to display information from a DB onto an html page. Using this code,

    PHP Code:
    <?php
    .
    .
    .
    $connection mysql_connect($db_connection$db_user$db_pass)
    or die (
    "Unable to connect to SQL");
    // select DB
    mysql_select_db("my_db") or die ("Unable to connect to DB");
    //query test
    $query "SELECT * FROM hl_placement_programs";
    $result mysql_query($query$connection)
    or die (
    "Error in query: $query. " mysql_error());
    //check if records were returned
    if (mysql_num_rows($result) > 0)
    {

        echo 
    "<table width = 100% border = 1>";
        echo 
    "<tr><td><b>Name</b></td><td><b>Street</b></td><td>
    <b>City</b></td><td><b>State</b></td><td><b>Zipcode</b>
    </td><td><b>Phone Number</b></td><td><b>Website</b></td></tr>"
    ;

        while (
    $row mysql_fetch_row($result))
        {
            echo 
    "<tr>";
            echo 
    "<td>" $row[0] . "</td>";
            echo 
    "<td>" $row[1] . "</td>";
            echo 
    "<td>" $row[2] . "</td>";
            echo 
    "<td>" $row[3] . "</td>";
            echo 
    "<td>" $row[4] . "</td>";
            echo 
    "<td>" $row[5] . "</td>";
            echo 
    "<td>" $row[6] . "</td>";
            echo 
    "</tr>";
        }
        echo 
    "</table>";
    }
    else
    {
        
    //print error message
        
    echo "Oh dear. No rows were found!";
    }
    //once processing is complete
    //free result set
    mysql_free_result($result);
    //close connection
    mysql_close($connection);
    ?>
    the table displays fine, but none of the information shows up, and the following code is displayed on the html page:

    0) { echo ""; echo ""; while ($row = mysql_fetch_row($result)) { echo "";
    echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "

    then the table, with things like " . $row[0] . " as one cell in each column, and then below the table

    "; } else { //print error message echo "Oh dear. No rows were found!"; }
    //once processing is complete //free result set mysql_free_result($result);
    //close connection mysql_close($connection); ?>


    I've seen something like this before, but I don't know how to fix it. The hl_placement_programs is a table in my DB. I'm confused because it seems to be connecting to the DB, but can't get info? Please help; it's probably some tiny thing I messed up on.

    Thanks!

    Apologies if this goes in the MySQL section. It's in a PHP file, so I figured I'd put it here.
    Last edited by freefallenigma; Apr 7, 2009, 11:14 PM. Reason: fixed php code

  • #2
    the following code is displayed on the html page:

    Code:
    0) { echo ""; echo ""; while ($row = mysql_fetch_row($result)) { echo "";
    echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
    That means that PHP didn't parse it correctly if you see the PHP code on the html page...........

    Comment


    • #3
      Originally posted by sea4me View Post
      That means that PHP didn't parse it correctly if you see the PHP code on the html page...........
      Yeah, either that or we're looking at a string. This doesn't make sense though.
      Is what you have posted here everything thats within the HTML or is there more before and after it? The difference that this will show is whether you're page is being correctly processed and the error is elsewhere, or if the page is not being executed as PHP at all.
      As well, whats in those . . . at the beginning of the script? Post that, remove any configuration directives with *'s.

      Edit:
      Oh yes, I don't think this is qualified as an SQL problem. It appears to be a processing problem with PHP, so this is the correct forum.
      PHP Code:
      header('HTTP/1.1 420 Enhance Your Calm'); 
      Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

      Comment


      • #4
        html before:
        Code:
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 STRICT//EN" "http:/www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        	<head>
        		<title>Program Navigation Tool</title>
        		<meta http-equiv="Content-Type"
        			content="text/html;charset=utf-8" />
        			<link href = "blah.css" rel="stylesheet" type="text/css" />
        	</head>
        <body>
        after is a giant table. I do close the html tag, though.
        As for the ...'s,
        PHP Code:
        <?php
        // open connection to DB
        $db_connection webaddress;
        $db_user username;
        $db_pass password;
        does the db_connection need to have /phpmyadmin included? or anything?

        Comment


        • #5
          PHP Code:
          <?php 
          // open connection to DB 
          $db_connection "webaddress"
          $db_user "username"
          $db_pass "password";
          You need quotes around text

          Comment


          • #6
            Also post the whole page including the giant table....

            Comment


            • #7
              Agreed, the entire html source code is also what I was looking for. The entire script you can post as well, just make sure they are in separate code and php blocks.

              And no, you don't need PHPmyAdmin to use mysql.
              PHP Code:
              header('HTTP/1.1 420 Enhance Your Calm'); 
              Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

              Comment


              • #8
                well one stupid issue solved: save it as a php.
                now I cannot connect, but at least it's giving me the error.

                I mostly wanted to see if it could connect and retrieve info, as the table the php spits out isn't necessary for the site.

                here 'tis:
                Code:
                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 STRICT//EN" "http:/www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
                <html xmlns="http://www.w3.org/1999/xhtml">
                	<head>
                		<title>Screening Program Navigation Tool</title>
                		<meta http-equiv="Content-Type"
                			content="text/html;charset=utf-8" />
                			<link href = "hpl.css" rel="stylesheet" type="text/css" />
                	</head>
                <body>
                PHP Code:
                <?php
                // open connection to DB
                $db_server "address";
                $db_user "user";
                $db_pass "pass";
                $connection mysql_connect($db_server$db_user$db_pass)
                or die (
                "Unable to connect to SQL");
                // select DB
                mysql_select_db("mydb") or die ("Unable to connect to DB");
                //query test
                $query "SELECT * FROM hl_placement_programs";
                $result mysql_query($query$connection)
                or die (
                "Error in query: $query. " mysql_error());
                print_r($result);
                //once processing is complete
                //free result set
                mysql_free_result($result);
                //close connection
                mysql_close($connection);
                ?>
                Code:
                <p align="right"><img src="HLearn2.png"><img src="hfpl3.jpg">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="hpllogo_blue_150.jpg"></p>
                <h2>Welcome to the Tool</h2>
                <div id="body">
                <form name="form" action="html_form_submit.asp" method="get">
                <table>
                	<tr>
                		<td>
                			<h3>Personal Information</h3>
                		</td>
                	</tr>
                </table>
                <table>	
                	<tr>
                		<td>	
                			First name:
                		</td>
                		<td>	
                			<input type="text" name="firstname">
                		</td>
                	<tr>
                		<td>	
                			Last name:
                		</td>
                		<td>	
                			<input type="text" name="lastname">
                		</td>
                	</tr>		
                </table>
                <br>
                <table align="left">
                	<tr>
                		<td>
                			What is your home address?
                		</td>
                	</tr>		
                	<tr>
                		<td>
                			Street Number
                		</td>
                		<td>
                			 <input type="text" name="home_street">
                		</td>
                	</tr>
                	<tr>
                		<td>		
                			City
                		</td>
                		<td>	
                			<input type="text" name="home_city">
                		</td>
                	</tr>		
                	<tr>
                		<td>
                			State
                		</td>
                		<td>
                			<input type="text" name="home_state">
                		</td>
                	</tr>
                	<tr>
                		<td>
                			Zipcode
                		</td>
                		<td>
                			<input type="text" name="home_zipcode">
                		</td>
                	</tr>						
                </table>
                <table>
                	<tr>
                		<td>
                			What is your workplace address?
                		</td>
                	</tr>		
                		<td>
                			Street Number
                		</td>
                		<td>
                			 <input type="text" name="work_street">
                		</td>
                	</tr>
                	<tr>
                		<td>		
                			City
                		</td>
                		<td>	
                			<input type="text" name="work_city">
                		</td>
                	</tr>		
                	<tr>
                		<td>
                			State
                		</td>
                		<td>
                			<input type="text" name="work_state">
                		</td>
                	</tr>
                	<tr>
                		<td>
                			Zipcode
                		</td>
                		<td>
                			<input type="text" name="work_zipcode">
                		</td>
                	</tr>				
                </table>
                <br>
                <table>	
                	<tr>
                		<td>
                			Use
                		</td>
                		<td>
                			<select name="work_or_home">
                				<option>Home</option>
                				<option>Work</option>
                			</select>
                		</td>			
                		<td>	
                			address.
                		</td>
                	</tr>
                </table>
                <table>	
                	<tr>	
                		<td>
                			What is your phone number?
                		</td>
                		<td>	
                			<input type="text" name="phone">
                		</td>
                		<td>	
                			<input type="checkbox" name="phone" value="no_phone"> I do not have a 				phone number.
                		</td>
                	</tr>
                </table>			
                <br>
                <br>
                <table>
                	<tr>
                		<td>
                			<h3>Literacy Information</h3>
                		</td>
                	</tr>
                	<tr>
                		<td>		
                			Select the last year of school completed:
                		</td>
                	</tr>
                </table>
                <table>	
                	<tr>
                		<td>
                			Primary  
                			<input type="radio" name="school_level" value="1"> 1
                		</td>
                		<td>	
                			<input type="radio" name="school_level" value="2"> 2
                		</td>
                		<td>	
                			<input type="radio" name="school_level" value="3"> 3
                		</td>
                		<td>	
                			<input type="radio" name="school_level" value="4"> 4
                		</td>
                		<td>	
                			<input type="radio" name="school_level" value="5"> 5
                		</td>
                		<td>	
                			<input type="radio" name="school_level" value="6"> 6
                		</td>
                		<td>	
                			<input type="radio" name="school_level" value="7"> 7
                		</td>
                		<td>	
                			<input type="radio" name="school_level" value="8"> 8
                		</td>
                	</tr>
                	<tr>
                		<td>
                			Secondary
                		</td>
                		<td>
                			<input type="radio" name="school_level" value="9"> 9
                		</td>
                		<td>		
                			<input type="radio" name="school_level" value="10"> 10
                		</td>
                		<td>	
                			<input type="radio" name="school_level" value="11"> 11
                		</td>
                		<td>	
                			<input type="radio" name="school_level" value="12"> 12
                		</td>
                	</tr>
                	<tr>
                		<td>
                			University 
                		</td>	
                		<td> 
                			<input type="radio" name="school_level" value="1"> 13
                		</td>
                		<td>	
                			<input type="radio" name="school_level" value="1"> 14 
                		</td>
                		<td>	
                			<input type="radio" name="school_level" value="1"> 15 
                		</td>
                		<td>	
                			<input type="radio" name="school_level" value="1"> 16 
                		</td>
                	</tr>
                </table>
                <br>
                <table>	
                	<tr>
                		<td>		
                			Have you taken English/reading classes before?
                		</td>
                		<td>	
                			<input type="checkbox" name="studied" value="yes"> Yes
                		</td>
                		<td>	
                			<input type="checkbox" name="studied" value="no"> No
                		</td>
                	</tr>
                </table>
                <table>	
                	<tr>
                		<td>
                			Where?
                		</td>
                		<td>	
                			<input type="text" name="place_studied">
                		</td>
                		<td>	
                			For how long (months)?
                		</td>
                		<td>	
                			<input type="text" name="time_studied">
                		</td>
                	</tr>
                </table>
                <br>
                <table>	
                	<tr>
                		<td>
                			How long have you lived in the United States?
                		</td>
                	</tr>
                </table>
                <table>	
                	<tr>
                		<td>
                			<input type="radio" name="time_lived" value="less_6_mo"> Less than 6 months
                		</td>
                		<td>	
                			<input type="radio" name="time_lived" value="6_mo_2_yr"> 6 months - 2 years
                		</td>
                		<td>
                			<input type="radio" name="time_lived" value="2_5_yr"> 2 - 5 years
                		</td>
                		<td>	
                			<input type="radio" name="time_lived" value="more_5_yr"> More than 5 years
                		</td>
                	</tr>
                </table>
                <table>	
                	<tr>
                		<td>
                			What country are you from?
                		</td>
                		<td>	
                			<input type="text" name="country">
                		</td>
                		<td>		
                			What is your native language?
                		</td>
                		<td>	
                			<input type="text" name="language">
                		</td>
                	</tr>
                </table>
                <br>
                <table>
                	<tr>
                		<td>		
                			Understanding English is...
                		</td>
                		<td>	
                			<input type = "radio" name="understand_lvl" value="easy"> Easy
                		</td>
                		<td>	
                			<input type = "radio" name="understand_lvl" value="medium"> Medium
                		</td>
                		<td>	
                			<input type = "radio" name="understand_lvl" value="difficult"> Difficult
                		</td>
                	</tr>
                		<td>
                			Speaking English is...
                		</td>
                		<td>	
                			<input type = "radio" name="speak_lvl" value="easy"> Easy
                		</td>
                		<td>	
                			<input type = "radio" name="speak_lvl" value="medium"> Medium
                		</td>
                		<td>	
                			<input type = "radio" name="speak_lvl" value="difficult"> Difficult
                		</td>
                	</tr>
                	<tr>
                		<td>		
                			Reading English is...
                		</td>
                		<td>		
                			<input type = "radio" name="read_lvl" value="easy"> Easy
                		</td>
                		<td>	
                			<input type = "radio" name="read_lvl" value="medium"> Medium
                		</td>
                		<td>	
                			<input type = "radio" name="read_lvl" value="difficult"> Difficult
                		</td>
                	</tr>	
                	<tr>
                		<td>	
                			Writing English is...
                		</td>	
                		<td>
                			<input type = "radio" name="write_lvl" value="easy"> Easy
                		</td>
                		<td>	
                			<input type = "radio" name="write_lvl" value="medium"> Medium
                		</td>
                		<td>	
                			<input type = "radio" name="write_lvl" value="difficult"> Difficult
                		</td>
                	</tr>
                </table>	
                </form>
                <a href="part2.html">>Next</a>
                </div> <!--end body div-->
                <div id="footer">
                <p>Last Modified: 03/05/2009 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="mailto:[email protected]">Contact Webmaster</a></p>
                 
                </div> <!--end footer div-->
                </body>
                </html>
                Last edited by freefallenigma; Apr 8, 2009, 12:17 AM. Reason: personal info

                Comment


                • #9
                  Originally posted by freefallenigma View Post
                  well one stupid issue solved: save it as a php.
                  now I cannot connect, but at least it's giving me the error.
                  The incorrect extension was my initial thought as well.
                  So this is good to go then? The connection errors can be resolved by adding the mysql_error to the die in those as well as it should tell you what the problem is and you can go from there.
                  PHP Code:
                  header('HTTP/1.1 420 Enhance Your Calm'); 
                  Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

                  Comment


                  • #10
                    One option is that my vpn/internet/school setup is being screwy and ethernet will solve all (this has happened before).
                    However, I am getting an absolutely blank white page now. (not even blue, as the css asks) :/
                    Code:
                    $connection = mysql_connect($db_server, $db_user, $db_pass)
                    or die ("Unable to connect to SQL." mysql_error());
                    Thanks for helping me figure this out!

                    Comment


                    • #11
                      nevermind...

                      Unable to connect to SQL.Lost connection to MySQL server at 'reading initial communication packet', system error: 0

                      I'll get there soon...

                      Comment


                      • #12
                        haha yup. ethernet solved it all. oi.

                        Comment

                        Working...
                        X