Jump to content

[SOLVED] Quick Questions PHP


Ell20

Recommended Posts

I have tried to query the database in the normal way and for some reason its not working:

 

<?php 
header("Content-type: text/css");
require_once ('mysql_connect.php');
include_once ('includes/functions.php');
$id = $_SESSION['club_id'];

$query = "SELECT * FROM club WHERE club_id = '$id'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);

$bgcolour = $row['bgcolour'];
$textcolour = $row['textcolour'];
?>

body {
color: <?=$textcolour?>;
background-color: <?=$bgcolour?>;
}

 

Can you see anything wrong with this?

 

Thanks

Link to comment
Share on other sites

Should be:

<?php 
header("Content-type: text/css");
require_once ('mysql_connect.php');
include_once ('includes/functions.php');
$id = $_SESSION['club_id'];

$query = "SELECT * FROM club WHERE club_id = '$id'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);

$bgcolour = $row['bgcolour'];
$textcolour = $row['textcolour'];
?>
body {
color: <?php echo $textcolour; ?>;
background-color: <?php echo $bgcolour; ?>;
}

Link to comment
Share on other sites

Well, lets firstly make sure your query works before trying to use its result. This is always good practice.

 

<?php

session_start();

header("Content-type: text/css");
require_once ('mysql_connect.php');
include_once ('includes/functions.php');

$id = $_SESSION['club_id'];

$query = "SELECT * FROM club WHERE club_id = '$id' LIMIT 1";
if ($result = mysql_query($query)) {
  if (mysql_num_rows($result)) {
    $row = mysql_fetch_assoc($result);
    $bgcolour = $row['bgcolour'];
    $textcolour = $row['textcolour'];
  } else {
    echo "No record found";
  }
} else {
  echo Query failed " . mysql_error();
}
?>
body {
  color: <?php echo $textcolour; ?>;
  background-color: <?php echo $bgcolour; ?>;
}

 

I also noticed you where missing a call to session_start().

Link to comment
Share on other sites

That dosent work either, no message displayed either.

 

Is the possible reason for this that on the php page header.html is called, header.html then calls the CSS?

 

The reason there is no session_start() is because the header.html includes this, which is included on every page.

 

Thanks

Link to comment
Share on other sites

The reason there is no session_start() is because the header.html includes this, which is included on every page.

 

Is your server configured to handle html as php? By the way... what file extension is on the code you have posted? *.php ?

Link to comment
Share on other sites

The reason there is no session_start() is because the header.html includes this, which is included on every page.

 

Is your server configured to handle html as php? By the way... what file extension is on the code you have posted? *.php ?

 

I have no idea, before I attempted to make the changes to the CSS it was working fine.

 

The code I have posted is newcss.php.

 

Thanks

Link to comment
Share on other sites

thorpe missed a quote.

 

<?php

session_start();

header("Content-type: text/css");
require_once ('mysql_connect.php');
include_once ('includes/functions.php');

$id = $_SESSION['club_id'];

$query = "SELECT * FROM club WHERE club_id = '$id' LIMIT 1";
if ($result = mysql_query($query)) {
  if (mysql_num_rows($result)) {
    $row = mysql_fetch_assoc($result);
    $bgcolour = $row['bgcolour'];
    $textcolour = $row['textcolour'];
  } else {
    echo "No record found";
  }
} else {
  echo "Query failed " . mysql_error();
}
?>
body {
  color: <?php echo $textcolour; ?>;
  background-color: <?php echo $bgcolour; ?>;
}

Link to comment
Share on other sites

Thanks Ken, but still no luck.

 

To answer a previous question about the php being used in html.

 

I think it must work because when I set say $bgcolour = '#fffff';

 

Then use echo in the CSS it works, its just when I attempt to get a value from the database that its just going white with no CSS at all.

 

Thanks

Link to comment
Share on other sites

Well you just have to have the file extension .php.

 

You can use HTML in a .php file like so:

<?php
// woot! some php codes 
?>
<b> hi </b>

See? All I did was close out the PHP and just use HTML or CSS. As long as the file extension is php, it should be fine.

 

Link to comment
Share on other sites

If by not work, do you mean the variables are empty? Because I think that's the case. Can you page source the page and find that part of the CSS and copy and paste it here? In firefox, you just press Ctrl+U, in IE, you go to Edit and Page Source. (I think, not sure since I don't use IE)

Link to comment
Share on other sites

If by not work, do you mean the variables are empty? Because I think that's the case. Can you page source the page and find that part of the CSS and copy and paste it here? In firefox, you just press Ctrl+U, in IE, you go to Edit and Page Source. (I think, not sure since I don't use IE)

 

I dont see how the variables can be empty as I have manually placed them in the database.

 

CSS dosent show up in page source, all I can see is this:

<link media="screen" href="/includes/newcss.php" rel="stylesheet" type="text/css" />

 

Thanks

Link to comment
Share on other sites

I went to: /includes/newcss.php

 

And I found this:

 

<br />

<b>Warning</b>:  require_once(mysql_connect.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in <b>/home/mysport/public_html/includes/newcss.php</b> on line <b>3</b><br />

<br />

<b>Fatal error</b>:  require_once() [<a href='function.require'>function.require</a>]: Failed opening required 'mysql_connect.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in <b>/home/mysport/public_html/includes/newcss.php</b> on line <b>3</b><br />

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.