gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:41:35 26 4
gpt4 key购买 nike

我正在用 mySQL 数据库编写一个 php 页面,该表完全由我试图在网站上查看的所有列构建而成,但我的选择语句返回错误。它不会识别我输入的第一列。我已经删除了“姓氏”搜索并让它以“名字”开头并且遇到了同样的问题。在该表的另一种使用中,我也无法将内容输入到第一列。我是在形成我的陈述错误还是什么?

我在这段代码中使用了一个通用的 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;
}

那么利用公共(public)函数的本地代码是:

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

最佳答案

看看你的代码。如果您遇到查询问题,请将其回显 到屏幕上。在您的情况下(只需查看您的代码),您传递的查询 ($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.太棒了。

关于php - 错误 1054,无法识别第一个选择列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12063318/

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