将mysql数组结果赋给php变量

时间:2021-09-23 15:43:59

I am trying to send sms to all the members in a mysql table using API.

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

I am able to send the sms.

我能发短信了。

But only first row in the table is being used, whereas I want that the sms is sent to all the records in the table.

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

The code I am using is like this:

我使用的代码是这样的:

$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 );

I have modified the code as per the suggestions in the answers, but I am still getting the result for the first row only.

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

$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 个解决方案

#1


0  

Use while($row = mysql_fetch_row($result)) to loop through the rows.

使用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 );
}

#2


0  

You need to loop all the rows.

您需要循环所有行。

something like:

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

also, the php mysql extension is deprecated, have a look at mysqli or pdo.

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

#3


0  

You're using mysql_fetch_row, which to my understanding only returns one row (if any is found). Consider using mysql_fetch_all instead

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

#1


0  

Use while($row = mysql_fetch_row($result)) to loop through the rows.

使用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 );
}

#2


0  

You need to loop all the rows.

您需要循环所有行。

something like:

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

also, the php mysql extension is deprecated, have a look at mysqli or pdo.

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

#3


0  

You're using mysql_fetch_row, which to my understanding only returns one row (if any is found). Consider using mysql_fetch_all instead

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