Thanks to a forum member I have moved a little forward but now I am stumped on how to convert a passed value to a variable so it can be used to search a MySQL database. When I use the passed variable as the value I receive the following error:

"SELECT * FROM courses WHERE org_id = 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"

If I "hard code" the actual value passed I am able to select the correct record. Here is php code:


// connect include
require ("connect.php");

$once = print_r($_GET, true);
echo "<p>" . $once. "<p>";

$twice= var_export($_GET, true);
echo "<p>" . $twice. "<p>";

if($_GET["varID"] === "") echo "varID is an empty string\n";
if($_GET["varID"] === false) echo "varID is false\n";
if($_GET["varID"] === null) echo "varID is null\n";
if(isset($_GET["varID"])) echo "varID is set\n";
if(!empty($_GET["varID"])) echo "varID is not empty";

/* $query = "SELECT * FROM organizations WHERE org_id = {$_GET['varID']}"; */
$query = "SELECT * FROM organizations WHERE org_id = 9661";

/* Temporary ECHO of the $sql string */ 
echo "<p>" . $query . "</p>";

$result = mysql_query($query)or die(mysql_error());
if (!$result) { 
  echo("<p>Error performing query: " . mysql_error() . "</p>"); 

$result = mysql_query($query)or die(mysql_error());
if (!$result) { 
  echo("<p>Error performing query: " . mysql_error() . "</p>"); 

$num = mysql_num_rows($result);

while ($row = mysql_fetch_assoc($result))

    echo("<tr>\n<td>" . $row["cityname"] . "</td>"); 
    echo("<td>" . $row["statename"] . "</td>"); 
    echo("<td>" . $row["orgname"] . "</td>");
    echo "<td>" .$row['org_id']."</td>";  

/* Closes Connection to MySQL server */ 

mysql_close ($connect); 

The output of this code is as follows:

Array ( [varID_] => 9661 ) 

array ( 'varID_' => '9661', )

Array ( [varID_] => 9661 ) varID is null 

SELECT * FROM organizations WHERE org_id = 9661

BILLINGSMONTANAAssociation of State Grazing Districts9661 

I have tried reading up on the php functions for arrays but can't find what I need. Any help would be appreciated.

Edited by mike_2000_17: Fixed formatting

7 Years
Discussion Span
Last Post by Capt Spaghetti

Hi Capt Spaghetti (love the name btw),

Are you sure the key 'varID' is being passed correctly in the URL? It looks like 'varID_' is the key that is being displayed in your echo statements ($once and $twice).



I down select using a MySQL statement and the user picks their choice based on the org_id. I am passing it as follows:

echo "<a href = 'graborginfo9.php?varID =".$row['org_id']."'>".$row['org_id']."</td></a>";

Should I be doing something differently?

Capt Spaghetti


Hi again,

you need to remove the space where you declare varID.


echo "<a href = 'graborginfo9.php?varID =".$row['org_id']."'>".$row['org_id']."</td></a>";


echo "<a href = 'graborginfo9.php?varID=".$row['org_id']."'>".$row['org_id']."</td></a>";

and try that.


Edited by Zagga: n/a



My father use to kid me by asking me if I majored in space(i.e. taking up space). Now I see how important space is. That did the trick. Thank you very much for the assistance.

Capt Spaghetti

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.