PHP从数据库中获取一些数据,然后将其回送到JS文件中

时间:2022-09-14 21:36:15

MYSQL table structure: id , name , status , color

MYSQL表结构: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)

我如何使用PHP从我的数据库(mysql)获取一些数据,php脚本将查找“状态”列中具有“好”的表行。然后回显那些具有良好状态的行(P / S输出到JS文件中。它还从每一行获得颜色(状态=好)并将其转换为javascript(jQUery)

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

我做了一些关于如何使用php生成javascript的研究。

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

这是一个PHP脚本,它不工作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.

connect.php正在工作(连接到数据库),只是脚本无法正常工作。

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

(顺便说一句:这个文件名是usercolor.js.php,是不是正确?)我希望有人可以指导我。

Thanks and have a wonderful day.

谢谢,祝你有个美好的一天。

1 个解决方案

#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.

请参阅手册以获得解释。此外,根据您的mysql表结构,该列称为名称而不是用户名。

#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.

请参阅手册以获得解释。此外,根据您的mysql表结构,该列称为名称而不是用户名。