MySQL 更新循环中的 PHP 变量 [英] MySQL Update with PHP variables in a loop

查看:57
本文介绍了MySQL 更新循环中的 PHP 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经坚持了一个小时.

I've been stuck on this for an hour.

我有一系列变量($recordsQuestion_1、$recordsQuestion_2 等).由于这些变量的总数发生了变化,我想循环更新我的数据库.但是,我无法弄清楚如何实际存储变量.只需 $l 或recordsQuestion_1 (2, 3, etc)"进入数据库.

I've got a series of variables ($recordsQuestion_1, $recordsQuestion_2, etc). Since the total number of these variables changes, I want to update my DB in a loop. However, I can't figure out how to actually store the variable. Just $l or "recordsQuestion_1 (2, 3, etc)" into the DB.

这就是我所拥有的,尝试抓取可变变量(甚至不确定这是否可行),但什么也做不了.也许是一个数组?

Here's what I've got, tried grasping variable variables (not even sure if that's how to do it), but couldn't get anything to work. Maybe an array?

建议?

$l = 1;
while ($l <= $num_rows) {
    $query = "UPDATE records SET recordListingID = $recordsQuestion_" . $l . " WHERE recordID = " . $l;
    mysql_query($query) or die(mysql_error());
    $l++;   
};

推荐答案

你可能想要使用这样的东西:

You probably want use someting like this:

$var1 = "foo";
$i = 1;

echo "${"var$i"} boo"; //foo boo

为什么不直接使用数组而不是变量系列?

Why don't you just use array instead of the series of variables?

这篇关于MySQL 更新循环中的 PHP 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆