且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何启动PHP程序?

更新时间:2023-12-05 23:16:16

要执行PHP,您需要需要一个像Apache这样的web服务器,可以处理你的请求。



你可以看看 XAMPP (Windows,Mac和Linux), MAMP (仅适用于Mac)或 WAMP (仅限Windows)运行本地Web服务器。


So I'm writing a website that query's MySQL db and just returns what's in it in a table form. This being my first time using PHP (I have read tutorials but can't find one where it explains this part) and when i launch the query that uses php it just opens another browser window that shows the php code only. Not sure why its doing this, so thought maybe you guys can help.

Here is my HTML:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Insert URL</title>
    <style type = "text/css">
        label { width: 5em; float: left; }
    </style>
</head>
<body>
    <form method = "post" action = "db_query.php">
        <div><label>URL: </label>
            <input type="text" name="URL"><br></div>
        <div><label>Description: </label>
            <input type="text" name="Description"></div>
        <p><input type = "submit" value = "Query"></p>
    </form>
</body>

Here is the PHP:

<html>
<head>
  <meta charset = "utf-8">
  <title>Search Results</title>
<style type = "text/css">
     body  { font-family: sans-serif;
             background-color: lightyellow; } 
     table { background-color: lightblue; 
             border-collapse: collapse; 
             border: 1px solid gray; }
     td    { padding: 5px; }
     tr:nth-child(odd) {
             background-color: white; }
  </style>
 </head>
 <body>
  <?php

        $db_connect = mysql_connect("192.168.0.1", "db", "password");
        $db_query = "SELECT * FROM urltable";

        if (!$db_connect)
        {
            die("Could not connect to database");
        }
        else
        {
            print("Connected to the Database!");
        }

     ?><!-- end PHP script -->

     <table>
     <caption>Results of "SELECT <?php print( "$db_query" ) ?> 
        FROM URLS"</caption>
     <?php
        // fetch each record in result set
        while ( $row = mysql_fetch_row( $result ) )
        {
           // build table to display results
           print( "<tr>" );

           foreach ( $row as $value ) 
              print( "<td>$value</td>" );

           print( "</tr>" );
        } // end while
     ?><!-- end PHP script -->
  </table>

Ignore the two input boxes in the HTML code, they are just place holder for now. Anyways when I hit the Query button it just opens a new browser that shows the php code and not the actual query. I'm not sure what I'm doing wrong??

To execute PHP you are going to need a web server like Apache that will handle the requests for you.

You can take a look at XAMPP (Windows, Mac and Linux), MAMP (on Mac only) or WAMP (on Windows only) to run a local web server.