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 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答