0

I am having difficulties with a MYSQl select statement. Is it possible to return a value where the right value's match. eg. I have a table.

id |   1   |   2   |   3   | etc. <--- COLUMNS
1  | Value | Value | Value | etc.
2  | Value | Value | etc.. 
3  | Value | Value |
4  | Value | Value |
5  | Value | Value | etc...

Im am trying through php to query the database to return value with.

$id = 3;
$inputvalues = "SELECT 'column'
FROM `DB`
WHERE `id` = $id
";

$return = mysql_query($inputvalues, $connection);


while ($r = mysql_fetch_array($return)){
$r[0]; 
echo "$loc = $row[3]<br />";

I think in theory this should work however all the returned value's come back empty?

Is this possible?

1
  • This should be possible, post your actual code and we may be able to help. Right now it's impossible to assist you. Also, presumably where you have DB you mean Table Commented Mar 22, 2011 at 1:09

3 Answers 3

2

You shouldn't use single quotes (') around the column names, you can use backticks (`).

SELECT column
FROM `DB`
WHERE `id` = '$id'

or

SELECT `column`
FROM `DB`
WHERE `id` = '$id'

Put together:

$id = 3;
$sql = "SELECT column
        FROM `DB`
        WHERE `id` = '$id'";

$return = mysql_query($inputvalues, $connection);

while ($r = mysql_fetch_array($return)) {
  print_r($r);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Not need single quotes around an integer.
@ggregoire, quotes around numeric values are not required but it's a good practice to prevent some problems with (missing) input.
Sorry I did'nt add in my code that the select column is actually a variable number. this could be my problem as inside the 'backticks' it may be trying to search the string of '$loc'.
you can and should check about missed number. But quotes around number can cause some performance issues. Sometimes mysql is trying not to cast string numeric to plain number, but the numeric field itself to string.
@zerkms, of course one should check. It just feels somehow better not to put anything without quotes to the query (even though I usually use intval or floatval on numerics instead of escaping). But the main reason is that it's been recommended to me when I started with mysql (not sure if it was a book) but that doesn't mean it's (still) right. I'll take a closer look at it.
0

Have you tried your query in a DB editor like MySql Workbench?

Your query should look like the following:

 "SELECT column FROM DB WHERE id = '$id'"

You must surround the value with single quotes, not around the column name.

Comments

0

If your columns are loaded dynamically don't use quotes around variable in php

$id = 3;
$sql = "SELECT {$column}
        FROM `DB`
        WHERE `id` = '$id'";

it seems like variable name isn't replaced with actual value in query

Comments

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.