开发者

Getting array from PHP function

开发者 https://www.devze.com 2023-04-05 18:28 出处:网络
My db table looks like that I\'m trying to get page title and content from this function function page($current, $lang){

My db table looks like that

Getting array from PHP function

I'm trying to get page title and content from this function

function page($current, $lang){
    global $db;
    $txt='txt_'.$lang;
    $title='title_'.$lang;
    $data=$db->query("SELECT '$txt', '$title' FROM pages WHERE `id`='$current'");
    $data=$data->fetch_array(MYSQLI_BOTH);
    return $data;
}

Then at the beginning of the index page getting array values

$data=page($id, $lang);

开发者_运维知识库

And using like this example

<title><?=$data[1]?></title>

But getting every time the same result title_en (en is my language variable.). What's wrong with my code?


Don't single quote your fields. MySQL interprets this as a string.

$data=$db->query("SELECT $txt, $title FROM pages WHERE `id` = $current");

Alternatively you can use backticks in MySQL. But note that $current still shouldn't have them assuming it's an integer field.

$data=$db->query("SELECT `$txt`, `$title` FROM pages WHERE `id` = $current");
0

精彩评论

暂无评论...
验证码 换一张
取 消