Sideshow
Sideshow

Reputation: 1351

Basic mysqli select

I have a select statement where I want to get all rows from a table but seem to be having a mental blockage - this should be elementary stuff but can't seem to get it working.

There are only two rows in the table 'postage_price' - and two columns : price | ref

Select statement is as follows:

$get_postage="SELECT price FROM postage_price ORDER BY ref DESC";
$get_postage_result=mysqli_query($dbc, $get_postage) or die("Could not get postage");
while($post_row=mysqli_fetch_array($dbc, $get_postage_result))
{
$post1[]=$post_row;
}

I am then trying to echo the results out:

echo $post1['0'];
echo $post1['1'];

this is not showing anything. My headache doesn't help either.

Upvotes: 1

Views: 411

Answers (5)

hakre
hakre

Reputation: 197757

If something does not work in a PHP script, first thing you can do is to gain more knowledge. You have written that

echo $post1['0'];
echo $post1['1'];

Is showing nothing. That could only be the case if those values are NULL, FALSE or an empty string.

So next step would be to either look into $post1 first

var_dump($post1);

by dumping the variable.

The other step is that you enable error display and reporting to the highest level on top of your script so you get into the know where potential issues are:

ini_set('display_errors', 1); error_reporting(~0);

Also you could use PHP 5.4 (the first part works with the old current PHP 5.3 as well, the foreach does not but you could make query() return something that does) and simplify your script a little, like so:

class MyDB extends mysqli
{
    private $throwOnError = true; # That is the die() style you do.

    public function query($query, $resultmode = MYSQLI_STORE_RESULT) {
        $result = parent::query($query, $resultmode);
        if (!$result && $this->throwOnError) {
            throw new RuntimeException(sprintf('Query "%s" failed: (#%d) %s', $query, $this->errno, $this->error));
        }
        return $result;
    }
}

$connection = new MyDB('localhost', 'testuser', 'test', 'test');
$query      = 'SELECT `option` FROM config';
$result     = $connection->query($query);
foreach ($result as $row) {
    var_dump($row);
}

Upvotes: 0

mrjimoy_05
mrjimoy_05

Reputation: 3568

while($post_row = mysqli_fetch_array($dbc, $get_postage_result))
{
    $post1[] = $post_row['price'];
}

As you see: $post_row in this line: = mysqli_fetch_array($dbc, $get_postage_result) is an array. You are trying to save the whole array value to another array in a block. :)

EDIT

while($post_row = mysqli_fetch_array($get_postage_result))
...

Upvotes: 1

Sideshow
Sideshow

Reputation: 1351

with all your help I have found the error - it is in the mysqli_fetch_array where I had the $dbc which is not required.

$get_postage="SELECT price FROM postage_price ORDER BY ref DESC";
$get_postage_result=mysqli_query($dbc, $get_postage) or die("Could not get postage");
while($post_row=mysqli_fetch_array($get_postage_result))
{
$post1[]=$post_row['price'];
}

instead of:

$get_postage="SELECT price FROM postage_price ORDER BY ref DESC";
$get_postage_result=mysqli_query($dbc, $get_postage) or die("Could not get postage");
while($post_row=mysqli_fetch_array($dbc, $get_postage_result))
{
$post1[]=$post_row['price'];
}

Bad day for me :(

Thanks all

Upvotes: 0

Sergey P. aka azure
Sergey P. aka azure

Reputation: 4742

You have $post1[]=$post_row; and $post_row is itself an array. So you can access post data with following: $post1[NUMBER][0] where NUMBER is a $post1 array index and [0] is 0-index of $post_row returned by mysqli_fetch_array.

Probably you wanted to use $post1[]=$post_row[0]; in your code to avoid having array of arrays.

Upvotes: 1

narruc
narruc

Reputation: 350

You are passing 1 and 0 as string indexes, this would only work if you had a column called 0 or 1 in you database. You need to pass them as numeric indexes.

Try:

print_r($post1[0]);
print_r($post1[1]);

or

print_r($post['price']);
print_r($post['ref']);

Upvotes: 0

Related Questions