1

i am trying to use a php script in my html website. The php script is working as intended when I run it. My php script:

<?php
mysql_connect("localhost","demo","123abc");
mysql_select_db("demo");
$sql=mysql_query("SELECT vorname FROM users"); 
if(mysql_num_rows($sql)){ 
$select= '<select name="select">';   
while($rs=mysql_fetch_array($sql)){ 
      $select.='<option value='.$rs['vorname'].'>'.$rs['vorname'].'</option>'; 
  } 
} 
$select.='</select>'; 
echo $select;  
?>

Now what I want is, to have the dropdown on my website on different locations. My idea was to just insert the php script whenever I need it. My html part:

<td>
<?php include 'http://localhost/Testsamples/test.php'; ?>
</td>

But it is not working and I don't know why. Can someone help me out?

Thanks

5
  • The html file is in a different folder. Html= localhost/Prompt/index.html and then php= localhost/Testsamples/test.php Commented Jun 28, 2014 at 16:04
  • 1
    Apart from the answers below about the path, your php will probably not get executed if the including file has the .html extension. Change that to .php as well. Commented Jun 28, 2014 at 16:05
  • @jeroen it's possible to have HTML execute php, but it requires some adjustments in the system files. I have no clue on where he should look though, since I have no clue what local server he uses. However safer bet is indeed to just change html to php Commented Jun 28, 2014 at 16:10
  • 1
    @Dorvalla That's why I said probably. Modifying the OP's server settings is indeed a bit out of the scope of this question :-) Commented Jun 28, 2014 at 16:12
  • @jeroen your post was the solution in the end. Thanks a lot to all the help from everyone. =D Commented Jun 28, 2014 at 20:35

4 Answers 4

5

You don't include the file by its URL, you must use the system filepath:

<td>
<?php include '/path/to/Testsamples/test.php'; ?>
</td>
Sign up to request clarification or add additional context in comments.

4 Comments

I moved them in the same folder now. Its localhost/Prompt/test.php now. The html file is in the Prompt folder also. Do I just write /test.php as the path now?
You can do ./test.php or test.php
@LittleMonstr Change include to require, the error message will tell you what path php is looking for.
Do you have error reporting turned on PHP should be telling you if it can't find that file.
3

You can't include using URL (like http://www.domain.com/script.php).

Use relative path like :

<?php include "/path/test.php"; ?>

Use require is better than include, you will have a direct error if file is not found.

3 Comments

"/path/test.php" is an absolute path. What the OP had was a URL.
You should modify the wording a bit: As mentioned, the original path is a url and the path you are using is actually an absolute path.
You are right, I just named "absolute" too fast. I edit my answer.
1

In order to include it from an absolute path you need to have allow_url_include enabled on your php.ini.

See here: http://phpsec.org/projects/phpsecinfo/tests/allow_url_include.html

However, if your file is on the same system, you should use relative urls. i.e. include 'testSamples/test.php (or something similar). You could also use basic folder nav ( e.g. ../testSamples/test.php or use an include relative to root: /public_html/foo/bar/testSamples/test.php)

Comments

0

Try this

<?php /* At the first instance of call, to eliminate multiple opening of SQL connections for the smae data */
    ob_start();
    get_file_contents('http://localhost/Testsamples/test.php'); 
    $contents = ob_get_contents();
?>
<td>
    <?php echo $contents; ?>
</td>

edit - as pointed out in the other answers, you probably do not want to be including absolute paths!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.