0

I'm trying to write a script that gets data from a sql server based on the id of the entry in my data base. when I try to access the page using the link with the id of the entry it returns as if it does not recognize the id. Below is the php code :

<?php
    require('includes/config.inc.php');
    require_once(MYSQL);
    $aid = FALSE;
    if (isset($_GET['aid']) && filter_var($_GET['aid'], FILTER_VALIDATE_INT, array('min_range' => 1)) ) {
          $aid = $_GET['aid'];
          $q = "SELECT aircraft_id, aircraft_name AS name, aircraft_type AS type, tail_number AS tn FROM aircraft USING(aircraft_id) WHERE aircraft_id = $aid";
          $r = mysqli_query($dbc, $q);
          if (!(mysqli_num_rows($r) > 0)) {
            $aid = FALSE;
          }
    }// end isset tid

    if ($aid) {
        while ($acdata = mysqli_fetch_array($r, MYSQLI_ASSOC)){
            echo'<h2>'. $acdata['tail_number'] .'</h2>';
        }
    } else {
      echo '<p>This pages was accessed in error.</p>';
    }
?>

Any hints?

3 Answers 3

1

Try to var_dump($q); before $r = mysqli_query($dbc, $q); to inspect your query and then just execute it through phphmyadmin or in MySQL server terminal directly and see what does it return.

Update:

Use var_dump($q);die(); to stop script from executing after dumping.

7
  • Is it supposed to output something, becasue it just gives me the same error. Sep 3, 2012 at 9:36
  • what does it mean nothing? Your script must stop and give you a SQL query. Sep 3, 2012 at 11:10
  • That did not happen, it just gave me the error This pages was accessed in error. Sep 3, 2012 at 18:33
  • Its the error that is attached to the else statment "This pages was accessed in error." Sep 4, 2012 at 6:45
  • So your error is in code, not in SQL? It means that program flow doesn't "go" in your first if (isset($_GET['aid']) && ... statement. If code goes there it would give you SQL query. Make it get there. Sep 4, 2012 at 6:48
0

You are using a field alias in your query, so you must use that in your echo to:

echo'<h2>'. $acdata['tn'] .'</h2>';
1
  • No that was not it, i get this error "This pages was accessed in error." regardless of what the $acdata tag was Sep 3, 2012 at 9:29
0

Get rid of the USING(aircraft_id), it causes an error and your query doesn't execute.

"SELECT aircraft_id, aircraft_name AS name, aircraft_type AS type, tail_number AS tn FROM aircraft WHERE aircraft_id = $aid"

I guess it's a leftover from a previous version of the query? Using (id) is a shortcut for

FROM
foo
INNER JOIN bar ON foo.id = bar.id

It can be used when the tables to be joined are joined on columns which have the same name. Just shorter to write.

Since you are not joining you have to remove it.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.