I’m trying to get my postgresql to work. I’ve worked with sql before, but never postgres.
I feel like the following should display a nice table of outputs.
I’ve tested the php in several places, and it seems that the result variable is not getting anything returned, and so is a boolean of false value, so there’s nothing retreived. If anyone could help me find the error I’d be grateful.
if($queryType != NULL)
echo'<table>;
if($limit == NULL && $offset == NULL)
{
$query = 'SELECT * FROM $1';
$stmt = pg_prepare($connection, "limitoffset", $query);
$result = pg_execute($connection, "limitoffset", array($queryType));
if($queryType == 'city'){
while($row = pg_fetch_assoc($result)){
echo '<tr>';
echo '<td>' . $row['id] . '</td>';
echo '</tr>';
}
echo '</table>';
again, it compiles and runs, but it seems $row is coming back as false after the query, and so the loop is not running to print out the results, because it has none.
thanks!
You can’t use a placeholder for an identifier (i.e. table or column name) so this:
won’t work. If you want the table name to be a variable, I think you’re stuck with string interpolation:
You’ll want to use
pg_escape_identifierto avoid problems with strange values in$queryTypebut keep in mind thatpg_escape_identifierwill double quote the identifier and that could give you case sensitivity problems.You also have two missing single quotes in your question: one in
echo'<table>;and another in. $row['id] .but those are presumably just in your question text since you say that the code compiles and runs.