dowbwrr3590709 2013-04-04 18:02
浏览 50
已采纳

检查mysql集中是否存在值,如果是,则打印它[复制]

How would I go about trying to check if a certain value exists in a mysql set? I have this code:

that I was experimenting with (I doubted it would work) And it's throwing this error:

$result = mysql_query("SELECT * FROM posts ORDER BY time DESC");
while ($row = mysql_fetch_array($result)) {
    $result1 = mysql_query("SELECT * FROM friends WHERE friends = $row{'name'}");
    $row1 = mysql_fetch_array($result1);
    if ($row == 1) {
        echo "<b style='color: #D55292;'>".$row{'name'}."</b> says <br>".$row{'cont'}."<br><b style='color: #A1A1A1';>Posted on ".$row{'time'}."</b><br><br>";
    }
 }

I'm getting these errors:

Notice: Array to string conversion in C:\xampp\htdocs\index.php on line 44

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in        C:\xampp\htdocs\index.php on line 45

Notice: Array to string conversion in C:\xampp\htdocs\index.php on line 44

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\index.php on line 45

Notice: Array to string conversion in C:\xampp\htdocs\index.php on line 44

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\index.php on line 45

Could you guys give me a few pointers on how I would go about doing this? Thanks in advanced!

I just thought the question might not be clear enough:

I have a set that stores all your "friends" in my "friends" table. I also have a table that stores all the "posts" with the values of your name, the content, the time, and who can view it. I'm trying to get those posts (Which I have done successfully), and print them. I can do that. I just want to make it so you can only see posts where the "name" value is equal to a string in your "friends" set.

</div>
  • 写回答

5条回答 默认 最新

  • duanlu1959 2013-04-04 18:06
    关注

    Some PHP errors: AFAIR it's not $row{'name'} but $row['name'] and you should initiate the variable outside of the string. Same thing for $row{'cont'}

    $result = mysql_query("SELECT * FROM posts ORDER BY time DESC");
    while ($row = mysql_fetch_array($result)) {
        $name = $row['name'];
        $result1 = mysql_query("SELECT * FROM friends WHERE friends = '$name'");
        $row1 = mysql_fetch_array($result1);
        if ($row == 1) {
            echo "<b style='color: #D55292;'>".$row['name']."</b> says <br>".$row['cont']."<br><b style='color: #A1A1A1';>Posted on ".$row['time']."</b><br><br>";
        }
     }
    

    I'm not sure, why do you need $result1 respectively $row1 since you don't use them in the code. A better solution would be, if you join the two tables so you wouldn't need the second query, which you execute for each post:

     $sql = "SELECT post.time, friends.name, ... 
               FROM post 
               JOIN friends on post.name = friends.name 
           ORDER BY post.time DESC";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)