0

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

这是我的代码,用于获取我在用户登录时设置的用户 cookie,然后获取它们关联的业务 ID,称为biz并在名为company的表中查找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'];
     }
}
?>

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

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

非常感谢大家的帮助!!

4

1 回答 1

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'] 等)

于 2012-08-06T23:58:13.710 回答