数据库中的颜色

时间:2015-04-20 17:02:57

标签: php mysql

我希望用户可以更改其用户名的颜色。我想在数据库中写这些颜色。所以我的输入代码是消息,但颜色不起作用,有什么不对?

我在db中创建了一个列。

聊天工作正常,但此代码的颜色不起作用,此删除旧邮件的代码也不起作用。

编辑:哦,我在错误的表格中创建了一个列颜色。

   <?php
session_start();
$uname = $_SESSION['username'];
$msg = $_REQUEST['msg'];

$con = mysql_connect('fdb13.biz.nf','18213213wqeqwed_chat','mewqe132131dddd1');
mysql_select_db('1846946_chat',$con);

mysql_query("INSERT INTO logs (username,msg) VALUES ('$uname','$msg')");

$result = mysql_query("SELECT * FROM logs ORDER by id DESC");
$resultc = mysql_query("SELECT * FROM korisnici ORDER by color DESC");

while(($extract = mysql_fetch_array($result))||($extractc = mysql_fetch_array($resultc)) ){
        echo "<span style='color:" . $extractc['color'] . ";' class='uname'>" . $extract['username'] . "</span> <span class='msg'>" . $extract['msg'] . "</span><br>";
    }
?>
$result2 = mysql_query("SELECT * FROM logs username");
if($result2==3){
mysql_query("DELETE FROM logs");
      }
?>

1 个答案:

答案 0 :(得分:0)

您的OR声明中不能包含||while)条件。它只会遍历2个结果查询中的一个,而不是两个。您可以创建2个单独的while循环,并将结果查询保存在数组中:

$result = mysql_query("SELECT * FROM logs ORDER by id DESC");
$resultc = mysql_query("SELECT * FROM korisnici ORDER by color DESC");

$resultArray = [];
$resultcArray = [];

// Put your results in an array
while( $extract = mysql_fetch_array($result) ){
     $resultArray[] = $extract;
}
while( $extractc = mysql_fetch_array($resultc) ){
     $resultcArray[] = $extract;
}

// Now loop over (one of) the arrays
for( $i = 0; $i < $resultArray; $i++ ) {
     echo "<span style='color:" . $resultcArray[$i]['color'] . ";' class='uname'>" . $resultArray[$i]['username'] . "</span> <span class='msg'>" . $resultArray[$i]['msg'] . "</span><br>";
}

然而,这很可能不会给你想要的结果,因为数组不会有相同的长度+它们没有相互耦合。您将需要使用JOIN sql查询而不是2个SELECT。

类似于:

$results = mysql_query("SELECT * FROM logs 
                       INNER JOIN korisnici 
                       ON logs.username = korisnici.username 
                       ORDER by logs.id, color DESC");
// You will need 2 columns with the same value to couple them to eachother, I took username as example  

while( $extract = mysql_fetch_array($results) ){
    echo "<span style='color:" . $extract['color'] . ";' class='uname'>" . $extract['username'] . "</span> <span class='msg'>" . $extract['msg'] . "</span><br>";
}