dongqiang1226 2013-11-07 21:21
浏览 268

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

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

<?
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:

<?
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:

'CA','CO,'TX'

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

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

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

    $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条回答 默认 最新

  • dre93205 2013-11-07 21:23
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图