错误:“字段列表”中的未知列“数组”

时间:2013-04-23 09:39:34

标签: php mysql xampp

我在尝试执行该过程时遇到错误:'字段列表'中的未知列'数组'。我仍然是php的新手,所以我想知道我的编码有什么问题。

首先是编码。

<?php
include("dbconnect.php");


$q = mysql_query("SELECT bachok_qtt FROM bachok WHERE bachok_type = 'pants' ");
if(is_resource($q) and mysql_num_rows($q)>0)
{
    $r = mysql_fetch_array($q);
    $r["bachok_qtt"];
}

$w = mysql_query("SELECT bachok_qtt FROM bachok WHERE bachok_type = 'clothes' ");
if(is_resource($w) and mysql_num_rows($w)>0)
{
    $s = mysql_fetch_array($w);
    $s["bachok_qtt"];
}       

$e = mysql_query("SELECT stock_qtt FROM stock WHERE stock_type = 'pants' ");
if(is_resource($e) and mysql_num_rows($e)>0)
{
    $a = mysql_fetch_array($e);
    $a["stock_qtt"];
}    

$t = mysql_query("SELECT stock_qtt FROM stock WHERE stock_type = 'clothes' ");
if(is_resource($t) and mysql_num_rows($t)>0)
{
    $b = mysql_fetch_array($t);
    $b["stock_qtt"];
}    


if ($r < $a)
{
    if($s < $b)
    {
        $sql1 = "UPDATE stock SET stock_qtt = stock_qtt - $r  WHERE stock_type = 'pants' ";
        $sql2 = "UPDATE stock SET stock_qtt = stock_qtt - $s  WHERE stock_type = 'clothes' ";

        $result1 = mysql_query($sql1) or die ("Error: " . mysql_error());
        $result2 = mysql_query($sql2) or die ("Error: " . mysql_error());

        if($result1)
        {
            if($result2)
            {
            echo "Successful";
            echo "<br>";
            echo "<a href='admin2.php'>Back to main page</a>";
            }
        }
        else 
        {
            echo "ERROR";
        }
    }
}
else
{
    echo "Not enough stock";
    echo "<br>";
    echo "<a href='admin2.php'>Back to main page</a>";
}




?> 

那么,为什么它在执行时会抛出未知的列数...我是否正确使用了MySQL_fetch_array()?

4 个答案:

答案 0 :(得分:3)

问题在这里..

$sql1 = "UPDATE stock SET stock_qtt = stock_qtt - $r  WHERE stock_type = 'pants' ";
$sql2 = "UPDATE stock SET stock_qtt = stock_qtt - $s  WHERE stock_type = 'clothes' ";

$r$s都是数组。

我认为你正试图这样做..

$r = mysql_fetch_array($q);
$r = $r["bachok_qtt"];

答案 1 :(得分:1)

您的问题是您将$r定义为数组,然后尝试在字符串中使用它。查看代码,我实际上怀疑您希望将$r["bachok_qtt"]的值赋给变量,然后在查询中使用它。

例如,以下代码不执行任何操作:

$r["bachok_qtt"];

您应该按如下方式更新您的代码:

$q = mysql_query("SELECT bachok_qtt FROM bachok WHERE bachok_type = 'pants' ");
if(is_resource($q) and mysql_num_rows($q)>0)
{
    $r = mysql_fetch_array($q);
    $bachok_qtt = $r["bachok_qtt"];
}

$w = mysql_query("SELECT bachok_qtt FROM bachok WHERE bachok_type = 'clothes' ");
if(is_resource($w) and mysql_num_rows($w)>0)
{
    $s = mysql_fetch_array($w);
    $bachok2 = $s["bachok_qtt"];
}    

然后你的SQL如下:

$sql1 = "UPDATE stock SET stock_qtt = stock_qtt - $bachok_qtt  WHERE stock_type = 'pants' ";
$sql2 = "UPDATE stock SET stock_qtt = stock_qtt - $bachok2  WHERE stock_type = 'clothes' ";

您还应该被告知,mysql_*系列函数现在处于弃用状态,很快就会失效。您应该考虑使用MySQLiPDO,而不是在新代码中使用它们。它们不仅带来了长寿的好处,而且还消除了使用mysql_*的一些安全风险。

答案 2 :(得分:0)

我认为你可以看一下$r["bachok_qtt"]; 也许你想说$r = $r["bachok_qtt"];

之类的东西

答案 3 :(得分:0)

在这里,我去修复你的错误。试试这个

 <?php
 include("dbconnect.php");


 $q = mysql_query("SELECT bachok_qtt FROM bachok WHERE bachok_type = 'pants' ");
 if(is_resource($q) and mysql_num_rows($q)>0)
 {
  $r = mysql_fetch_array($q);

  }

 $w = mysql_query("SELECT bachok_qtt FROM bachok WHERE bachok_type = 'clothes' ");
 if(is_resource($w) and mysql_num_rows($w)>0)
 {
  $s = mysql_fetch_array($w);

 }       

 $e = mysql_query("SELECT stock_qtt FROM stock WHERE stock_type = 'pants' ");
 if(is_resource($e) and mysql_num_rows($e)>0)
 {
  $a = mysql_fetch_array($e);

 }    

 $t = mysql_query("SELECT stock_qtt FROM stock WHERE stock_type = 'clothes' ");
 if(is_resource($t) and mysql_num_rows($t)>0)
 {
 $b = mysql_fetch_array($t);

 }    


 if ($r["bachok_qtt"] < $a["stock_qtt"])
 {
 if($s["bachok_qtt"] < $b["stock_qtt"])
 {
    $sql1 = "UPDATE stock SET stock_qtt = stock_qtt - '".$r["bachok_qtt"]."'  WHERE stock_type = 'pants' ";
    $sql2 = "UPDATE stock SET stock_qtt = stock_qtt - '".$s["bachok_qtt"]."'  WHERE stock_type = 'clothes' ";

    $result1 = mysql_query($sql1) or die ("Error: " . mysql_error());
    $result2 = mysql_query($sql2) or die ("Error: " . mysql_error());

    if($result1)
    {
        if($result2)
        {
        echo "Successful";
        echo "<br>";
        echo "<a href='admin2.php'>Back to main page</a>";
        }
    }
    else 
    {
        echo "ERROR";
    }
 }
 }
 else
 {
 echo "Not enough stock";
 echo "<br>";
 echo "<a href='admin2.php'>Back to main page</a>";
 }

  ?>