Jump to content

[SOLVED] Unknown column '38' in 'where clause'


mike12255

Recommended Posts

Im trying to query my database using the following code:

 

<?php
include ("connect.php");
$id = $_GET['id'];
$id = mysql_real_escape_string($id);

//$sql = "SELECT * FROM tbl_product WHERE pd_id = $'id'";

$sql = "SELECT * FROM `tbl_product` WHERE `pd_id` = '$id'";



$result = mysql_query ($sql) or die (mysql_error());
$row = mysql_fetch_row($result);
extract($row);

echo "<tr>";
echo "<td align = \"center\">";
echo "<a href = \"item.php?id=$pd_id\"><img src=\"$pd_path\" border=\"0\"><br>$pd_name</a>";
echo "</td>";
echo "</tr>";

 

for some reason i get the error in the title though

 

Unknown column '38' in 'where clause'

 

but when i manually enter the sql using 38 instead of id it works so im confused

all i did was copy $sql delete the begining and change id to 38 and used it in php my admin with ease.

Link to comment
Share on other sites

Change

$id = $_GET['id'];
$id = mysql_real_escape_string($id);

//$sql = "SELECT * FROM tbl_product WHERE pd_id = $'id'";

$sql = "SELECT * FROM `tbl_product` WHERE `pd_id` = '$id'";

 

to

if(isset($_GET['id']) && !is_numeric($_GET['id']))
   die('Forbidden');

$id = (int) $_GET['id'];

$sql = "SELECT * FROM `tbl_product` WHERE `pd_id` = $id";

 

mysql_real_escape_string should only be used on strings, not numbers.

Link to comment
Share on other sites

I believe the correct syntax for SQL is:

 

$sql = "SELECT * FROM tbl_product WHERE pd_id = '".$id."'";

 

if Ppd_id is alpha or

 

$sql = "SELECT * FROM tbl_product WHERE pd_id = ".$id;

 

if Ppd_id is numeric

 

Regards,

 

Valentin

Link to comment
Share on other sites

Change

<?php
$sql = "SELECT * FROM `tbl_product` WHERE `pd_id` = '$id'";
$result = mysql_query ($sql) or die (mysql_error());
?>

to

<?php
$sql = "SELECT * FROM `tbl_product` WHERE `pd_id` = '$id'";
$result = mysql_query ($sql) or die ("Problem with the query: $sql<br>" . mysql_error());
?>

 

This will show you what your query is. Please post that, since it might give a hint.

 

Ken

 

 

Link to comment
Share on other sites

Change

<?php
$sql = "SELECT * FROM `tbl_product` WHERE `pd_id` = '$id'";
$result = mysql_query ($sql) or die (mysql_error());
?>

to

<?php
$sql = "SELECT * FROM `tbl_product` WHERE `pd_id` = '$id'";
$result = mysql_query ($sql) or die ("Problem with the query: $sql<br>" . mysql_error());
?>

 

This will show you what your query is. Please post that, since it might give a hint.

 

Ken

 

This is going to sound really messed up but i entered your code i know i uploaded the right file after saving i know im looking at the right file, and i still got the same error.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.