1

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:

<?php 
      $names; $surname;
    require_once('connect_db.php'); 
    $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: ";

        require_once('connect_db.php'); 
        $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.

Update: 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

3
  • 1
    there's no way either of your query calls could produce SELECT FROM in your error message. The error message says you're missing the field(s) list in the query, but you clearly have them in your calls. So either this isn't the code causing the errors, or you've forgotten something while cutting/pasting. Commented Feb 12, 2015 at 14:25
  • @MarcB: you are right, I was missing the field in the query, even though I put it in the post. I am editing the post to show the real problem Commented Feb 12, 2015 at 14:34
  • WARNING: Do not use the low-level Postgres driver, especially not without taking proper precautions to properly escape values. This is always dangerous territory, a single missed escape call can make your entire application vulnerable. Instead use a database layer like PDO that makes doing all this much easier both to write and later verify you've done it correctly. Commented Feb 12, 2015 at 15:12

1 Answer 1

1

I will try following..

require_once('connect_db.php'); 
$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).

Sign up to request clarification or add additional context in comments.

4 Comments

I tried. It prints this query like this : SELECT department FROM lecturer WHERE name=
By the way, connect_db.php is tested and works in other queries. The issue is that it doesn't compare name to anything
@amygrimaldi if print empty variable after equal sign that mean you have a problem with variable $name
Exactly. Thanks for the tip. But now Im stuck because I dont know how to do the comparison name=$name. If i use quotes around $name, it doesnt recognize it at all. Even if I dont, its the same thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.