user3233664
user3233664

Reputation:

retrieving data from database does not work on this code

categories databse table:

id  | name  
1   | electronics
2   | Automotive

classifieds database table:

id  user_id  category_id    cover          title       price    description 
102  1       2              iamges/1.jpg    blabla     10      blablabla

I get an error saying: Notice: Undefined variable: name1 in **\post.php on line 49

$id = $_GET['id'];
$res2 = mysql_query("SELECT * FROM `classifieds` WHERE `id` = '" . $id . "' LIMIT 1");

if($res2 && mysql_num_rows($res2) > 0){
    while($row = mysql_fetch_assoc($res2)){
        $id = $row['id'];
        $user_id = $row['user_id'];
        $category_id = $row['category_id'];
        $price = $row['price'];
        $cover = $row['cover'];
        $title = $row['title'];
        $description = $row['description'];
        $profile_data = user_data($user_id, 'username');

        $res3 = mysql_query("SELECT * FROM `categories` WHERE `id` = '" . $category_id . "' LIMIT 1");
        if($res3 && mysql_num_rows($res3) > 0){
            while($row1 = mysql_fetch_assoc($res3)){
                $id1 = $row1['id'];
                $name = $row1['name'];
            }
        }  
    }
}else{
    echo 'error';
}echo $name;

why do i get the error for the echo $name1; ?

Upvotes: 0

Views: 72

Answers (5)

Vegeta
Vegeta

Reputation: 1317

The error

"Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in *\post.php on line 20 line 20"

means that the mysql_query(); function has not executed successfully .

May be check the "id" value received or not.

Upvotes: 1

Satish Sharma
Satish Sharma

Reputation: 9635

you are using $row in both sql queries user $row2 and $row3 like given below

try this

$id = $_GET['id'];
$id = mysql_real_escape_string(stripslashes($id));
$res2 = mysql_query("SELECT * FROM `classifieds` WHERE `id` = '" . $id . "' LIMIT 1");

if(mysql_num_rows($res2) > 0){
    while($row2 = mysql_fetch_assoc($res2)){
        $id = $row2['id'];
        $user_id = $row2['user_id'];
        $category_id = $row2['category_id'];
        $price = $row2['price'];
        $cover = $row2['cover'];
        $title = $row2['title'];
        $description = $row2['description'];
        $profile_data = user_data($user_id, 'username');

        $res3 = mysql_query("SELECT * FROM `categories` WHERE `id` = '" . $category_id . "' LIMIT 1");
        while($row3 = mysql_fetch_assoc($res3)){
            $id = $row3['id'];
            $name = $row3['name'];
        }

    }   
}

UDPATE 2 :

as you tell that you are using a query in function user_data

$data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM users WHERE user_id = $user_id"));

change it as

$data = "";
$res_data = mysql_query("SELECT $fields FROM users WHERE user_id ='$user_id' LIMIT 1 ") or die(mysql_error());
if($row_data = mysql_fetch_assoc($res_data))
{
   $data = $row_data['your_field_name'];
}

Upvotes: 1

Vegeta
Vegeta

Reputation: 1317

Try this ...

replace with this..

$res2 = mysql_query("SELECT * FROM `classifieds` WHERE `id` = $id  LIMIT 1");

also

$res3 = mysql_query("SELECT * FROM `categories` WHERE `id` = $category_id  LIMIT 1");

Upvotes: 1

Jignesh Patel
Jignesh Patel

Reputation: 1022

Try below code

    $id = $_GET['id'];
    $res2 = mysql_query("SELECT * FROM `classifieds` WHERE `id` = '" . $id . "' LIMIT 1");

    if($res2 && mysql_num_rows($res2) > 0){
        while($row = mysql_fetch_assoc($res2)){
            $id = $row['id'];
            $user_id = $row['user_id'];
            $category_id = $row['category_id'];
            $price = $row['price'];
            $cover = $row['cover'];
            $title = $row['title'];
            $description = $row['description'];
            $profile_data = user_data($user_id, 'username');

            $res3 = mysql_query("SELECT * FROM `categories` WHERE `id` = '" . $category_id . "' LIMIT 1");
            if($res3 && mysql_num_rows($res3) > 0)
                while($row1 = mysql_fetch_assoc($res3)){
                    $id = $row1['id'];
                    $name = $row1['name'];
                }
            }   

        }   
    }

Upvotes: 0

M I
M I

Reputation: 3682

you should print the query and run in php myadmin driectly and see if it returns correct result.

you threewhile loops contains same $row. please change below one to some else

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

and 

  while($row = mysql_fetch_assoc($res3)){
  ------^ 

change to some else like $row3

  while($row3 = mysql_fetch_assoc($res3)){
        $id = $row3['id'];
        $name = $row3['name'];
    }

Upvotes: 0

Related Questions