0

我正在尝试使用 API 向 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 );
}
4

3 回答 3

0

您正在使用 mysql_fetch_row,据我了解,它只返回一行(如果找到的话)。考虑改用 mysql_fetch_all

于 2013-08-20T13:57:09.110 回答
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 );
}
于 2013-08-20T13:56:14.937 回答
0

您需要循环所有行。

就像是:

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

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

于 2013-08-20T13:56:34.137 回答