0

I am trying to create a HMTL select box which populates from a table in a SQL database.

This is my test code:

<head>
</head>
<body>
<?php
mysql_connect('localhost', 'root', 'apassword');
mysql_select_db('webforms');

$sql = "SELECT site FROM sites_rcs";
$result = mysql_query($sql);

echo "<select name='sub1'>";
while ($row = mysql_fetch_array($result)) {
    echo "<option value '" . $row['site'] ."'>" . $row['site'] ."</option>";
    }
echo "</select>"            

</body>

When I open this page in a web browser I get this result:

"; while ($row = mysql_fetch_array($result)) { echo "" . $row['site'] .""; } echo ""

I have tried rewriting the code from various examples provided on the internet: https://www.youtube.com/watch?v=IVl5GPpMJsY http://www.yourwebskills.com/mysqldropdown.php

When I run the query SELECT site FROM sites_rcs in phpMyAdmin it returns the values in a list.

Any ideas?

D Martin
  • 23
  • 3
  • 1
    There is not closing tag for `` before `

    `. and change `echo "" ` to `echo "";`

    – Adam Azad Nov 11 '15 at 10:35
  • 2
    Multiple syntax errors... Missing closing `;` in your `echo ""` and you aren't closing your php either, add `?>` after the last echo. – Epodax Nov 11 '15 at 10:35
  • 3
    Please dont use the `mysql_` database extensions, they are deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is why](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Nov 11 '15 at 10:36
  • one you are missing a semicolon after this `echo ""`, also close the php code `?>` and finaly maybe you are trying to use mysql_fetch_assoc for associative array result. – Mujnoi Gyula Tamas Nov 11 '15 at 10:39
  • If the file extension on this script is not `.php` then change it to `.php` and apache will send the php code to the php interpreter for compile and execution – RiggsFolly Nov 11 '15 at 10:40
  • @RiggsFolly, we need a campaign for this! The problem is `mysql` is taking over the all web development tutorials. Sites provides tutorials has outdated articles, and at `mysql`era. The biggest problem is that newbies who tend using free hosting solutions which don't provide `mysqli` (there are few tho) – Adam Azad Nov 11 '15 at 10:40
  • @AdamAzad Agreed. It will probably take years or at least until PHP7 is well bedded in and the default option on all standard hosting packages. – RiggsFolly Nov 11 '15 at 10:44
  • Hi guys. Thanks for all your responses. It was the issue that it was a .html file and not .php I had being misinformed when I was told by a colleague that you can run php code inside a html file. The syntax issues have also been fixed and it is now workin as expected. I will look into learning the new mysqli/PDO commands over the deprecated commands. – D Martin Nov 11 '15 at 14:52

2 Answers2

0

Is PHP installed on the web server? Looks like the server just responds with the contents of your php file. Have a look at the page source.

andrrs
  • 2,289
  • 3
  • 17
  • 25
0

1- Make sure you have a server with php installed. 2- Make sure your file is .php not .html 3- Make sure your server is running 4- If testing on a local machine, make sure you're running the file through the server and not by double clicking it

Ralph Melhem
  • 767
  • 5
  • 12