简体   繁体   English

PHP查询数据库返回空白结果

[英]php query to database returning blank results


----- UPDATED - - - 更新

Code works when I'm running it from my schools public_html folder. 当我从学校的public_html文件夹中运行代码时,该代码就可以工作。 However, when I create the exact same file, upload to my personal web server it does not work and only produces a blank screen. 但是,当我创建完全相同的文件时,将其上传到我的个人Web服务器不起作用,只会产生空白屏幕。 Not exactly sure what this means, maybe that I don't have permissions? 不确定是什么意思,也许我没有权限?

Updated Code to reflect suggestions (still not working) 更新了代码以反映建议(仍然不起作用)

----------- -----------

Hi I'm a student learning to use php and postgres. 嗨,我是一个学习使用php和postgres的学生。 Currently I'm just trying to open a connection from my hosted website to my database on my school's server. 目前,我只是想打开从托管网站到学校服务器上数据库的连接。 I'm getting a blank result with my following php file. 我的以下php文件得到空白结果。

I put 'xxxxx' as the connection fields in order not to give away my username and password - the real file has the correct information. 为了不泄露用户名和密码,我将“ xxxxx”作为连接字段-真实文件具有正确的信息。

My question is... is there anything wrong with this php file? 我的问题是...这个php文件有什么问题吗?

<html>
<body>
<h1>Test</h1>

<?php
$username = "xxxxx"
$password = "xxxxx";
$databasename = "xxxxx";
$hostname = "xxxxx";

$connection = pg_connect("host=$hostname dbname=$databasename user=$username password=$password") or die ("could not connect");

$query = "SELECT unit_name from units";

$result = pg_query($connection, $query) or die("ERROR: " . pg_last_error());

$row = 0;
while ($row = pg_fetch_row($result)) {
    echo "$row[0]<br>\n"
};


pg_close($connection);
?> 

</body>
</html>

You have to use pg_fetch_row 您必须使用pg_fetch_row
Try this 尝试这个

$query = "SELECT unit_name from units";

$result = pg_query($connection, $query) or die("ERROR: ".pg_last_error());

if(!result)
{
    echo "An error occurred.\n";
    exit;
} 

$data = [];
while ($row = pg_fetch_row($result)) {
   $data[] = $row; 
}

foreach($data as $value)
{
   echo $value;
}

This Code success on my side! 这段Code成功站在我这一边! try this. 尝试这个。

$link = mysqli_connect("YourHostname", "Your Username", "YourPassword", "YourDBName");

// Check conn
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$sql="/*Your SQL CODE HERE*/";

if(mysqli_query($link, $sql)){
 echo "Success!";
} else{
 echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link); 

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

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