-1

I am developing a simple application on PHP . If I trying to run it always showing undefined variable.. I need some suggestions regarding how to fix this, Please find the code snippet below,

<?php
session_start();
include("profilesql.php");
$result = mysql_query("SELECT * FROM addfriends where meid='$_SESSION[stuid]' ");
while($row = mysql_fetch_array($result))
  {
$uid1[$i] = $row["friendid"];
$i++;
  }

 $acrec1 = mysql_query("SELECT * FROM addfriends WHERE userid='$uid1[1]'");

while($row = mysql_fetch_array($acrec2))
  {
    $img1[0]=  $row["image"];
  }

  $acrec2 = mysql_query("SELECT * FROM addfriends WHERE userid='$uid1[2]'");

while($row = mysql_fetch_array($acrec2))
  {
    $img1[1]=  $row["image"];
  }

  $acrec3 = mysql_query("SELECT * FROM profile WHERE userid='$uid1[3]' ");

while($row = mysql_fetch_array($acrec3))
  {
    $img1[2]=  $row["image"];
  }

  $acrec4 = mysql_query("SELECT * FROM profile WHERE userid='$uid1[4]' ");
while($row = mysql_fetch_array($acrec4))
  {
    $img1[3]=  $row["image"];
  }
  ?>

As per the above code snippet, I am getting the error message like stated below,

Notice: Undefined variable: uid1 in C:\xampp\htdocs\collegenetwrking\friends.php on line 11

Notice: Undefined variable: acrec2 in C:\xampp\htdocs\collegenetwrking\friends.php on line 13

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\collegenetwrking\friends.php on line 13

Notice: Undefined variable: uid1 in C:\xampp\htdocs\collegenetwrking\friends.php on line 18

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

Notice: Undefined variable: uid1 in C:\xampp\htdocs\collegenetwrking\friends.php on line 25

Notice: Undefined variable: uid1 in C:\xampp\htdocs\collegenetwrking\friends.php on line 32

Please suggest me on this.

Sriraman
  • 7,250
  • 4
  • 37
  • 60
robin
  • 3
  • 2
  • Doesn't look like you have set the $_SESSION['studid'] anywhere before you used it in the query – Aditya Jan 24 '14 at 04:17

3 Answers3

0

You are getting those errors because you are trying to index into them before they have been created. Try initializing them as arrays before you index into them and that should solve your problem.

I'm also not seeing where you define $i... that would need to exist somewhere before you use it.

Your SQL statements are also broken... you need to form them as complete strings like the following:

"SELECT * FROM addfriends WHERE meid=" . $_SESSION[stuid]

TMan
  • 1,203
  • 1
  • 12
  • 25
0

The "undefined" notices are due to un-initialised variables. try adding to the top of your script a $uid1=null, and that notice will be gone (same for the other similar ones)

While notices aren't really a problem, they can point to deeper issues or things that can bite you in the arse later...

now the "Warning: mysql_fetch_array()" points to a deeper problem; your result was empty. Try checking if you got a meaningful result back, first. There's actually a good example here on the php manual page: http://au2.php.net/mysql_query

edit: ah the line 13 error is because you're fetching the wrong result, check your variable names! acrec1 != acrec2

Steve Horvath
  • 465
  • 4
  • 10
  • **i solve line 13 error but i not solve this prob. mysql_fetch_array() expects parameter 1 – robin Jan 24 '14 at 04:33
  • Maybe the query is wrong; check the manual page on how to handle errors from mysql_query, it'll help you to identify that one. You might also want to output the query before sending it to mysql, to be sure that it makes sense (and then you can manually run it to double check it, too) – Steve Horvath Jan 24 '14 at 05:51
0
$i = 0;
while($row = mysql_fetch_array($result)) {
    $i++;
    $uid1[$i] = $row["friendid"];
}
Cyril Joudieh
  • 132
  • 2
  • 15