duan7007 2012-08-21 21:28
浏览 99
已采纳

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

I am coding a php page off of an mySQL database and the table is fully built with all the columns I am trying to have viewed on a website, but my select statement is returning an error. It won't recognize the first column I input. I've taken out the "lastname" search and had it just start with "firstname" and had the same problem. On another use of this table I am also having trouble inputting stuff into the first column. Am I forming my statement wrong or something?

I am using a common functions php with this code:

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;
}

Then local code that is utilizing the common functions is:

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

Here is the table structure:

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条回答 默认 最新

  • douchao1864 2012-08-21 21:40
    关注

    Take a look at your code. If you're having an issue with a query, echo it to the screen. In your case (just by looking at your code), the query you pass ($statement) looks like this:

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

    In PHP, you can define a string over multiple lines in order to avoid such mistakes. Like so:

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

    UPDATE:

    In response to your comments below, I would recommend using PDO to set up your query:

    //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. It's pretty awesome.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟