从while循环中获取mysql的值,并在while循环中使用它

时间:2014-10-26 05:48:48

标签: php mysql mysqli

我在表格incoming_sms中有一些不同的数字,我希望从另一个表jobs_content插入一个随机值,同时使用我从{{1}中选择的数字}。

这是我的代码

incoming_sms

如何使用我在此行中的表<?php $mysqli = new mysqli("localhost", "root", "", "test"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $result = $mysqli->query("SELECT distinct(dest_msisdn) FROM incoming_sms where text_message like '%Acc%' AND in_date >= ( CURDATE() - INTERVAL 2 DAY ) AND dest_msisdn in (SELECT msisdn FROM 20214_R4K)"); while($row = $result->fetch_array()) { $distinct_number = $row['dest_msisdn']; /** select description into from jobs_content where category = 'Accounting' order by rand() limit 1; */ $mysqli->query("insert into test_subject(the_number,the_description) values($distinct_number,rand())"); } $result->close() ?> 中选择的实际随机值

jobs_content

而不是使用mysql的$mysqli->query("insert into test_subject(the_number,the_description) values($distinct_number,rand())");?。

1 个答案:

答案 0 :(得分:0)

我通过移动我想要从while循环外部获取值的变量来解决它。

<?php
$mysqli = new mysqli("localhost", "root", "", "test");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$result = $mysqli->query("SELECT distinct(dest_msisdn)  FROM incoming_sms  where text_message like '%Acc%' AND in_date >= ( CURDATE() - INTERVAL 2 DAY ) AND dest_msisdn in (SELECT msisdn FROM 20214_R4K)");

$name = $mysqli->query("select description from jobs_content where category = 'Accounting' order by rand() limit 1")->fetch_object()->description;  

while($row = $result->fetch_array())
  {
  $name = $mysqli->query("select description from jobs_content where category = 'Accounting' order by rand() limit 1")->fetch_object()->description;

  $distinct_number = $row['dest_msisdn'];

  /**
  select description into from jobs_content where category = 'Accounting' order by rand() limit 1;
  */

  $mysqli->query("insert into test_subject(the_number,the_description) values($distinct_number,'$name')");

  }

$result->close()

?>