我需要查询其他数据库的条目。这样,我就可以翻译所需的单词。我可以使用一张桌子,但我认为这样会很复杂。如果我使用mysqli_fetch_row
没有错误,但是如果我使用mysqli_fetch_array
并输入数据库中有的单词,那就有错误。因此,如果我输入“简短”一词
像
$wordGermanBrief
$verbGermanBrief
$pronounGermanBriefwordGermanBrief$_POST["wordGerman"]=Brief
注意:未定义索引:C:\ xampp \ htdocs \ topics \ toknow \ mysqli_use_result \ mysqli_use_result.php中的wordGerman wordGerman $ _POST [“ wordGerman”] =简介
CREATE TABLE `germanverbs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`verbGerman` varchar(20) DEFAULT NULL,
`verbEnglish` varchar(20) DEFAULT NULL,
`PartOfSpeech` varchar(20) DEFAULT NULL,
`SingularFirst` varchar(20) DEFAULT NULL,
`SingularSecond` varchar(20) DEFAULT NULL,
`SingularThird` varchar(20) DEFAULT NULL,
`PluralFirst` varchar(20) DEFAULT NULL,
`PluralSecond` varchar(20) DEFAULT NULL,
`PluralThird` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
CREATE TABLE `germanpronouns` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`pronounGerman` varchar(20) DEFAULT NULL,
`pronounEnglish` varchar(20) DEFAULT NULL,
`PartOfSpeech` varchar(20) DEFAULT NULL,
`SingularFirst` varchar(20) DEFAULT NULL,
`SingularSecond` varchar(20) DEFAULT NULL,
`SingularThird` varchar(20) DEFAULT NULL,
`PluralFirst` varchar(20) DEFAULT NULL,
`PluralSecond` varchar(20) DEFAULT NULL,
`PluralThird` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
CREATE TABLE `germannouns` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`wordGerman` varchar(20) DEFAULT NULL,
`wordEnglish` varchar(20) DEFAULT NULL,
`PartOfSpeech` varchar(20) DEFAULT NULL,
`Nominativ` varchar(60) DEFAULT NULL,
`Genetive` varchar(20) DEFAULT NULL,
`Dative` varchar(20) DEFAULT NULL,
`Accusative` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
index.php
<?php
$link = mysqli_connect("localhost","root","","dictionary");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$wordGerman = $_POST['wordGerman'];
$verbGerman = $_POST['wordGerman'];
$pronounGerman = $_POST['wordGerman'];
$query = <<<_SQL_
SELECT * FROM germannouns WHERE wordGerman ='$wordGerman';
SELECT * FROM germanverbs WHERE verbGerman ='$verbGerman';
SELECT * FROM germanpronouns WHERE pronounGerman ='$pronounGerman';
_SQL_;
echo '$wordGerman'.$wordGerman.'<br />'
.'$verbGerman'.$verbGerman.'<br />'
.'$pronounGerman'.$pronounGerman;
if(mysqli_multi_query($link, $query)){
do {
/* store first result set */
if($result = mysqli_use_result($link))
// while($row=mysqli_fetch_row($result)){
// printf("%s\n", $row[0]);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC )){
echo $row["verbGerman"];
echo $row["wordGerman"];
echo $row["pronounGerman"];
}
mysqli_free_result($result);
/* print divider */
if(mysqli_more_results($link)){
printf("-----------------\n");
}
} while(mysqli_next_result($link));
}
/* close conncection */
mysqli_close($link);
?>
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search a word in German</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div style="margin" 100px auto 0; width: 300px;">
<form name="form1" id="form1" action="index.php" method="post">
<fieldset>
Enter German word<input type="text" name="wordGerman" placeholder="german word" />
<br />
<input type="submit" name="submit" value="submit" />
</fieldset>
</form>
</div>
</body>
</html>
如果您建议我,我会很高兴。
答案 0 :(得分:1)
每个查询的列名都不同。第一个查询的结果仅包含$row['wordGerman']
,第二个查询的结果包含$row['verbGerman']
,最后一个查询的包含$row['pronounGerman']
。但是,您尝试在整个循环中每次都打印所有三个。您应该检查哪一个存在,
if (isset($row["verbGerman"])) {
echo $row["verbGerman"];
} elseif (isset($row["wordGerman"])) {
echo $row["wordGerman"];
} elseif (isset($row["pronounGerman"])) {
echo $row["pronounGerman"];
}
代替使用mysql_multi_query()
,您可以简单地将它们作为对每个表的单独查询,并在查询后打印该查询的结果。
您还可以将它们全部放在带有partOfSpeech
列的单个表中。然后,您可以只执行一个查询:
SELECT partOfSpeech
FROM GermanWords
WHERE word = '$wordGerman'