I am trying to send SMS to multiple recipients. The code I am trying to achieve this is like this:
The problem I am facing is the message is being delivered to only the first record in the table.
Need some help, what & where I am going wrong?
$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 );
}
You need to stick to Joomla coding standards. Don't go using mysql_* functions as they are deprecated and pose security threats.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*')
->from('members');
$db->setQuery($query);
$rows = $db->loadObjectList();
while($rows){
foreach ( $rows as $row ) {
$mobile = $row->mobile;
$name = $row->name;
}
//rest of code
}
Related
I have this code:
$query="Select SUBJECT,NOTES from CAMPNOTIFICATION
where TYPE LIKE 'message_blackboard' AND VALIDAFTER <= GETDATE() AND (VALIDUNTIL >= GETDATE() OR VALIDUNTIL IS NULL)";
$encode = array();
//$query = strtr($query, array('{$raum}' => $raum));
$query_result = mssql_query($query);
while ($row = mssql_fetch_row($query_result))
{
$encode[] = $row;
$text = $row[1];
$text = str_replace("<br />","\n",$text);
$text = str_replace("<br>","\n",$text);
$text = str_replace("<br/>","\n",$text);
$text = str_replace("<p>","\n",$text);
$text = str_replace("\r","",$text);
$text = strip_tags($text);
$text = str_replace("\n","<br>",$text);
$text = str_replace("<br>\r<br>","",$text);
$text = str_replace("<br><br>","<br>",$text);
echo "<h2>" . $row[0] . "</h2>" . $text . "<br>";
}
I have to change the connections to the sqlsrv model. I managed to do it this way:
$query="Select SUBJECT,NOTES from CAMPNOTIFICATION
where TYPE LIKE 'message_blackboard' AND VALIDAFTER <= GETDATE() AND (VALIDUNTIL >= GETDATE() OR VALIDUNTIL IS NULL)";
//$params = array(SQLSRV_PHPTYPE_*);
$encode = array();
$query_result = sqlsrv_query($connection, $query);
if ($query_result === false){
die(print_r( sqlsrv_errors(), true));
}
while ($row = sqlsrv_fetch_object($query_result))
{
//echo "Contenido de Row ". $row -> NOTES;
$encode[] = $row;
$text = $row -> NOTES;
$text = str_replace("<br />","\n",$text);
$text = str_replace("<br>","\n",$text);
$text = str_replace("<br/>","\n",$text);
$text = str_replace("<p>","\n",$text);
$text = str_replace("\r","",$text);
$text = strip_tags($text);
$text = str_replace("\n","<br>",$text);
$text = str_replace("<br>\r<br>","",$text);
$text = str_replace("<br><br>","<br>",$text);
echo "<h2>" . $row -> SUBJECT . "</h2>" . $text . "<br>";
}
But I need to keep the structure in which I use the position of the array instead of calling the object.
Does anyone know of any way? Thank you very much.
Solution:
Function mssql_fetch_row() is part of the MSSQL PHP extension (mssql_ functions) and fetches one row of data as a numeric array. If you want to get the data in similar way using PHP Driver for SQL Server (sqlsrv_ functions), you should use sqlsrv_fetch_array() with SQLSRV_FETCH_NUMERIC as second parameter value.
Your code should look like this:
<?php
....
while ($row = sqlsrv_fetch_array($query_result, SQLSRV_FETCH_NUMERIC)) {
$encode[] = $row;
// Additional code here ...
}
...
?>
Additional explanations:
If the SELECT statement returns more than one result set, you need to use sqlsrv_next_result() to make the next result of the specified statement active.
Example, using MSSQL extension:
<?php
// Connection
$server = "server\instance";
$database = "database";
$username = "username";
$password = "password";
$conn = mssql_connect($server, $username, $password);
mssql_select_db($database, $conn);
// Statement
$sql = "
SELECT [name], [age] FROM [dbo].[persons];
SELECT [name], [salary] FROM [dbo].[jobs];
";
$stmt = mssql_query($sql, $conn);
// Fetch data
do {
while ($row = mssql_fetch_row($stmt)) {
echo print_r($row, true);
}
} while (mssql_next_result($stmt));
// End
mssql_free_result($stmt);
mssql_close($conn);
?>
Example, using SQLSRV extension:
<?php
// Connection
$server = "server\instance";
$database = "database";
$username = "username";
$password = "password";
$info = array(
"Database" => $database,
"UID" => $username,
"PWD" => $password
);
$conn = sqlsrv_connect($server, $info);
// Statement
$sql = "
SELECT [name], [age] FROM [dbo].[persons];
SELECT [name], [salary] FROM [dbo].[jobs];
";
$stmt = sqlsrv_query($conn, $sql);
// Fetch data
do {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo print_r($row, true);
}
} while (sqlsrv_next_result($stmt));
// End
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
?>
Function equivalence:
The table below displays more information about the equivalence between the functions from each extension:
--------------------------------------------------------------------------------------
MSSQL PHP extension PHP Driver for SQL Server
--------------------------------------------------------------------------------------
mssql_fetch_assoc($stmt) sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)
mssql_fetch_row($stmt) sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)
mssql_fetch_array($stmt, MSSQL_ASSOC) sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)
mssql_fetch_array($stmt, MSSQL_NUM) sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)
mssql_fetch_array($stmt, MSSQL_BOTH) sqlsrv_fetch_array($stmt, SQLSRV_FETCH_BOTH)
mssql_next_result($stmt) sqlsrv_next_result($stmt)
I had a code that works for addsmanager component but they make some changes and now dont work anymore.
Old code is this
function getAdsmanagerRouteContent($id)
{
$db =JFactory::getDBO();
$sql = "SELECT category,ad_manufacturers,ad_modelauto,ad_jetmodels,ad_model FROM #__adsmanager_ads WHERE id = ".(int)$id;
$db->setQuery($sql);
$result = $db->loadObject();
$url = TText::_($result->ad_manufacturers);
if ($result->ad_modelauto != "") {
$url .= "-".TText::_($result->ad_modelauto );
}
if ($result->ad_jetmodels != "") {
$url .= "-".TText::_($result->ad_jetmodels );
}
if ($result->ad_model != "") {
$url .= "-".TText::_($result->ad_model );
}
$url = TTools::stringURLSafe($url );
$url = JString::substr($url ,0,30);
return $url ;
}
They have this code now
function getAdsmanagerRouteContent($id)
{
$db =JFactory::getDBO();
$sql = "SELECT ad_headline FROM #__adsmanager_ads WHERE id = ".(int)$id;
$db->setQuery($sql);
$result = $db->loadResult();
$result= TTools::stringURLSafe($result);
$result = JString::substr($result,0,30);
return $result;
}
wich i have it modified like this
function getAdsmanagerRouteModel($id)
{
$db =JFactory::getDBO();
$sql = "SELECT ad_model,ad_motomodel FROM #__adsmanager_ads WHERE id = ".(int)$id;
$db->setQuery($sql);
$result = $db->loadResult();
if ($result->ad_model != "") {
$result .= "-".TText::_($result->ad_model );
}
if ($result->ad_motomodel != "") {
$result .= "-".TText::_($result->ad_motomodel );
}
$result= TTools::stringURLSafe($result);
$result = JString::substr($result,0,30);
return $result;
}
The ad_model is loaded but if instead ad_model is ad_motomodel this isn t loaded.
Is a way to make this code work? I am not such good at php .
Your code was originally written to work with two different variables ($result and $url) but uses one instead.
$result = $db->loadResult();
if ($result->ad_model != "") {
$result .= "-".TText::_($result->ad_model );
}
Once the thrid line gets executed, you now don't have the object $result anymore. It is gone. You now have a string $result containing whatever the "stringification" of your previous object yields (an empty string in this case) plus a dash and the contents of ad_model. After that, you cannot access $result->ad_motomodel anymore.
What you really want is something like this:
$db =JFactory::getDBO();
$sql = "SELECT ad_model,ad_motomodel FROM #__adsmanager_ads WHERE id = ".(int)$id;
$db->setQuery($sql);
$result = $db->loadResult();
$url="";
if ($result->ad_model != "") {
$url .= "-".TText::_($result->ad_model );
}
if ($result->ad_motomodel != "") {
$url .= "-".TText::_($result->ad_motomodel );
}
$url = TTools::stringURLSafe($url);
$url = JString::substr($url,0,30);
return $url;
I created a cronjobs on direct admin, and if that running it will check curl =>>> change values in mysql. But it's not working. Please help me ##. Thanks
This is my code:
<?php
class ControllerVemaybayCronJobapp
{
function index(){
$connect = $this->connect();
$query_routes_1 = $connect->query("SELECT * FROM **** where status = '0' GROUP BY routes ");
for ($result = array();
$row = $query_routes_1->fetch_assoc();
$result[array_shift($row)] = $row);
foreach ($result as $i => $aaa){
$routes = $aaa['routes'];
$detail_routes_2 = $connect->query("SELECT * FROM **** where routes = '" . $routes . "' AND status = '0' ");
for ($result2 = array();
$row2 = $detail_routes_2 ->fetch_assoc();
$result2[array_shift($row2)] = $row2);
foreach ($result2 as $t => $value1) {
$ngay = $value1['ngay'];
$thang = $value1['thang'];
$nam = $value1['nam'];
$min = $value1['minPrice'];
$max = $value1['maxPrice'];
$providers = $value1['providers'];
$startdate = $nam.$thang.'01';
$enddate = $nam.$thang.'31';
$bien = $this->getlist_ticketsofdate($routes,$startdate,$enddate);
foreach ($bien as $k => $value) {
$moi2 = array();
$moi = $value['c'];
$moi2 = $value['f'];
if($value['_id']['dim'] == $ngay ){
if($min <= $value['c'] && $value['c'] <= $max && strpos($providers, $value['p']) !== false){
$connect->query("UPDATE **** SET status='1' where customer_id = '" . $value1['customer_id'] . "' ");
break;
}else{
foreach ($moi2 as $key => $value2) {
if($min <= $value2['cp'] && $value2['cp'] <= $max && strpos($providers, $value2['p']) !== false){
$connect->query("UPDATE **** SET status='1' where customer_id = '" . $value1['customer_id'] . "' ") ;
break;
}
}
}
}
}
}
}
}
function connect(){
$servername = "****";
$username = "****";
$password = "****";
$databasename = "****";
$conn = new mysqli($servername, $username, $password,$databasename);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
return $conn;
}
function getlist_ticketsofdate($diadiem,$startDate,$endDate){
$url = '****';
$providers = array();
$providers[0] = '****';
$providers[1] = '****';
$providers[2] = '****';
$routes = array();
$routes[0] = $diadiem;
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Connection:keep-alive';
$param = array(
'startDate' => $startDate,
'endDate' => $endDate,
'minPrice' => '0',
'maxPrice' => '700000',
'providers' => $providers,
'routes' => $routes,
'type' => 'date',
);
$data_string = json_encode($param);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$result_tickets = json_decode($response,true);
return $result_tickets;
}
}
?>
This is my cronjobs
Cron job may not works directly on admin, cause it may required authentications for some process.
Sample php file name mail.php
<?php
// the message
$msg = "First line of text\nSecond line of text";
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// send email
mail("someone#example.com","My subject",$msg);
?>
Cron command:
For url
* * * * * wget http://example.com/mail.php &> /dev/null
For Path
* * * * * <BASE DIR>/mail.php
This cron job send mail every minute.
I am passing value from android using POST and run the query in SQL DB using PHP. I am always getting
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 1.
The code is working fine if i pass the values directly insted $_POST[].
I've gone through SO but no questions solved my problem. I tried with mysql_real_escape_string($searchTermBits) also but that doesn't worked.
PHP:
<?php
$username = "xxxx";
$password = "xxxx";
$host = "xxxx";
$database = "xxxx";
$server = mysql_connect($host, $username, $password);
$connection = mysql_select_db($database, $server);
$search = $_POST["deskey"];
$search = explode(" ", $search);
$commonwords = "a,an,and,I,it,is,do,does,for,from,go,how,the,etc,in,on,are";
$commonwords = explode(",", $commonwords);
foreach ($search as $value)
{
if (!in_array($value, $commonwords))
{
$query[] = $value;
}
}
$query = implode(" ", $query);
$searchTerms = explode(" ", $query);
$searchTermBits = array();
foreach ($searchTerms as $term)
{
$term = trim($term);
if (!empty($term))
{
$searchTermBits[] = "description LIKE '%$term%'";
}
}
$myquery = "SELECT * FROM `logins` WHERE " . implode(' OR ', mysql_real_escape_string($searchTermBits)) . "";
$query = mysql_query($myquery);
if (!$query)
{
echo mysql_error();
die;
}
$data = array();
for ($x = 0; $x < mysql_num_rows($query); $x++)
{
$data[] = mysql_fetch_assoc($query);
}
echo json_encode($data);
mysql_close($server);
?>
What changes can i make this code to work with $_POST[].
I have a function in PHP and I would like to export an array from my MYSQL Database and then use the rows in a loop to do some stuff with them.
$DB_Server = "XXX.XXX.XXX.XXX";
$DB_Username = "XXXX";
$DB_Password = "XXXXX";
$DB_Database = "XXXXX";
$conn = new mysqli($DB_Server, $DB_Username, $DB_Password, $DB_Database);
$query = "Select Name, Wert from test.DBPBX";
function show(array $options) {
global $showresult, $master, $conn, $query;
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)) {
$cnlongname = $row["Name"];
$usercontent = $row["Wert"];
$cn = $cnlongname;
$options['config']->value = 1;
$config = $options["config"]->value;
$show = new SimpleXMLElement("<show/>");
$user = $show->addChild("user");
$user->addAttribute("cn", $cn);
if ($config)
$user->addAttribute("config", "true");
print "cmd: " . htmlspecialchars($show->asXML()) . "\n";
$showresult = $master->Admin($show->asXML());
print "result: " . htmlspecialchars($showresult) . "\n";
$mod = "text=".$usercontent;
$modify = new SimpleXMLElement("$showresult");
$user = $modify->user;
$path = explode("/device/hw/", $mod);
$srch = $user;
$nsegments = count($path);
$i = 1;
foreach ($path as $p) {
if ($i == $nsegments) {
// last part, the modification
list($attr, $value) = explode("=", $p);
$srch[$attr] = $value;
} else {
$srch = $srch->$p;
}
$i++;
}
$modify = new SimpleXMLElement("<modify>" . $user->asXML() . "</modify>");
print "cmd: " . htmlspecialchars($cmd = $modify->asXML()) . "\n";
// do it
$result = $master->Admin($cmd);
print "result: " . htmlspecialchars($result);
}
}
For $cn I would like to use $cnlongname (or $row["Name"]). And for $mod I would like to use $usercontent (or $row["Wert"]). However when I would like to use it I get an error after the first loop:
Warning: mysqli_fetch_assoc() expects parameter 1 to be
mysqli_result, string given in /sample.php on
line 175
Line 175 is:
while ($row = mysqli_fetch_assoc($result)) {
Can you please help me?
Inside your while loop you overwrite your result, therefore you lose your mysql result and cannot query it any more.
// do it
$result = $master->Admin($cmd);
Use a different variable there. :)