我目前有一个带有html代码的php文件。在主体标记im的开头,包括包含db连接的dbcon.php、查询和fetch_result。我现在想在html文件后面使用这些结果,但是我无法让它工作。
网站-档案如下:
<html>
<head>...</head>
<body>
<?php include("dbcon.php"); ?>
...
<some html stuff>
...
<? here i want to use the data from the query ?>
...
</body></html>dbcon.php只包含连接、查询和fetch_results。
编辑: dbcon:
<?php
$con=mysql_connect("localhost:8889","user","pw","db");
$result_query = mysql_query($con,"SELECT * FROM table");
$results = mysql_fetch_array($results_query);
?>我不能访问html文件下部的数据。
发布于 2013-11-26 10:56:19
您的代码是“正确的”,因为您不需要任何东西来访问您的dbcon.php变量。
但是,您正在混合mysql_和mysqli_语法:
mysql_query将查询作为第一个参数,而不是连接mysqli_query将连接作为第一个参数,查询作为第二个参数。您应该使用mysqli_:
$con = mysqli_connect("localhost:8889","user","pw","db");
$result_query = mysqli_query($con, "SELECT * FROM table");
$results = mysqli_fetch_array($results_query);另一个版本,面向对象的:
$mysqli = new mysqli("localhost:8889", "user", "pw", "db");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$results = array();
if ($result_query = $mysqli->query("SELECT * FROM table")) {
$results = $result_query->fetch_array();
} 发布于 2013-11-26 11:02:36
不要使用mysql_函数,它是分解的。
不管怎样,您使用了错误的变量名。$results_query in mysql_fetch_array($results_query),因此将其更改为$result_query,它可能会工作。
<?php
$con=mysql_connect("localhost:8889","user","pw","db");
$result_query = mysql_query("SELECT * FROM table");
$results = mysql_fetch_array($result_query );
?>https://stackoverflow.com/questions/20214750
复制相似问题