1

I am creating a library for PHP scripts and I want to be able to show php code on a html webpage.

I have looked at using highlight_file(); but this will show the whole page

For example, If I have a page called code.php which has an sql query on ( select code from table where sequence = $_GET["id"] ) - example then I use

Highlight_file('code.php?id=123');

This will work but will also show the select query which I do not want to show. I would just want to show the code from the database (code column)

How can I display just the code from the database with the correct colours and formatting etc

UPDATE:

<?php
$conn=mysql_connect("localhost","charlie_library","Pathfinder0287");
mysql_select_db("charlie_library",$conn);

function highlight_code_with_id($id, $conn)
{
    $query = "select * from library_php where sequence = '$id' ";
    $rs = mysql_query($query,$conn);
    $code  = mysql_fetch_array($rs);
    echo highlight_string($code["code"]);
}

// and, use it like this:
highlight_code_with_id($_GET['id'], $conn);
?>

I have tried the above code, which is just displaying the code in plain text

1
  • Show the whole code you have written in code.php Commented Dec 26, 2013 at 23:01

2 Answers 2

3

use highlight_string function, like this:

<?php
highlight_string($code);
?>

where $code is the code you have obtained from your SQL query.


You can create a function around this (something along the following lines):

<?php

function highlight_code_with_id($id, $mysqli) {
  $query = $mysqli->query("select code from table where sequence = '$id'");
  $code  = current($query->fetch_assoc());
  return highlight_string($code);
}

// and, use it like this:
echo highlight_code_with_id($_GET['id'], $mysqli);

UPDATE:

Your code is a bit incorrect, you can use:

<?php
$conn=mysql_connect("localhost","charlie_library","Pathfinder0287");
mysql_select_db("charlie_library",$conn);

function highlight_code_with_id($id)
{
    $query = "select * from library_php where sequence = '$id' ";
    $rs    = mysql_query($query);
    $code  = mysql_fetch_assoc($rs);      // change is in this line
    echo highlight_string($code["code"]);
}

// and, use it like this:
highlight_code_with_id($_GET['id']);
?>

Note that you do not need to include $conn in your function, it can be ommitted. Also, note that you should use mysqli->* family of functions, since mysql_* family has been deprecated.

Sign up to request clarification or add additional context in comments.

5 Comments

+1 even though mysql_query is deprecated and should not be used.
@Arjan: updated the code.. thank you for reminding me.. old habits die hard! :)
This is returning Array 1
i fixed the Array 1 issue, it now shows the code but not the way that highlight_file does (with the correct colouring etc) it just displays black text
@charlie: can you update your question to add the code you are using?
0

Perhaps this would work for you.

This post is originally for HTML, but the answer linked above shows an example using PHP.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.