user517593
user517593

Reputation: 2883

PHP: How to take info from MYSQL, make it a variable and place it in a link

Here's my code - not sure what the issue is?

// Make the query:
$query = "SELECT template FROM pages_meta WHERE user_id=$id";  

// RUN THE QUERY
$result = @mysqli_query ($dbc, $q);

$url = 'template';

echo "<link  href=\"$url\" type=\"text/css\" rel=\"stylesheet\" />";

Upvotes: 0

Views: 172

Answers (2)

Satya Prakash
Satya Prakash

Reputation: 3502

Yes, as said by Jakub, you need to get the data from Mysql and assign to $url.

Do something like this:

$query = "SELECT template FROM pages_meta WHERE user_id=$id";

if ($result = $mysqli->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        echo '<link  href="'. $row['template'] . '" type="text/css" rel="stylesheet" />';
    }

    /* free result set */
    $result->close();
}

For more: http://www.php.net/manual/en/mysqli-result.fetch-assoc.php

Upvotes: 1

izym
izym

Reputation: 143

There is in fact a few issues.

You set the variable $query, but use $q in mysqli_query(). By the way, you shouldn't be using @, but rather implement some sort of error handling.

Also, you set url to a string value, meaning that it will simply have the value template. I believe you should set it to something like $result[0]->template, although I'm not completely sure. Try putting the following line in your code and post what it writes out.

var_dump($result);

Upvotes: 1

Related Questions