0

I was experimenting if I could use a mySQL database to store CSS settings. I set up a simple database "colors" with one table "color" that had simple structure tag and color columns. In that, one row is h1 => red.

<?php
//function to dynamically change CSS

    $tag = 'h1';
    $q = "SELECT * FROM `colors` WHERE `tag`='" . $tag . "'" ;
    echo $q . "<br>";
    $query = mysqli_query($link, $q);
    if ($row = mysqli_fetch_assoc($query))
    {
        echo $row['color'];
    } else
    {
        echo "error - no such tag";
    }
?>

When I tried to convert to a function, the code does not work at all.

<?php
//function to dynamically change CSS
function getCSS($tag)
{
    $tag = 'h1';
    $q = "SELECT * FROM `colors` WHERE `tag`='" . $tag . "'" ;
    echo $q . "<br>";
    $query = mysqli_query($link, $q);
    if ($row = mysqli_fetch_assoc($query))
    {
        echo $row['color'];
    } else
    {
        echo "error - no such tag";
    }
}
getCSS('h1');
?>

Help please?

2
  • 1
    remove $tag = 'h1' inside function body Commented Feb 24, 2016 at 13:44
  • Please print your output or error. Commented Feb 24, 2016 at 13:48

4 Answers 4

1

My guess is that in

$query = mysqli_query($link, $q);

$link goes out of scope and is empty. You should pass it to the function as well.

For the record: using $tag without escaping could be an sql injection attack possibility.

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

2 Comments

thank you. With regard to the SQL injection, I was just experimenting so not live. Appreciate the advice though.
Thats what i thought but on a site like this where everybody is learning ... functions get rewrapped a couple of times in the future ... and the lib goes live and voila... i completely stopped doing it. I recommend doing the same.
0

in function, there is no $link, you shoud define it as a global variable.

Comments

0

At the start of your function add a reference to your global DB link:

function getCSS($tag) {
    global $link;
    ...

1 Comment

thanks a lot. All responsive point to he same problem. I do appreciate your time.
0

This should work:

<?php

$link = mysqli_connect('server_host', 'user', 'password', 'database') OR die('Could not connect because: '.mysqli_connect_error());

//function to dynamically change CSS
function getCSS($link, $tag){

$q = 'SELECT * FROM colors WHERE tag = "' . $tag . '"' ;
$r = mysqli_query($link, $q);

if(mysqli_num_rows($r)>0){ // check if there are results

    while($row = mysqli_fetch_assoc($r)){
    //echo '<pre>';      
    //print_r($row);            // print the result array for debugging
    //echo '</pre>';

    echo $row['color'] . '<br />';

    }

    return $row;

} else {  // if no result is found

    echo 'No such tag';

}

}
// test it:
echo '<br />if tag is h1<br />';
getCSS($link, 'h1');

echo '<br />if tag is h2<br />';
getCSS($link, 'h2');

?>

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.