gpt4 book ai didi

php - 使用PHP在HTML中显示MySQL

转载 作者:行者123 更新时间:2023-11-28 03:29:04 25 4
gpt4 key购买 nike

更新:我更改了 my_select_db 并将行名称更改为没有 # 符号。它仍然对我不起作用。

我刚开始使用 PHP 和 MySQL。我已经使用 HTML、CSS 和 JQuery 编写了一些页面。

我有一个 MySQL 数据库,我试图在网页上显示几行。

我正在使用 XAMPP 并在本地运行代码。我的网站位于 xampp htdocs 文件夹中。

这是我的 HTML 文件 partslookup.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 文件 livesearch.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#”、“Description”。

我花了很多天试图解决这个问题,但无济于事。我确定我只是遗漏了一些愚蠢的东西。

提前致谢。

最佳答案

您的数据库名为“taft_test1”,但您的代码试图使用表名作为您的数据库名称:

mysql_select_db('ariens_prices');

应该是:

mysql_select_db('taft_test1');

哦,就像第一条评论所说的那样,这些函数已被弃用。

http://php.net/manual/en/function.mysql-connect.php

Warning This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysqli_connect() PDO::__construct()

已更新 - 修改了 sql 查询以仅包含您需要的列。此外,我假设这些字段被命名为:Part#、Description、Super#

我建议不要在列名中使用“#”符号。

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

关于php - 使用PHP在HTML中显示MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19106809/

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