简体   繁体   中英

php query to database returning blank results


----- UPDATED

Code works when I'm running it from my schools public_html folder. However, when I create the exact same file, upload to my personal web server it does not work and only produces a blank screen. 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. 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.

I put 'xxxxx' as the connection fields in order not to give away my username and password - the real file has the correct information.

My question is... is there anything wrong with this php file?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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