2

I am new to PHP. I am working on a project and tying to learn PHP. But I am facing a hectic problem which i don't know why this happening. I have a table in database name gift_items And I am trying to fetch data of that table through PHP code. But when run the query in php and my browser show me some data not the complete data from data base.

Here is my ServerConnection.php

<?php  

$Server = 'localhost';
$username = 'root';
$password = '';


$database = 'db_gifters';


$connection = mysqli_connect($Server,$username,$password);

if($connection)
{
    mysqli_select_db($connection,$database);
}
else
{
    echo "Could not connect to server";
}


?>

And part of php code in index.php where i am actually running the query.

<?php 
    $Listquery = "select giftname, gifttype from gift_items order by 
    gifttype,giftname";                                                    

    $gifttype_query = "select distinct gifttype from gift_items";                                                    

    $ListqueryResult = mysqli_query($connection,$Listquery);                                                    

    $gifttype_queryResult = mysqli_query($connection,$gifttype_query);                                                    

    $Listresult = mysqli_fetch_array($ListqueryResult);                                                    

    $gifttype_result = mysqli_fetch_array($gifttype_queryResult);                                                    

    foreach ($gifttype_result as $value)                                                     
    {
        echo $value;
    }
?>

The output is as given below: Its gives me the output of same data twice instead of two different data values. As given enter image description here

Here i am also attaching the screen shoot of result in database with same query adn it gives me accurate result but in php code its gives me same value twice as output.

enter image description here Can someone resolve this issue or tell me what is the actual problem in my script code ??? is there any logical error.??

10
  • Also, please do not abuse the snippet tools. That is for HTML/CSS/Javascript only. Commented Aug 10, 2017 at 18:08
  • Fine. But I just used it for better understating. What the actual problem is. Commented Aug 10, 2017 at 18:11
  • Output $connection->error Where #Grumpy ?? Commented Aug 10, 2017 at 18:13
  • 2
    change $gifttype_result = mysqli_fetch_array($gifttype_queryResult); to $gifttype_result = mysqli_fetch_array($gifttype_queryResult, MYSQLI_ASSOC); Commented Aug 10, 2017 at 18:18
  • 1
    while($row = mysqli_fetch_array($gifttype_queryResult, MYSQLI_ASSOC)) { echo $row['gifttype']; } Commented Aug 10, 2017 at 18:22

1 Answer 1

1

You need to fetch the data as an Associative Array rather than an Indexed Array. It will solve the issue.

There is another post although using PDO that explains the reason for this: PHP why does my function return twice the result of the array with different keys?

In this case just to each query you want to get the array from.

mysqli_fetch_array([ RESULT VARIABLE HERE ],MYSQLI_ASSOC)

$Listresult = mysqli_fetch_array($ListqueryResult,MYSQLI_ASSOC);                                                    

$gifttype_result = mysqli_fetch_array($gifttype_queryResult,MYSQLI_ASSOC);                                                    

foreach($gifttype_result as $value)                                                     
{
    echo $value['gifttype'];
}

More information can be read here: Assoc. Array Fetch courtesy of PHP.net

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

2 Comments

when i run this code it gives error "Warning: Illegal string offset 'gifttype' in " at echo $value['gifttype'];
It usually means your trying to use a string as an array. before the foreach loop var_dump($Listresult) and var_dump($gifttype_result) and show your results on here.

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.