Forum Moderators: coopster
Here is the relevent part of the code. I can not figure out why the ID from the URL is not passing into the sql query.
<?php
$ID=intval($_get['id']);
?>
<?php
$link = mysql_connect('db', 'name', 'pswd');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query('SELECT class_name, ID FROM table where id=$id');
if (!$result) {
die('Invalid query: ' . mysql_error());
rest of code......
I am very new to PHP but I am good at SQL. I have tried
snipets of code like
<?php
$id = $_GET['id'];
echo "the id is: $id";
?>
and am able to get the variable to pass through.
I have tried variations of 's and "s and either get a SQL syntax error or a blank page. I tried adding the intval() funtion thinking that the id was being passed as text instead of a number, but that didn't seem to work either.
Any thoughts?
Thank you in advance.
it's probably a variable interpolation problem.
try this:
$result = mysql_query('SELECT class_name, ID FROM table where id=' . $id);
a good practice (especially for debugging) is to assign the sql string to a variable so you can print out the resulting statement:
$sql = 'SELECT class_name, ID FROM table where id=' . $id;
$result = mysql_query($sql);
also, i just noticed your variable assignment for $ID is upper case and your usage in the string is lower case.
(i'm not a PHP programmer so this may not be an issue.)
<?php
$id = $_GET['id'];
$link = mysql_connect('localhost', 'user', 'pswd');
if (!$link) {die('Could not connect: ' . mysql_error());}
$sql = 'SELECT class_name, ID FROM test.test_ems_ce where id=' . $id;
$result = mysql_query($sql);
if (!$result) {die('Invalid query: ' . mysql_error());}
echo "the id is: $id";
$row = mysql_fetch_array($result);
echo $row['class_name'];
echo "<br />";
mysql_close($link);
?>
I really don't know for sure what the error was other than possibly I was missing a ; when I was doing
$row = mysql_fetch_array($result)
and that is what caused the blank page to be returned.
Anyway thanks for the help.