0

for my work, i must to send multiples sms with an api and i want to put numbers in array from mysql like this :

(the numbers are in my database)

I want like this :

   $list_num = array(
        '0601020301','0601020302','0601020303','0601020304','0601020305','0601020306','0601020307','0601020308','0601020309','0601020310',
        '0601020311','0601020312','0601020313','0601020314','0601020315','0601020316','0601020317','0601020318','0601020319','0601020320',
        '0601020321','0601020322','0601020323','0601020324','0601020325','0601020326','0601020327','0601020328','0601020329','0601020330'
    );

i have try this, but doesnt work :

$result = mysql_query("SELECT * FROM test");
if (!$result) {
     die('Requête invalide : ' . mysql_error());
}
$a = array();
while ($row = mysql_fetch_assoc($result)) // moi je fais comme ca un mysql_fetch_* 
{
     $a[] = $row['numero'];
     foreach ($a as $val) {
          $ret .= "$val";
          if ($ret == end($a))
               //var_dump($val);
          {
               //echo "'".$val."'";
          }
          else
          {
               echo "'".$val."',";
          }
     }
}

Probably wrong method, i have search before here before, Thanks a lot.

chris85
  • 23,846
  • 7
  • 34
  • 51
Jeed
  • 25
  • 1
  • 7
  • 1
    What happens with this? You should indent your control blocks and upgrade to `mysqli` or `pdo`. – chris85 Apr 10 '17 at 12:40
  • 1
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 10 '17 at 12:40
  • Also if `mysql_fetch_*` isn't on the same line as `// moi je fais comme ca un` that will cause an error `//` is for a single line comment. – chris85 Apr 10 '17 at 12:43
  • thanks, i work on local with an old wamp version (5) i can't with pdo :'( – Jeed Apr 10 '17 at 12:58

2 Answers2

0

This should do the trick. You just need to adapt names/parameters to your needs. I used json, do something else if you need, or decode...

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

include"config.inc.php";

$mysqli = mysqli_connect("$host", "$user", "$mdp", "$db");

if (mysqli_connect_errno()) { echo "Error connecting : " . mysqli_connect_error($mysqli); }

$query = " SELECT id_test, name_test, phone_test FROM tbl_tests ";
$stmt = $mysqli->prepare($query);

$results = $stmt->execute();
$stmt->bind_result($id_test, $name_test, $phone_test);
$stmt->store_result();

if ($stmt->num_rows > 0) {
$phones = array();
$phone = array();
while($stmt->fetch()){
echo"[ $id_test -> $name_test -> $phone_test ]<br />";

$phone = "$phone_test";

array_push($phones, $phone);
 }
}
else
{ echo"[ no data ]"; }

//print_r($phones);
echo json_encode($phones);

?>

output : ["00544656347","0065465836","00656566745","0068456868","00626565064"]

EDIT: please As mysql_* was deprecated in PHP 5.5 (please refer to PHP doc) you should prefer PPS : Prepared Parameterized Statements. This will help Preventing SQL injection

Community
  • 1
  • 1
OldPadawan
  • 1,247
  • 3
  • 16
  • 25
  • Wow, this trick works so fine! thanks a lot!! json output is so good. – Jeed Apr 10 '17 at 13:46
  • So sorry, but after test with my php script, doesn't works :'( – Jeed Apr 10 '17 at 18:51
  • @Jeed : any error ? or no/wrong result ? if you need this type of structure as your array, `$list_num = array( '0601020301','0601020302');` you can easily get it out of the json format. Either you modify your script to work around with json format, or you just read the structure -> loop -> 1 entry -> one SMS. Have you used json_decode ? – OldPadawan Apr 10 '17 at 18:53
0

my script doesn't works with teh output json, however is a good trick. How to make like this :

$data = array(
array(
'number 1 from my database',
'number 2 from my database',
'number 3 from my database',
'number 4 from my database',
'number 5 from my database',
'number 6 from my database',
'number 7 from my database'
),
Jeed
  • 25
  • 1
  • 7
  • my previous comment (below) -> any error ? or no/wrong result ? if you need this type of structure as your array, $list_num = array( '0601020301','0601020302'); you can easily get it out of the json format. Either you modify your script to work around with json format, or you just read the structure -> loop -> 1 entry -> one SMS. Have you used **json_decode** ? – OldPadawan Apr 10 '17 at 19:18