将mysql数组结果赋给php变量

时间:2013-08-20 13:50:44

标签: php mysql arrays curl

我正在尝试使用API​​将sms发送到mysql表中的所有成员。

我能发送短信。

但是只使用表中的第一行,而我希望将短信发送到表中的所有记录。

我使用的代码是这样的:

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$result = mysql_query("SELECT name,mobile FROM members");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
$mobile=$row[1];
$name=$row[0];

$request = "";
$param['mobileno'] = "91". $mobile;
$param['message']  = "Dear $name (organisation name) wishes you a very very Happy Birthday";
$param['username'] = "username";
$param['password']  = "password";
$param['sendername'] = "sender";
foreach ($param as $key => $val ){$request .= $key . "=" . urlencode($val);    
$request .= "&";}$request = substr( $request, 0, strlen( $request ) - 1 );
$url = "http://smsapi" . $request;
$ch = curl_init($url);curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
var_dump($result);exit;
curl_close( $ch );

我已根据答案中的建议修改了代码,但我仍然只获得第一行的结果。

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$result = mysql_query("SELECT name,mobile FROM members");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while($row = mysql_fetch_row($result)) {
$mobile=$row[1];
$name=$row[0];

$request = "";
$param['mobileno'] = "91". $mobile;
$param['message']  = "Dear $name (organisation name) wishes you a very very Happy Birthday";
$param['username'] = "username";
$param['password']  = "password";
$param['sendername'] = "sender";
foreach ($param as $key => $val ){$request .= $key . "=" . urlencode($val);    
$request .= "&";}$request = substr( $request, 0, strlen( $request ) - 1 );
$url = "http://smsapi" . $request;
$ch = curl_init($url);curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
var_dump($result);exit;
curl_close( $ch );
}

3 个答案:

答案 0 :(得分:0)

使用while($ row = mysql_fetch_row($ result))循环遍历行。

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$result = mysql_query("SELECT name,mobile FROM members");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while($row = mysql_fetch_row($result)) {
$mobile=$row[1];
$name=$row[0];

$request = "";
$param['mobileno'] = "91". $mobile;
$param['message']  = "Dear $name (organisation name) wishes you a very very Happy Birthday";
$param['username'] = "username";
$param['password']  = "password";
$param['sendername'] = "sender";
foreach ($param as $key => $val ){$request .= $key . "=" . urlencode($val);    
$request .= "&";}$request = substr( $request, 0, strlen( $request ) - 1 );
$url = "http://smsapi" . $request;
$ch = curl_init($url);curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
var_dump($result);exit;
curl_close( $ch );
}

答案 1 :(得分:0)

您需要循环所有行。

类似的东西:

while($row = mysql_fetch_row($result)) { .... }

另外,不推荐使用php mysql扩展,看看mysqli或pdo。

答案 2 :(得分:0)

你正在使用mysql_fetch_row,根据我的理解,只返回一行(如果有的话)。考虑使用mysql_fetch_all而不是