-1

So I have these specific rows that I'm pulling if a code matches the database but I have no idea on how to echo this to my full html, is there anyway to make this $rows a $_POST or $_get to html?

thanks

 <?php
    $db_hostname = 'localhost';
    $db_database = 'codedb';
    $db_username = 'root';
    $db_password = '';
    $table = 'users';
    $field = 'code';
    $test = 'first_name';


    // Connect to server.
    $connection = mysql_connect($db_hostname, $db_username, $db_password) OR DIE ("Unable to 
    connect to database! Please try again later.");

    // Select the database.
    mysql_select_db($db_database,$connection)
        or die("Unable to select database: " . mysql_error());

    $query = "SELECT * FROM $table WHERE $field = '{$_GET["qcode"]}'";
    $result = mysql_query($query);
    if(mysql_num_rows($result) > 0) {
        while($row = mysql_fetch_array($result)) {
            $name = $row["$field"];
            $test = $row["$test"];
            echo "Hello: $name $test";
        }
    } else {
        echo "error msg";
    }
    mysql_close($connection);
?>  
Phil
  • 157,677
  • 23
  • 242
  • 245
  • 2
    Is this file separate from the HTML file? – AyB May 01 '14 at 05:22
  • Generally, it prints out the data as HTML. However, you can place them in any of your HTML codes in case you have these PHP codes in side HTML page – Tepken Vannkorn May 01 '14 at 05:22
  • [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Phil May 01 '14 at 05:25

3 Answers3

0

You just need to update your while loop

Following code is to create your result Array, by that result array you can use the values in HTML too.

$resArr = array();

while($row = mysql_fetch_array($result)) {
       $resArr[] = $row;
}
echo "<pre>";print_R($resArr);exit;
Keyur Mistry
  • 926
  • 6
  • 18
0

try

$query = "SELECT * FROM '$table' WHERE '$field' = '".$_GET["qcode"]."'";
        $result = mysql_query($query);
        if(mysql_num_rows($result) > 0) 
        { 
        while($row = mysql_fetch_assoc($result)) 
        {
           $name1 = $row[$field];
           $test1 = $row[$test];
           echo "Hello:" .$name1. $test1;
        }
        }
        else { echo "error msg"; }

Also use mysql_real_escape_string() to prevent sql injection or better to use mysqli or PDO

Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
0

You have two alternatives :

i) Use a .php file and write the html part there. This way you run php code with simple, php tags and display stuff where you need.

example : Create a file called test.php and put this code in it and run.

   </head>
   <body>
      <?php
          $db_hostname = 'localhost';
    $db_database = 'codedb';
    $db_username = 'root';
    $db_password = '';
    $table = 'users';
    $field = 'code';
    $test = 'first_name';


    // Connect to server.
    $connection = mysql_connect($db_hostname, $db_username, $db_password) OR DIE ("Unable to 
    connect to database! Please try again later.");

    // Select the database.
    mysql_select_db($db_database,$connection)
        or die("Unable to select database: " . mysql_error());

    $query = "SELECT * FROM $table WHERE $field = '{$_GET["qcode"]}'";
        $result = mysql_query($query);
        if(mysql_num_rows($result) > 0) 
        { 
               while($row = mysql_fetch_array($result)) {
                    $name = $row["$field"];
                    $test = $row["$test"];
                    echo "<p>".$name." ".$test."</p>";
               }
        }
        else { echo "error msg"; }
        mysql_close($connection);
      ?>
   </body>
</html>

This example puts the content in a paragraph in the html.

ii) echo the content from php by encoding it JSON and receive it using jquery from your html form. I'll not elaborate on this since it is not in the scope of the question.

And DO REMEMBER TO USE THE mysql_real_escape_string() to keep your code robust and prevent sql injection.

Priyabrata
  • 1,202
  • 3
  • 19
  • 57