将mysql DB中的字符串转换为数组

时间:2021-08-12 21:16:06

the code shown below performs an mysql query using implode() to search for multiple variables in one expression, which i like:

下面显示的代码使用implode()在一个表达式中搜索多个变量来执行mysql查询,我喜欢:

<?
include("connect.php"); // file to connect to db

$states = array('CA','CO','TX');
$states_str = implode("','", $states);

$query="SELECT * FROM table1 WHERE state IN ('$states_str')";

$result=mysql_query ($query);
while($row = mysql_fetch_array($result)) {
echo $row['state'];
}
?>

but i have run into a new issue. as you can see from the above 'states' is hard-coded.

但我遇到了一个新问题。从上面的“状态”可以看出,它是硬编码的。

i want the user to be able to select the states they want to pull, have that choice saved to a separate table in the DB, and then have that list read into the 'states' array shown above. Something like this:

我希望用户能够选择他们想要提取的状态,将该选择保存到DB中的单独表中,然后将该列表读入上面显示的“状态”数组中。像这样的东西:

<?
include("connect.php"); // file to connect to db

$sql_gs="SELECT states_c FROM states WHERE id='1'";
$result_gs=mysql_query($sql_gs);
$row_gs = mysql_fetch_row($result_gs);
$gcs = $row_gs[0];

$states = array($gcs);
$states_str = implode("','", $states);

$query="SELECT * FROM table1 WHERE state IN ('$states_str')";

$result=mysql_query ($query);
while($row = mysql_fetch_array($result)) {
echo $row['state'];
}
?>

but this code throws an error.

但是这段代码会抛出一个错误。

what is saved to the states table noted above, and what is contained in the states_c field, a VARCHAR field, is this:

保存到上面提到的states表中的内容,以及states_c字段中包含的VARCHAR字段包含的内容是:

'CA','CO,'TX'

so the '$gcs' variable in the code above ...

所以上面代码中的'$ gcs'变量......

echo $gcs; // result 'CA','CO,'TX'

in other words, the text of $gca (for 'get chosen states') is exactly the same as the text in the hard-coded version above that works

换句话说,$ gca(用于'获取选择状态')的文本与上面硬编码版本中的文本完全相同

but when i try to place the results of the $gca variable, the exact same text, i get an error, no data is pulled, because in this statement

但是当我尝试放置$ gca变量的结果时,完全相同的文本,我得到一个错误,没有数据被拉,因为在这个语句中

    $states = array($gcs); // $gcs reads exactly as 'CA','CO,'TX'

the data is not read, whereas in this one

数据不被读取,而在这一个

    $states = array('CA','CO,'TX');

the states are recognized and the code fires.

状态得到承认,代码触发。

what is the issue with trying to place the same text in this line of code via a named variable as opposed to 'spelling it out' in a hard coded fashion?

尝试通过命名变量将相同的文本放在这行代码中而不是以硬编码的方式“拼写出来”是什么问题?

TIA

===================

UPDATE

I tried this:

我试过这个:

<?
include("connect.php"); // file to connect to db

$sql_gs="SELECT states_c FROM states WHERE id='1'";
$result_gs=mysql_query($sql_gs);
$row_gs = mysql_fetch_row($result_gs);
$gcs = $row_gs[0];

$states = explode("','", $gcs); // replaces $states = array('CA','CO','TX');
$states_str = implode("','",$states);

$query="SELECT * FROM table1 WHERE state IN ('$states_str')";

$result=mysql_query ($query);
while($row = mysql_fetch_array($result)) {
//echo $row['mail_state']; 
}

?>

but still no result ...

但仍然没有结果......

1 个解决方案

#1


0  

Use explode.

$states = explode(",",$gcs);

Hmm. After looking at your implode statement. you may actually need:

嗯。看了你的内幕声明。你可能真的需要:

$states = explode("','",$gcs);

http://php.net/manual/en/function.explode.php

#1


0  

Use explode.

$states = explode(",",$gcs);

Hmm. After looking at your implode statement. you may actually need:

嗯。看了你的内幕声明。你可能真的需要:

$states = explode("','",$gcs);

http://php.net/manual/en/function.explode.php