2012-08-06 90 views
0

我正在构建一个网站来学习PHP,并刚刚构建了一个会员应用程序。mysql select和echo results results问题

这里是我的代码来获得我设置当用户登录时,再取它们与相关的业务ID的用户的cookie,叫做BIZ,并期待所有细节的业务与ID等于BIZ,在指定的表公司:(顺便说一句,我知道我用mysql,但是当我完成我的应用程序,我会切换到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']; 
    } 
} 
?> 

现在我展示一个链接claim.php如果用户的业务ID等于0,或者如果用户的业务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?

非常感谢大家的帮助!

+0

inb4,'mysql'已被弃用。使用'PDO'或'mysqli' – 2012-08-07 00:27:37

+1

他知道。 “顺便说一句,我知道我使用的是MySQL,但是当我最终确定我的应用时,我将切换到PDO或mysqli” – Jaxkr 2012-08-07 00:30:26

回答

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

导致您的问题。它更改为

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

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

+0

谢谢!很好的答案,谢谢你的解释! – Muhambi 2012-08-07 01:17:40