错误1054,无法识别第一个选择列

时间:2012-08-21 21:28:21

标签: php mysql

我正在编写一个关于mySQL数据库的php页面,该表完全构建了我试图在网站上查看的所有列,但我的select语句返回错误。它不会识别我输入的第一列。我已经取出了“姓氏”搜索,只是从“名字”开始并遇到了同样的问题。在此表的另一个用途中,我也无法将内容输入第一列。我的陈述是错误的吗?

我正在使用这个代码的常用函数php:

function connectDatabase() {
    require('../DBtest.php');

    $host     = 'localhost';
    $userid   = '7an7';
    $password = '7dl7';

    $db = mysql_perry_pconnect($host, $userid, $password);

    if (!$db) {
        print "<h1>Unable to Connect to MySQL</h1>";
        exit;
    }

    $dbname = '7phpmysql7';
    $dbtest = mysql_perry_select_db($dbname);

    if (!$dbtest) {
        print "<h1>Unable to Select the Database</h1>";
    }

    return $db;
}

function selectResults($statement) {

    $output      = "";
    $outputArray = array();

    $db = connectDatabase();

    if ($db) {
        $result = mysql_query($statement);

        if (!$result) {
            $output .= "ERROR";
            $output .= "<br /><font color=red>MySQL No: " . mysql_errno();
            $output .= "<br />MySQL Error: " . mysql_error();
            $output .= "<br />SQL Statement: " . $statement;
            $output .= "<br />MySQL Affected Rows: " . mysql_affected_rows() . "</font><br />";

            array_push($outputArray, $output);
        } else {

            $numresults = mysql_num_rows($result);

            array_push($outputArray, $numresults);

            for ($i = 0; $i < $numresults; $i++) {
                $row = mysql_fetch_array($result);

                array_push($outputArray, $row);
            }
        }
    } else {

        array_push($outputArray, 'ERROR-No DB Connection');
    }

    return $outputArray;
}

然后使用常用功能的本地代码是:

<?php
include "king_common_functions.php";

viewGuestbook();

//****************************************************************
//Display Admin Guestbook Data (All Submissions)
//****************************************************************

function viewGuestbook() {
    $outputDisplay = "";
    $myrowcount    = 0;

    $statement = "SELECT lastname, firstname, contact_type, contact_info, city, comments, date_added";
    $statement .= "FROM u1585_Guestbook ";
    $statement .= "ORDER BY lastname ";

    $sqlResults = selectResults($statement);

    $error_or_rows = $sqlResults[0];


    if (substr($error_or_rows, 0, 5) == 'ERROR') {
        print "<br />Error on DB";
        print $error_or_rows;
    } else {
        $arraySize = $error_or_rows;

        for ($i = 1; $i <= $error_or_rows; $i++) {
            $lastname     = $sqlResults[$i]['lastname'];
            $firstname    = $sqlResults[$i]['firstname'];
            $contact_type = $sqlResults[$i]['contact_type'];
            $contact_info = $sqlResults[$i]['contact_info'];
            $city         = $sqlResults[$i]['city'];
            $comments     = $sqlResults[$i]['comments'];
            $date_added   = $sqlResults[$i]['date_added'];

            $outputDisplay = "<h3>View Guestbook:</h3>";
            $outputDisplay .= '<table border=1 style="color: black;">';
            $outputDisplay .= '<tr><th>Last Name</th><th>First Name</th><th>Contact Type</th><th>Contact Info</th>';
            $outputDisplay .= '<th>City</th><th>Comments</th><th>Date Added</th></tr>';

            $numresults = mysql_num_rows($sqlResults);

            for ($j = 0; $j < $numresults; $j++) {
                if (!($j % 2) == 0) {
                    $outputDisplay .= "<tr style=\"background-color: #F5DEB3;\">";
                } else {
                    $outputDisplay .= "<tr style=\"background-color: white;\">";
                }

                $myrowcount++;

                $outputDisplay .= "<td>" . $lastname . "</td>";
                $outputDisplay .= "<td>" . $firstname . "</td>";
                $outputDisplay .= "<td>" . $contact_type . "</td>";
                $outputDisplay .= "<td>" . $contact_info . "</td>";
                $outputDisplay .= "<td>" . $city . "</td>";
                $outputDisplay .= "<td>" . $comments . "</td>";
                $outputDisplay .= "<td>" . $date_added . "</td>";
                $outputDisplay .= "</tr>";
            }
        }
    }

    $outputDisplay .= "</table>";
    $outputDisplay .= "<br /><br /><b>Number of Rows in Results: $myrowcount </b><br /><br />";
    print $outputDisplay;
}

这是表结构:

CREATE TABLE u1585_Guestbook (
    guest_id int(11) NOT NULL AUTO_INCREMENT,
    lastname varchar(40) NOT NULL,
    firstname varchar(30) NOT NULL,
    contact_type varchar(30) NOT NULL,
    contact_info varchar(40) NOT NULL,
    city varchar(40) NOT NULL,
    comments varchar(200) NOT NULL,
    date_added date NOT NULL,
    PRIMARY KEY (guest_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

1 个答案:

答案 0 :(得分:1)

看看你的代码。如果您遇到查询问题,请将echo添加到屏幕上。在您的情况下(仅通过查看您的代码),您传递的查询($statement)如下所示:

  

SELECT lastname,firstname,contact_type,contact_info,city,   comments,date_addedFROM u1585_Guestbook ORDER BY lastname

在PHP中,您可以在多行上定义一个字符串,以避免出现此类错误。像这样:

<?php
.
.
.
$statement = "SELECT lastname, firstname, contact_type, contact_info, city, comments, date_added
    FROM u1585_Guestbook 
    ORDER BY lastname ";

<强>更新

在回复下面的评论时,我建议您使用PDO设置查询:

//connect to DB
$db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

// prepare your statement
$query = $db->prepare("INSERT INTO u1585_guestbook(lastname, firstname, contact_type, contact_info, city, comments, date_added) VALUES (?, ?, ?, ?, ?, ?, ?)");
$data = array($mylastname, $myfirstname, $mycontact_type, $mycontactinfo, $mycity, $mycomments, $mydate);

// execute your statement
$query->execute($data);

Take a look at this overview to learn more about PDO.非常棒。