I mysql query from a database to return a field which I want to use later.
我从数据库查询mysql以返回我想稍后使用的字段。
I can get the query to run and I can print the data from the field using echo to prove the result is as expected . I have tried various scripts to store the resultant field as a variable, but no success so far.
我可以让查询运行,我可以使用echo从字段打印数据,以证明结果符合预期。我已经尝试了各种脚本来将结果字段存储为变量,但到目前为止还没有成功。
Can anyone assist.
谁能帮助。
Here is the query script -
这是查询脚本 -
$query = mysql_query("SELECT ID FROM users WHERE username = ".$_SESSION['user']);
while($row = mysql_fetch_object($query) )
{
echo "$row->ID<br />";
I now need to store the ID as a variable to test on and possibly store in another table with other data.
我现在需要将ID存储为变量以进行测试,并可能存储在具有其他数据的另一个表中。
I am not a coder and am trying to finish a project for the family which was started by someone else.
我不是一个程序员,我正在尝试完成一个由其他人创建的家庭项目。
Thanks.
EDIT 1
I have been trying to get the data into a temporary database and am having an issue.
我一直在尝试将数据放入临时数据库并且遇到问题。
If I enter this into phpMyadmin it works
如果我把它输入phpMyadmin就行了
INSERT INTO tempusertrip (username,ID) SELECT username,ID FROM users WHERE ID=1
However if I add it to my php script it does not, and there are no errors in the log file. I add it like this
但是,如果我将它添加到我的PHP脚本,它不会,并且日志文件中没有错误。我这样添加它
$query1 = ("INSERT INTO temp2 (username,ID) SELECT username,ID FROM users WHERE ID=1");
There is obviously something wrong with my script, but I can't seem to figure out what it is.
我的脚本显然有问题,但我似乎无法弄清楚它是什么。
Ideally I need to enter the data where the condition is based on a username contained in a session set previously ($_SESSION['user']) nstead of using 'where ID=1'.
理想情况下,我需要输入数据,其中条件基于先前会话集中包含的用户名($ _SESSION ['user']),而不是使用'where ID = 1'。
Any guidance would be appreciated.
任何指导将不胜感激。
1 个解决方案
#1
1
First, you'll need to create a table for your data. It can be a temporary table that only your connection can see or it can be a regular table (if other connections will need the data). You'll need to decide what columns you need from the source (if not all) and what extra columns you might need for what you are doing (if any). Often you just need the primary key from your original table(s) in your new table. With MySQL if the new table is going to be identical to your old table, use the CREATE ... LIKE old_tbl_name
syntax for forward-compatibility.
首先,您需要为数据创建一个表。它可以是只有您的连接可以看到的临时表,也可以是常规表(如果其他连接需要数据)。您需要确定从源(如果不是全部)所需的列以及您可能需要的额外列(如果有的话)。通常,您只需要新表中原始表中的主键。对于MySQL,如果新表与旧表相同,请使用CREATE ... LIKE old_tbl_name语法进行前向兼容。
Next (and this is probably what you were looking for in the first place), you'll insert into the new table from a select of the old table:
接下来(这可能是您首先要查找的内容),您将从旧表的选择中插入到新表中:
INSERT INTO new_tbl_name SELECT ... FROM old_tbl_name WHERE ...
If you have extra columns, you'll need to specify which columns you are copying:
如果您有额外的列,则需要指定要复制的列:
INSERT INTO selected_users (username) SELECT username FROM users WHERE ...
(The columns do not need to be named the same, but why ask for trouble.)
(列不需要命名相同,但为什么要麻烦。)
#1
1
First, you'll need to create a table for your data. It can be a temporary table that only your connection can see or it can be a regular table (if other connections will need the data). You'll need to decide what columns you need from the source (if not all) and what extra columns you might need for what you are doing (if any). Often you just need the primary key from your original table(s) in your new table. With MySQL if the new table is going to be identical to your old table, use the CREATE ... LIKE old_tbl_name
syntax for forward-compatibility.
首先,您需要为数据创建一个表。它可以是只有您的连接可以看到的临时表,也可以是常规表(如果其他连接需要数据)。您需要确定从源(如果不是全部)所需的列以及您可能需要的额外列(如果有的话)。通常,您只需要新表中原始表中的主键。对于MySQL,如果新表与旧表相同,请使用CREATE ... LIKE old_tbl_name语法进行前向兼容。
Next (and this is probably what you were looking for in the first place), you'll insert into the new table from a select of the old table:
接下来(这可能是您首先要查找的内容),您将从旧表的选择中插入到新表中:
INSERT INTO new_tbl_name SELECT ... FROM old_tbl_name WHERE ...
If you have extra columns, you'll need to specify which columns you are copying:
如果您有额外的列,则需要指定要复制的列:
INSERT INTO selected_users (username) SELECT username FROM users WHERE ...
(The columns do not need to be named the same, but why ask for trouble.)
(列不需要命名相同,但为什么要麻烦。)