在Flex项目中,我有一个包含对象的数组。我想将这个数组与其他一些基本信息(如标题和id)一起保存在mysql表的单元格中。
编辑:只是澄清一下,因为我似乎得到了解释如何呼应所有行的回复。。。我正在尝试回显一个数组的内容,该数组被序列化并放置在一个单元格中。这个数组中有对象。
因此,我在这里有这段代码来序列化数组,并将它与其他信息一起插入到我的DB:中
function submitLogDbObj($array,$id,$title)
{
$title=mysql_real_escape_string($title);
return mysql_query("INSERT INTO logs (text,id,title) VALUES ('".serialize($array)."','$id','$title')");
}
然后为了测试,我试图制作一个循环,以一种看起来像对话的方式显示日志。。。
我的数组中的一个对象看起来像:
[1]
icon = ""
msg = "this is a test"
name = "Them: "
systemMsg = 0
[2]
icon = ""
msg = "yep it sure is"
name = "You: "
systemMsg = 0
到目前为止,这就是我所得到的,但它不起作用!我如何制作一个循环,从数据库中获取该数组,取消序列化,然后以类似聊天日志的方式回显卷积?
谢谢!
<?php
include_once("dbinfo.php");
$id= $_GET['id'];
$result = mysql_query("SELECT text,title FROM logs WHERE id='$id'")
or die(mysql_error());
$row = mysql_fetch_array($result);
if($result)
{
$log = unserialize($row['text']);
echo 'starting loop!';
echo "<ul>";
/* im not sure how to represent the length of an array in php thats why i just have $log.length */
for ($i = 1; $i <=$log.length; $i++)
{
echo "<div id='logbox'>";
echo "<li>";
$name=$log[$i]['name'];
$msg=$log[$i]['msg'];
echo "$name - $msg";
echo "</li>";
echo "</div>";
echo "<br />";
}
echo "</ul>";
echo 'finished loop!';
}
else
{
echo "Looks like this chat log has been deleted. Sorry!";
}
好吧,这里有一些东西可以做得更好:
- 数组的长度是通过
count( $array )
或sizeof( $array )
找到的 - $value$otherValue表示连接这两个值。由于
length
在此上下文中未定义,$log.length的意思是"$log.length" - 循环遍历数组的最佳方式是
foreach( $set as $val)
或foreach( $set as $key => $val )
- 遍历SQL结果的首选方法是while循环:
while($row = mysql_fetch_array($result)){
或do... while
(见下文)。除非你明确而有意识地只想要一个,否则最好使用它。如果您只想要一个,那么在查询中输入一个Limit - 数据库中的序列化数组有多余的味道。你确定这就是你想要的吗
- 在插入序列化数组之前,还应该通过
mysql_real_escape_string
运行它 - 如果你在
div
中围绕某个东西,那么br
真的不应该被需要 - 适当缩进,否则死亡的小猫就会来找你
改进后的代码:
$row = mysql_fetch_array($result);
// row could be empty if there were no results
if($row)
{
// we've already grabbed the first value, so we need
// to invert while into do... while.
do
{
$log = unserialize($row['text']);
echo "<ul>";
foreach( $log as $line )
{
// Are you sure this should be outside of the li?
echo "<div id='logbox'>";
echo "<li>";
$name=$line['name'];
$msg=$line['msg'];
echo "$name - $msg";
echo "</li>";
echo "</div>";
}
echo "</ul>";
}
while( $row = mysql_fetch_array($result) );
echo 'finished loop!';
}
else
{
echo "Looks like this chat log has been deleted. Sorry!";
}
首先,养成正确缩进代码的习惯,这样可以在查找错误时省去很多麻烦。
你不需要知道数组的长度,你只需要使用while循环:(这里是从臀部编码,所以如果你有错误,请告诉我)
$result = mysql_query("......") or die("Query failed");
//Keep going while $row isn't FALSE
//mysql_fetch_array returns false when there are no more rows
while($row = mysql_fetch_array($result)){
//You can close PHP tags here and insert the
//variables in the HTML, it often looks neater
//and your editor can colour code HTML, helping
//you to find problems
?>
<div>
<li><?php echo $row['name'] ?></li>
<li><?php echo $row['msg'] ?></li>
</div>
<?php
}
有关更多示例,请参阅本教程的"fetch arraywhile loop"。
$result = mysql_query("SELECT text,title FROM logs WHERE id='$id'")
echo "<ul>";
while ($row = mysql_fetch_assoc($results)) {
$name = $row['name'];
$msg = $row['msg'];
//display data how ever you want
}
echo "</ul>";