繁体   English   中英

来自包含的php文件的变量

[英]Variables from included php file

我目前有一个带有html代码的php文件。 在body标签的开头,im包含一个dbcon.php,其中包含一个db连接,一个查询和一个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文件下部的数据。

您的代码是“正确的”,因为您无需再访问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();
} 

不要使用mysql_函数,它已被描述。
无论如何,您使用了错误的变量名。 $results_querymysql_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 );

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM