-3

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.

Beaurocks16
  • 227
  • 1
  • 7
  • 16
  • i think first error is in $result1, try this $result1 = mysql_query("SELECT * FROM friends WHERE friends = ".$row['name']) – Mat Apr 04 '13 at 18:05

5 Answers5

1

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.

Viktor S.
  • 12,509
  • 1
  • 24
  • 50
1

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";
agim
  • 1,824
  • 12
  • 17
  • Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\index.php on line 45 – Beaurocks16 Apr 04 '13 at 18:09
  • Actually mysql_fetch_array is deprecated and you can find the exact syntax here: http://php.net/manual/en/function.mysql-fetch-array.php You should consider using PDO or MySQLi – agim Apr 04 '13 at 18:16
  • Could you give me an example of how I would fix the error? I'll read up on that also. – Beaurocks16 Apr 04 '13 at 18:18
  • @Beaurocks16 that error means that your `mysql_query` fails (Actually, query is broken). Add `die mysql_error();` before that `mysql_fetch_array` and see what error it returns – Viktor S. Apr 04 '13 at 18:20
  • Quick Question, would that sql, the: ' $sql = "SELECT post.time, friends.name, ... FROM post JOIN friends on post.name = friends.name ORDER BY post.time DESC"; ' work if I tried to do the same, except change friends.name to a set in mysql? – Beaurocks16 Apr 04 '13 at 20:41
  • @Beaurocks16 I'm not quite sure what you mean with a set. Can you give an example? – agim Apr 04 '13 at 21:05
  • @agim You can have sets in mysql that are pretty much arrays, but in one column. – Beaurocks16 Apr 04 '13 at 21:13
0

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

$result1 = mysql_query("SELECT * FROM friends WHERE friends = '" . $row['name'] . "'");
SeanWM
  • 16,319
  • 7
  • 49
  • 83
0

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.

Vivek Sadh
  • 4,190
  • 2
  • 29
  • 47
  • Crazy, but that syntax is correct) Even if it is unusual. Never knew that it may work, but just tried it. http://au.php.net/manual/en/language.types.array.php#99015 – Viktor S. Apr 04 '13 at 18:12
0

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.

Touch
  • 1,443
  • 10
  • 18