Getting an array from DB

Next code snippet is working. And I am getting just one (first) variable.
   
   
     if (isset($_POST["getProjectsList"]) ) { 
          $weGotTheuser= mysqli_real_escape_string($db2, $_POST['weGotTheuser']);
          $sql = "SELECT projectName FROM partners  WHERE user2='$weGotTheuser'";
          $query = mysqli_query($db2, $sql);
		   $row = mysqli_fetch_row($query);
                    $myLine=$row['0'];
                    echo $myLine;
       }

Open in new window


But as I am having many "projectname" i have to make an array.
With next code I can not get anything. What I am doing wrong?
 if (isset($_POST["getProjectsList"]) ) { 
          $weGotTheuser= mysqli_real_escape_string($db2, $_POST['weGotTheuser']);
          $sql = "SELECT projectName FROM partners  WHERE user2='$weGotTheuser'";
          $query = mysqli_query($db2, $sql);
          while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
	  array_push($ProjectsList, $row["projectName"]);
	  }
	  echo $ProjectsList;
       }

Open in new window

Ivan GolubarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
I use the following method to put rows into an array.  However, you cannot just 'echo' an array.  You have to use something like 'foreach' to extract all of the array elements to display them.
 if (isset($_POST["getProjectsList"]) ) { 
      $ProjectsList = array();
          $weGotTheuser= mysqli_real_escape_string($db2, $_POST['weGotTheuser']);
          $sql = "SELECT projectName FROM partners  WHERE user2='$weGotTheuser'";
          $query = mysqli_query($db2, $sql);
          while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
	  $ProjectsList[] = $row["projectName"];
	  }
	//echo $ProjectsList;
       }

Open in new window

0
Chris StanyonWebDevCommented:
If you fancy using PDO instead of mySQLi, there is a built in way to do this:

if ( isset($_POST["getProjectsList"]) ):
    $dbh = new PDO("mysql:host=yourHost;dbname=yourDb", "username", "password");

    $query = $dbh->prepare("SELECT projectName FROM partners  WHERE user2 = ?");
    $query->execute( array($_POST['weGotTheuser']) );

    $data = $query->fetchAll(PDO::FETCH_COLUMN, 0);
endif;

Open in new window

$data will be an array containing all your values. The code I've posted also uses a parameterised query for added security so there's no need to escape the POST data :)
0
Ivan GolubarAuthor Commented:
function  getProjectsListForUserF(){  

 $.ajax({
  method:"POST",
  url: '/wp-content/themes/net4/addProjectsForUser.php',
  data:  {
      "getProjectsListForUser":1,
       "weGotTheuser":weGotTheuser
   },
   datatype: "text",
    success: function(strdate){
        console.log(strdate);

Open in new window


Dave Baldwin
I need echo for ajax above.
0
Chris StanyonWebDevCommented:
You're going to need to do a couple of things. Firstly, once you've got your array in your PHP file, you will need to JSON encode it:

while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
    $ProjectsList[] = $row["projectName"];
}
echo json_encode($ProjectsList);

Open in new window

or using PDO:

echo json_encode( $query->fetchAll(PDO::FETCH_COLUMN, 0) );

Open in new window

You will then need to tell your AJAX script that you expect JSON back. This will allow you to then loop through the array as you need:

$.ajax({
    method: 'post',
    url: '/wp-content/themes/net4/addProjectsForUser.php',
    data: {
        'getProjectsListForUser' : 1,
        'weGotTheuser' : weGotTheuser
    },
    dataType: 'json',
    success: function(data){
        $.each(data, function(key, value) {
            console.log(value);
        }); 
    }
});

Open in new window

Couple of things to note. The property for the data type is dataType, not datatype (note the capital T). Also, in your AJAX call, you are sending a POST variable called getProjectsListForUser but in your PHP, you're checking for one called getProjectsList
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.