Results 1 to 7 of 7

Thread: PHP wannaby needing help with echoing value from PHP query

  1. #1
    Join Date
    Feb 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP wannaby needing help with echoing value from PHP query

    Hi All,

    Hope you are good. I need a little advice if poss, more so in the form of "ah your code line should be" instead of references to manuals. I find manuals, although helpful in cases, take up lots of time and dont often give a good enough example where I have specific probs (partly my ignorence I am sure, but I am learning to this will become less an issue in time). Manuals so far have given me a very good general understanding though, so not dissing them per say. Anyway, 'nuf of ramble

    If have a need to acquire the largest value from a column in a table and turn it into a PHP variable. I have:
    ___________________________________________________

    $connent = connect to db

    $query = "SELECT MAX(column name) FROM table";
    $result = mysqli_query($connect,$query) or die ("die statement");

    $row = mysqli_fetch_row($result);
    echo = $row;
    ___________________________________________________

    $connent = works
    $query; when typing the same query straight into phpMyAdmin I receive my desired result, number 'X'

    I use "echo $row" as I am expecting it to provide the same result as my SQL query in myPHPAdmin.

    It doesn't, it displays "Array".

    I have tried:

    mysqli+fetch_row
    mysqli+fetch_assoc
    mysqli+fetch_array

    No change in output.

    I am under the impression this 'should' make $row = 'number X', therefore when I echo $row I should see number 'X'. If I can see the page display 'number X' I knowt I can then turn $row into a better named variable for the rest of my script.

    Does anybody have any suggestions?

    Many thanks

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    PHP can't echo an Array, it comes across an Array and then just returns "Array" to let you know it's there. You can do one of two things:

    1. Use print_r which means print readable so it will print the array so that you can see the contents.

    Take for example the following array:

    PHP Code:
    <?php

    $fruits 
    = array("Banana""Strawberry""Tomato"// Yes tomato is a fruit

    echo $fruits// Prints "Array" to the screen, doesn't show the values within it.

    // But if we use print_r it will print the following

    print_r($fruits);

    // Prints : Array ( [0] => Banana [1] => Strawberry [2] => Tomato ) 
    ?>
    2. Use echo to access an individual item in the array:

    PHP Code:
    // To access one of these values you need to put an index

    echo $fruits[1]; // Will echo Strawberry, remember the first item is at 0 
    You just need to put $row['something'], depending on which column you want to access.

    So something like:

    PHP Code:
    <?php

    $con 
    mysqli_connect("localhost""root""pass");

    $query ="SELECT something FROM table";

    $result mysqli_query($query$con);

    while (
    $row mysqli_fetch_array($result))
    {
    echo 
    $row['something'] . "<br />"// Echos all records within the selected column
    }

    ?>
    Last edited by Schmoopy; 02-05-2009 at 12:27 PM.

  3. #3
    Join Date
    Feb 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Sorted:

    Below the query script I need to include the following -

    $row = mysqli_fetch_assoc($result);
    $max_'variable name' = $row[0]]
    echo $max_'variable name'

    I can now see my maximum variable number as $max_'variable name', thus can use $max_'variable name' is other parts of my code where I need to.

    I knew it would be quite simple in terms of some additional code with correct syntax after the initial query.

    I've now got this working to pull two max variable values out of my table The rest of my code should be easy now I have obtained two maximums I needed

  4. #4
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    PHP Code:
    $row mysqli_fetch_assoc($result);

    echo 
    $row['column name']; 
    Schmoory hit on it. The "assoc" part of mysqli_fetch_assoc() means return an associative array. So you can use the column name to get to the value of each variable in the array.

    If there is only one value in your result set, $row['0'] will work to get that one value. If there is more than one value and depending on what you are doing, using the numerical index can get complicated. At least for me.

  5. #5
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    But the strange thing is if you use mysql_fetch_array() you can still access values in the array by saying $row['title'], so it's still an associative array, that's why I don't really see the point in the fetch_assoc() when you can do the same with the other one.

    Can anyone explain it?

  6. #6
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    mysqli_fetch_assoc() only returns an associative array.

    mysqli_fetch_array() returns by default both an associative array and a numerical. Or more specificlly one array with the values duplicated $row['0'] = awesome, $row['jasondfr'] = awesome (joking)

    You can add a second parameter:

    mysqli_fetch_array($result, MYSQLI_NUM) to return only a numerical.

    mysqli_fetch_array($result, MYSQLI_ASSOC) to return only an associative.

    mysqli_fetch_array($result, MYSQLI_BOTH) to return both. This is the default.

    As a rule of thumb, don't pull in any more data than you need. So, if you don't need a numerical indexed array, don't return it. And vice-versa.
    Last edited by JasonDFR; 02-05-2009 at 05:00 PM.

  7. #7
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Ah I see, thanks for clearing that up.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •