gpt4 book ai didi

php - mysql select 和 echo 结果问题

转载 作者:行者123 更新时间:2023-11-29 08:48:53 25 4
gpt4 key购买 nike

我正在构建一个网站来学习 PHP,并且刚刚构建了一个成员(member)应用程序。

这是我的代码,用于获取用户登录时设置的用户 cookie,然后获取与其关联的企业 ID(名为 biz),并查找该企业的所有详细信息id 等于 biz,在名为 company 的表中:(顺便说一句,我知道我正在使用 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 未显示,错误为注意: undefined variable :business_name。为什么未设置business_name?

非常感谢您的帮助!!

最佳答案

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

导致了你的问题。将其更改为

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

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

关于php - mysql select 和 echo 结果问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11837534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com