类别数据库表:
id | name
1 | electronics
2 | Automotive
分类数据库表:
id user_id category_id cover title price description
102 1 2 iamges/1.jpg blabla 10 blablabla
我收到错误消息:通知:未定义变量:第49行** \ post.php中的name1
$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;
为什么我会收到echo $ name1的错误; ?
答案 0 :(得分:1)
您在sql查询用户$row
和$row2
中使用$row3
,如下所示
试试这个
$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:
正如您所说,您正在使用函数user_data
$data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM users WHERE user_id = $user_id"));
将其更改为
$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'];
}
答案 1 :(得分:1)
错误
"Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in *\post.php on line 20 line 20"
表示mysql_query();
函数具有not executed successfully
。
可以检查" id"收到与否的价值。
答案 2 :(得分:1)
试试这个......
替换为此..
$res2 = mysql_query("SELECT * FROM `classifieds` WHERE `id` = $id LIMIT 1");
也
$res3 = mysql_query("SELECT * FROM `categories` WHERE `id` = $category_id LIMIT 1");
答案 3 :(得分:0)
尝试以下代码
$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'];
}
}
}
}
答案 4 :(得分:0)
你应该打印查询并在php myadmin driectly中运行,看看它是否返回正确的结果。
你们三个循环包含相同的$row
。请将下面的一个更改为其他人
while($row = mysql_fetch_assoc($res2)){
and
while($row = mysql_fetch_assoc($res3)){
------^
更改为$row3
while($row3 = mysql_fetch_assoc($res3)){
$id = $row3['id'];
$name = $row3['name'];
}