2

I am trying to print all the items in a row from a particular table from a mysql database.

Here is the out put I am getting (which is not what I want):

**mysqli_result Object ( [current_field] => 0 [field_count] => 3 [lengths] => [num_rows] => 28 [type] => 0 ) Array ( )**

Here is how I am doing it:

<?php
    $connect = mysqli_connect("localhost", "root", "root");
    mysqli_select_db($connect, "TrackTool");

    $fetch1 = mysqli_query($connect, "SELECT * FROM SalesForceActions");

    // set array
    $array = array();

    // look through query
    while($row = mysql_query($fetch1)){

        // add each row returned into an array
        $array[] = $row;

    }

    print_r($fetch1);
    print_r($array);

    mysqli_close($connect);

?>

What am I doing wrong?

4
  • What do you want to obtain? Each row will be stored in an array. Commented Jul 26, 2013 at 3:05
  • why are you mixing mysql_ and mysqli_ functions? Your mysqli_ functions are not correct, as it looks like you are using the mysql_ syntax/parameters, but using the mysqli_ prefix. see php.net/manual/en/mysqlinfo.api.choosing.php for examples of each. Commented Jul 26, 2013 at 3:05
  • 1
    Something that doesn't pan out here. You say that your database name is ActionTable, yet you're trying to connect to TrackTool. Then there's SalesForceActions. Something I'm not getting? Commented Jul 26, 2013 at 3:19
  • 1
    @Fred yeah i messed up by forgetting to change it in the code.. whoops Commented Jul 26, 2013 at 3:39

2 Answers 2

5

You need to fetch the result after running the query. So you execute the query then loop which fetching from those results

Like so:

$fetch1 = mysqli_query($connect, "SELECT * FROM SalesForceActions");

$array = array();
while($row = mysqli_fetch_assoc($fetch1)){
    $array[] = $row;
}

You were also mixing mysqli_* functions with mysql_* functions. Be sure to NOT mix those. In fact, just use mysqli_* functions from now on as the mysql_* functions are being deprecated.

Sign up to request clarification or add additional context in comments.

Comments

3

You need to fetch the result using mysqli_fetch_assoc() (or equivalent):

$array = array();
while($row = mysqli_fetch_assoc($fetch1)) {
  $array[] = $row;
}
print_r($array);

If you indeed want all the rows, take a look mysqli_fetch_all():

$array = mysqli_fetch_all($fetch1, MYSQLI_ASSOC);
print_r($array);

6 Comments

thats returning the same thing.
so basically it is returning nothing it looks like this: "Array()"
This is not the full code. Replace the relevant code above. If it's still an empty array, then something else is wrong.
@Moose Another thing I don't get. This answer is the same as you accepted above, minus $fetch1 = mysqli_query($connect, "SELECT * FROM SalesForceActions"); which you already had (posted) and mysql_. Theoretically, this was the first correct answer, or was it the fact that your code didn't work with mysql_? Curious.
@Fred, its because the other answer pointed out that i messed up by forgetting the "i" in mysqli_*, therefore it was slightly better an answer in my case. Sorry, I can only choose one answer as correct, not two.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.