-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.

user2625119
  • 25
  • 1
  • 4

2 Answers2

0

Define the $uid1 variable at the starting:

session_start();
$uid1 = array();
$i = 0;

As this variable is not find so it it global by defining it at the top.

This issue is occurs due to scope of variable. The scope of variable is within the while loop only so define it at the top to make it accessible within all conditions.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
0

Please define all the variables on top, then you will not received this errors:
For example:

var $a = "";
var $b = "";
$array_name = array();

Please have an habit of defining an all the variable before use/assign value.

Shreyas
  • 247
  • 1
  • 14