从数据库中提取和分离单列数据

时间:2013-08-27 12:29:15

标签: php mysql

我正在尝试仅在仪表板中显示用户选择的页面

我在这个数据库中有两列

create table users(userid varchar(50), scripts varchar(100))

userid column我将拥有登录的用户名,并在scripts column中,他们希望以逗号分隔格式在仪表板中显示它的页面名称。 ex:total,cust_orders,...

我想单独从脚本列中获取页面名称,如total.php, cust_orders.php... 我试过这样做

$sql = "select scripts from users where userid = '".$_SESSION['UserID']."' ";

$result = DB_query($sql,$db);
$myrow = DB_fetch_array($result);


foreach ($myrow as $res)
    {

        $array123[] = $res;
        $var123 = $array123[0];
        $var222 = $array123[1];

    }

但它不会起作用,因为页面可以是1到8,有人可以帮我吗?

EDITED

我这样做了

$result = DB_query($sql,$db);
$myrow = DB_fetch_array($result);

$arr= $myrow['scripts'];

$arr1 = explode(',', $myrow['scripts']);
print_r ($arr1);

它有效,它显示如下

Array ( [0] => total_dashboard [1] => customer_orders [2] => unpaid_invoice [3] => lat

但动态地如何将它分开,我必须将.php添加到此...

2 个答案:

答案 0 :(得分:1)

$sql = "select scripts from users where userid = '".$_SESSION['UserID']."' ";
$result = mysql_query($sql,$db);
while($myrow = mysql_fetch_array($result))
{
    $arr=explode(',',$myrow["scripts"]);//this will strip the , separated values in an array 
    //now you can fetch the scripts from database
}

答案 1 :(得分:0)

您可以使用PHP - explode之类的:

while ($myrow = mysql_fetch_object($result))
{
    // $myrow->scripts = "text1.php,text2.php,text3.php";
    $scripts = explode(',', $myrow->scripts);
}

因此$scripts将包含每个PHP-Page / Skript作为数组中的自己位置。

$scripts[0] = "text1.php";
$scripts[1] = "text2.php";
$scripts[2] = "text3.php";