检查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>

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";

Your second query needs to look like this: $row['name']

$result1 = mysql_query("SELECT * FROM friends WHERE friends = '" . $row['name'] . "'");

In this line:

$result1 = mysql_query("SELECT * FROM friends WHERE friends = $row{'name'}");

replace $row{'name'} with {$row['name']}:

$result1 = mysql_query("SELECT * FROM friends WHERE friends = {$row['name']}");

Or looks like you may simply do:

$result1 = mysql_query("SELECT * FROM friends WHERE friends = {$row{'name'}}");

Beside, you have very unusual style of taking value by index. Usually brackets like [ and ] are used for that.

mysql_fetch_array() returns an array of strings. You can't write:-

$row1 = mysql_fetch_array($result1);

That's why its giving this warning:-

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

Second thing:-

$row{'name'}-< this syntax is wrong. Use $row['name'] instead.

I don't know how you are coding but this is how I would implement what you code is trying to do what I think it's trying to do.


$result = mysql_query("SELECT * FROM posts ORDER BY time DESC");

//use mysql_fetch_assoc instead of mysql_fetch_array. Makes more sense to me

while ($row = mysql_fetch_assoc($result)) {

    //Then instead or $row{'name'}, you use $row['name']
    $result1 = mysql_query("SELECT * FROM friends WHERE friends = $row{'name'}");
    $row1 = mysql_fetch_array($result1);
    if ($row == 1) {
        echo "".$row{'name'}." says 
".$row{'cont'}."
Posted on ".$row{'time'}."

"; } }

Hope it helps.