Php 如何将字段中的值用作$table?

Php 如何将字段中的值用作$table?,php,hidden,Php,Hidden,表格中的表名是一个隐藏字段,出于某种原因,我不得不这样做,但当我执行以下操作时,它不起作用: // This is being processed from the previous page in a form $table = mysql_real_escape_string($_POST['just_field']); //Here i am using the $table value to use in place of a table name $sql = "select `

表格中的表名是一个隐藏字段,出于某种原因,我不得不这样做,但当我执行以下操作时,它不起作用:

 // This is being processed from the previous page in a form
 $table = mysql_real_escape_string($_POST['just_field']);

//Here i am using the $table value to use in place of a table name
$sql = "select `Field` from `$table` where `another_field` = '$id' 
and `another_another_field` = '$something'";
$query = mysql_query($sql) or die ("Error: ".mysql_error());

while ($row = mysql_fetch_array($query)){

    $somefield = $row['field'];
  }
 mysql_free_result($query);

如何使其工作,以便命令读取
$table

无需在表分配周围加引号

$table = mysql_real_escape_string($_POST['just_field']);

不需要在表格分配周围加引号

$table = mysql_real_escape_string($_POST['just_field']);

你的问题是区分大小写

在您的查询中:

select `Field` from `$table`....
        ^ Capital F
后来呢

$somefield = $row['field'];
                   ^ lower case f
一个例子

php > $cxn = mysql_connect( 'localhost', '****', '****' );
php > mysql_select_db( 'test' );
php > $res = mysql_query( 'select * from genres' );
php > $row = mysql_fetch_assoc( $res );
php > print_r( $row );
Array
(
    [id] => 1
    [name] => Pop //notice the field name is lower case
)
php > echo $row['name']; //this works
Pop // <- see
php > echo $row['Name']; //this doesn't
php > // <- look, nothing
php>$cxn=mysql_connect('localhost','**','**');
php>mysql_选择_数据库('test');
php>$res=mysql_查询('select*from-genres');
php>$row=mysql\u fetch\u assoc($res);
php>print\u r($row);
排列
(
[id]=>1
[name]=>Pop//注意字段名是小写的
)
php>echo$row['name']//这很有效
Pop//echo$row['Name']//这并不重要

php>/您的问题是区分大小写

在您的查询中:

select `Field` from `$table`....
        ^ Capital F
后来呢

$somefield = $row['field'];
                   ^ lower case f
一个例子

php > $cxn = mysql_connect( 'localhost', '****', '****' );
php > mysql_select_db( 'test' );
php > $res = mysql_query( 'select * from genres' );
php > $row = mysql_fetch_assoc( $res );
php > print_r( $row );
Array
(
    [id] => 1
    [name] => Pop //notice the field name is lower case
)
php > echo $row['name']; //this works
Pop // <- see
php > echo $row['Name']; //this doesn't
php > // <- look, nothing
php>$cxn=mysql_connect('localhost','**','**');
php>mysql_选择_数据库('test');
php>$res=mysql_查询('select*from-genres');
php>$row=mysql\u fetch\u assoc($res);
php>print\u r($row);
排列
(
[id]=>1
[name]=>Pop//注意字段名是小写的
)
php>echo$row['name']//这很有效
Pop//echo$row['Name']//这并不重要
php>/这对您有用吗

$sql = "select `Field` from ".$table." where `another_field` = '$id' and `another_another_field` = '$something'"; $sql=“从“$table”中选择“Field”。其中“另一个字段”='$id' 和`另一个`字段'='$something'; 这对你有用吗

$sql = "select `Field` from ".$table." where `another_field` = '$id' and `another_another_field` = '$something'"; $sql=“从“$table”中选择“Field”。其中“另一个字段”='$id' 和`另一个`字段'='$something';
在通过
mysql\u real\u escape\u string()
发送之前,请尝试打印
$table
的内容,以确保它包含您认为它包含的值

在通过
mysql\u real\u escape\u string()
发送之前,请尝试打印
$table
的内容,以确保它包含您认为的值

尝试包含连接字符串

$sql = "select `Field` from `$table` where `another_field` = '$id' 
and `another_another_field` = '$something'";
$query = mysql_query($sql,$conn) or die ("Error: ".mysql_error());

 while ($row = mysql_fetch_array($query)){

    $somefield = $row['field'];
  }
 mysql_free_result($query);

如果未使用任何框架,请尝试包含连接字符串

$sql = "select `Field` from `$table` where `another_field` = '$id' 
and `another_another_field` = '$something'";
$query = mysql_query($sql,$conn) or die ("Error: ".mysql_error());

 while ($row = mysql_fetch_array($query)){

    $somefield = $row['field'];
  }
 mysql_free_result($query);

如果未使用任何框架

请在运行查询之前打印
$sql
变量的内容。格式是否正确?删除
mysql\u real\u escape\u字符串
周围的引号?是的,这是一个输入错误,但仍然无法解决问题在运行查询之前打印
$sql
变量的内容。格式正确吗?删除mysql\u real\u escape\u字符串周围的引号
?是的,这是一个输入错误,但仍然无法解决问题抱歉,这是一个输入错误,尽管这不是问题所在problem@SamKhan:您能发布您收到的错误,以便我们能更好地帮助您吗?很抱歉,这是一个打字错误,尽管这不是问题所在problem@SamKhan:你能把错误贴出来吗你得到的是我们能更好地帮助你吗?我怀疑这一点。当返回结果的列名时,区分大小写并不重要。我对此表示怀疑。当返回结果的列名时,区分大小写并不重要。很好,如果有效,请告诉我您是否需要其他内容。如果有效,请告诉我您是否需要其他内容