duanpu6319 2015-02-25 17:15
浏览 390

MYSQL查询将结果数组保存为变量

im after the simplest (and easiest to understand from a novice programmers perspective) to do the following;

I have a query on a database:

$newsquery = mysql_query('SELECT * FROM news ORDER BY id DESC LIMIT 3');

I want to to select the top three rows in my table and get the column 'news'

Then I need to save each result as a separate variable, this could be $news1, $news2 and $news3

So i could then use these variables to echo as needed on my page.

I hope this makes sense, many thanks .

  • 写回答

2条回答 默认 最新

  • dongyu1614 2015-02-26 05:43
    关注

    stop using mysql as PHP is removing it at the end of the year,use PDO or mysqli

    $rows = array();
    while($row = mysql_fetch_assoc($newsquery)) {
    $rows[] = $row['news'];
    }
    $news1 = $rows[0];
    $news2 = $rows[1];
    $news3 = $rows[2;
    

    with PDO

    $rows = $results->fetchAll(PDO::FETCH_ASSOC);
    
    $news1 = $rows[0]['news'];
    $news2 = $rows[1]['news'];
    $news3 = $rows[2]['news'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效