>更新:我更改了my_select_db并将行名更改为没有#符号。它仍然对我不起作用。
我是使用PHP和MySQL的新手。 我已经用HTML,CSS和JQuery写了几页。
我有一个MySQL数据库,我正在尝试在网页上显示几行。
我正在使用 XAMPP 并在本地运行代码。我的网站位于 xampp htdocs 文件夹中。
这是我的HTML文件部分查找.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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></title>
<script type="text/javascript" language="Javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="js/jquery.horizontalNav.js"></script>
<script>
// When document is ready...
$(document).ready(function() {
$('.full-width').horizontalNav({}); // Call horizontalNav on the navigations wrapping element
});
</script>
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","livesearch.php?q="+str,true);
xmlhttp.send();
}
</script>
<link rel="stylesheet" type="text/css" href="style.css" />
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="style-ie.css" />
<![endif]-->
</head>
<body>
<div id="page-wrap">
<div id="inside">
<div id="header">
</div>
<div id="menu">
<nav class="horizontal-nav full-width horizontalNav-notprocessed">
<ul>
<li><a href="#">Navigation Item</a></li>
<li><a href="#">Work</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
<li><a href="partslookup.html">Parts Look Up</a></li>
</ul>
</nav>
</div>
<div id="main-content">
<?php include 'livesearch.php'; ?>
<div style="clear: both;"></div>
<div id="footer">
<p></p>
</div>
</div>
<div style="clear: both;"></div>
</div>
</body>
</html>
这是我的PHP文件实时搜索.php:
<?php
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM ariens_prices';
mysql_select_db('taft_test1');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "EMP ID :{$row['Part#']} <br> ".
"EMP NAME : {$row['Description']} <br> ".
"EMP SALARY : {$row['Super#']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully'n";
mysql_close($conn);
?>
我的数据库名为 taft_test1,其中有一个名为 ariens_prices 的表。ariens_prices有 7 列,其中只有 2 列我想显示,称为"Part#"、"描述"。
我花了很多天试图弄清楚这一点,但无济于事。 我确定我只是错过了一些愚蠢的东西。
提前谢谢。
您的数据库名为"taft_test1",但您的代码正在尝试使用表名作为您的数据库名称:
mysql_select_db('ariens_prices');
应该是:
mysql_select_db('taft_test1');
哦,就像第一条评论所说,这些功能已被弃用。
http://php.net/manual/en/function.mysql-connect.php
警告 此扩展从 PHP 5.5.0 开始已弃用,并且将 将来删除。相反,MySQLi或PDO_MySQL扩展 应该使用。另请参阅 MySQL:选择 API 指南和相关常见问题解答 了解更多信息。此功能的替代方法包括: mysqli_connect() PDO::__construct()
已更新 - 修订了 sql 查询以仅包含所需的列。另外,我假设字段被命名为:部分#,描述,超级#
我建议不要在列名中使用"#"符号。
<?php
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT Part#, Description, Super# FROM ariends_prices';
mysql_select_db('ariens_prices');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "EMP ID :{$row['Part#']} <br> ".
"EMP NAME : {$row['Description']} <br> ".
"EMP SALARY : {$row['Super#']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully'n";
mysql_close($conn);
?>
已更新
这是错误的语法
$sql = 'SELECT Part#, Description, Super# FROM ariends_prices'
正确的方式
$sql = 'SELECT '`Part#'`, Description, '`Super#'` FROM ariends_prices'
"#"字符可能是 SQL 的问题。