mysql选择和回显结果问题

时间:2012-08-06 23:54:41

标签: php mysql

我正在建立一个网站来学习PHP,并且刚刚建立了一个会员应用程序。

这是我的代码,用于获取用户登录时设置的用户Cookie,然后获取与之关联的商家ID,称为 biz ,并查找所有详细信息在名为公司的表格中,id等于 biz 的商家:(顺便说一句,我知道我使用的是mysql但是当我最终确定我的应用时,我&# 39; ll切换到PDO或mysqli)

<?
$auth = $_COOKIE["auth"];
if ($auth != "1"){
    header("Location: ./signin.php");
}
//Grab all the cookies

$firstname = $_COOKIE['firstname'];
$id = $_COOKIE['id'];
$fname = ucwords($_COOKIE['firstname']);
$lname = ucwords($_COOKIE['lastname']);
$email = $_COOKIE['email'];
$city = ucwords($_COOKIE['city']);
$biz = $_COOKIE['biz'];

if(!empty($biz)){
    $donthaveabizyet = "false";
}
else{
    include("./config.php");
    $result = mysql_query("SELECT * FROM company WHERE id = '$biz'") or mysql_error();
    while($row = mysql_fetch_array($result))
    {
           $business_name = $row['name'];
           $business_phone = $row['phone'];
           $business_website = $row['website'];
           $business_phone = $row['phone'];
           $business_cat1 = $row['cat1'];
           $business_cat2 = $row['cat2'];
           $business_cat3 = $row['cat3'];
           $business_subcat1 = $row['subcat1'];
           $business_subcat2 = $row['subcat2'];
           $business_subcat3 = $row['subcat3'];
           $business_email = $row['email'];
           $business_product1 = $row['product1'];
           $business_product2 = $row['product2'];
           $business_product3 = $row['product3'];
           $business_product4 = $row['product4'];
           $business_product5 = $row['product5'];
           $business_product6 = $row['product6'];
           $business_product7 = $row['product7'];
           $business_noaddress = $row['noaddress'];
           $business_address = $row['address'];
           $business_address2 = $row['address2'];
           $business_zipcode = $row['zipcode'];
           $business_city = $row['city'];
    }
    $result = mysql_query("SELECT * FROM company_secondary WHERE company_id = '$biz'") or mysql_error();
    while($row = mysql_fetch_array($result))
    {
           $business_description = $row['company_description'];
           $business_since = $row['phone'];
           $business_logo = $row['logo'];
           $business_since = $row['since'];
           $business_smoking = $row['smoking'];
           $business_delivery = $row['delivery'];
           $business_alcohol = $row['alcohol'];
           $business_kids = $row['kids'];
           $business_wheelchair = $row['wheelchair'];
           $business_twitter = $row['twitter'];
           $business_facebook = $row['facebook'];
           $business_youtube = $row['youtube'];
           $business_creditcards = $row['creditcards'];
           $business_outdoor = $row['outdoor'];
           $business_featured = $row['featured'];
     }
}
?>

现在,如果用户的业务ID等于0,我会显示到claim.php的链接,或者如果设置了用户的业务ID,我会显示该业务的名称。

<?php 
if($donthaveabizyet != "false")
{
     echo "<br/><br/>You haven't claimed a business yet. <a href='claim.php'>Click here to claim one now.</a>";
}
else
{
     echo $business_name;
}
?>

不幸的是,$ business_name没有显示,错误是Notice: Undefined variable: business_name。为什么没有设置business_name?

非常感谢所有帮助!!

1 个答案:

答案 0 :(得分:2)

while($row = mysql_fetch_array($result))
{

导致您的问题。将其更改为

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

这是因为fetch_array创建了一个带有数字索引的数组($ array [1],$ array [2]等)。 fetch_assoc使索引与列名相同($ array ['this'],$ array ['that']等。)