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 sh开发者_StackOverflow社区ould work however all the returned value's come back empty?
Is this possible?
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);
}
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.
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
精彩评论