第一个查询不完全执行

时间:2014-02-27 04:40:34

标签: php mysql

我正在建立一个票务系统并需要进行一些查询,我收到以下错误。

  

PHP警告:mysql_fetch_array()期望参数1是资源,   给定布尔值

该行指向我在下方添加了评论。有人有主意吗?谢谢!

$check = "SELECT * FROM stix_cart WHERE confirmation = '$confirmation'";
$result = mysql_query($check);
if ($result === FALSE) {
    error_log(mysql_error());
} else {
    while($row = mysql_fetch_array($result)) {

        $sql = "INSERT INTO stix_orders (
                paymethod,
                confirmation,
                payer_first,
                payer_last,
                startorder,
                endorder,
                name,
                code,
                qty,
                price,
                original,
                puname,
                txn_id)
            VALUES (
                'paypal',
                '".$row['confirmation']."',
                '".$row['payer_first']."',
                '".$row['payer_last']."',
                '".$row['timestamp']."',
                '".$currenttime."',
                '".$row['name']."',
                ".$row['code'].",
                ".$row['qty'].",
                ".$row['price'].",
                ".$row['original'].",
                '".$row['puname']."',
                '".$txn_id."')";

        if (!mysql_query($sql)) {
            error_log(mysql_error());
            exit(0);
        }

        $confirmation = $row['confirmation'];
        $name = $row['name'];
        $code = $row['code'];


        $ticketcheck = 'SELECT MAX(ticket_number) AS max_number FROM stix_tickets WHERE item_number = $row[code]';
        $resulttix = mysql_query($ticketcheck);
        if ($resulttix === FALSE) {
            $incnumber = 1;
        }

        while($ticketrow = mysql_fetch_array($resulttix)) { // This line
            $incnumber = $ticketrow['max_number'] + 1;

            for ($i=1; $i <= $itemqty; $i++) {
                $sql = "INSERT INTO stix_tickets (
                        txn_id,
                        confirmation,
                        item_name,
                        item_number,
                        ticket_number)
                    VALUES (
                        '".$txn_id."',
                        '".$confirmation."',
                        '".$name."',
                        ".$code.",
                        ".$incnumber.")";
                $incnumber++;

                if (!mysql_query($sql)) {
                    error_log(mysql_error());
                    exit(0);
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您的查询在这里失败

$ticketcheck = 'SELECT MAX(ticket_number) AS max_number FROM stix_tickets WHERE item_number = $row[code]';

那是因为..单引号下的变量不会被解析!

将其更改为......

$ticketcheck = "SELECT MAX(ticket_number) AS `max_number` FROM `stix_tickets` WHERE item_number = '".$row['code']."'";

<强> SideNote

从PHP 5.5.0开始,不推荐使用(mysql_*)扩展名,将来会删除它。相反,应使用MySQLiPDO_MySQL扩展名。切换到PreparedStatements甚至可以更好地抵御SQL注入攻击!

答案 1 :(得分:1)

我将如何修复它。

$ticketcheck = "SELECT MAX(ticket_number) AS max_number FROM stix_tickets WHERE item_number = '{$row['code']}'";

在查询中使用大括号表示所有数组。 以下是有关此内容的更多信息 http://fr2.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex