Lazarus Rising Lazarus Rising - 1 year ago 67
PHP Question

postgresql select query not working

I have a table lecturer with a column name and another department. I know for sure that one of the entries is name='Denis' and department='007'.
This is the code:

$names; $surname;
$firstname = pg_query(connect(), "SELECT name FROM keep_track");
while($row = pg_fetch_array($firstname)){ $names = $row['path']." ".$row['name']; }
$lastname = pg_query(connect(), "SELECT surname FROM keep_track");
while($row = pg_fetch_array($lastname)){ $surname = $row['path']." ".$row['surname']; }

echo '<div id="show_dialog" class="ui-dialog-content ui-widget-content">';
echo "Lecturer: ".$names." ".$surname."<br>Department: ";

$a = pg_query(connect(), "SELECT department FROM lecturer WHERE name='$name'");
while($row = pg_fetch_array($a)){ echo "Hi!!!"; }

echo '</div>';

But it doesnt echo anything. The variable $names has already been set and echoed in previous lines successfully and its value was set as John.
The other queries Ive done are working fine. Idk why this one is not.

OK, I know why it doesn't work, but idk how to fix it. The problem is in this line:

$a = pg_query(connect(), "SELECT department FROM lecturer WHERE name='$name'");

I need to compare column name with variable $name, but it won't work like that. What is the right syntax? I am looking, but i havent been very successful till now

Answer Source

I will try following..

$query = "SELECT department FROM lecturer WHERE name='$name'";
echo $query;
$a = pg_query(connect(), $query);

and will try to execute echoed query directly from some postgresql manager (or psql).