2

MYSQL table structure: id , name , status , color

how can i use PHP to get some data from my database(mysql) ,the php script will look for table rows that have "good" in the "status" column. Then echo those rows which have good in status(P/S out into a JS file. It also get the color from each row (with status=good) and ptu it into javascript(jQUery)

I did a little research on how to use php to generate javascript.

Here's a php script, it's not working btw:

<?php
header("content-type: application/x-javascript");

  include 'connect.php';
  $sql = "SELECT * FROM users WHERE status ='good'"; 
  $query = mysql_query($sql)or die(mysql_error());

  $rows = array();
while($row = mysql_fetch_array( $query )){
  $rows[] = $row;
  echo "$('a[href*=\"row[username]\"]').css('color', 'row[color]');\n";
}

?>

The out put is (which wont work):

$('a[href*="row[username]"]').css('color', 'row[color]');

The connect.php is working(connects to database) , just the script is not working.

(BTW: The file name of this is usercolor.js.php , is it correct?) I hope someone could guide me.

Thanks and have a wonderful day.

3
  • 3
    just the script is not working. - in what way is it not working? No output, the javascript is invalid, etc etc - give us a clue! Commented Dec 1, 2011 at 10:04
  • 1
    Check the output of the script and add it to your question. Commented Dec 1, 2011 at 10:05
  • sorry about that , i've edited it Commented Dec 1, 2011 at 10:10

1 Answer 1

2
echo "$('a[href*=\"row[username]\"]').css('color', 'row[color]');\n";

won't work. Try

$username = $row['username'];
$color = $row['color']; 
echo "$('a[href*=\"$username\"]').css('color', '$color');\n";

or

echo "$('a[href*=\"{$row['username']}\"]').css('color', '{$row['color']}');\n";

see the manual for an explanation. Also, according to your mysql table structure, the column is called name and not username.

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

3 Comments

@roman there is a syntax error in your code. Hence my edit. Please refrain from rolling back to broken code.
@Treffynnon oh, I didn't rollback intentionally, we just edited at the same time. Thanks anyway for correcting the typo.
Thanks for helping me guys , selected as answer

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.