#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    12
    Rep Power
    0
    Hi can any one tell me please why i keep getting a value of 2 when i include this scipt (i know i have over 200 items)it works fine from a mysql prompt as select count(FIELD) from table; gives correct result
    but this in php gives a result of 2 ?


    $item_count = "SELECT count(FIELD)FROM table";


    $item_result = mysql_query($item_count)
    or die("Couldn't get count");

    if ($item_result == "1") {
    echo "<strong>1</strong> item.";
    } else {
    echo "<strong>$item_result</strong> items.";
    }

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    32
    Rep Power
    15
    I think you still need to use,
    mysql_fetch_array.

    eg,

    $item_count = "SELECT count(FIELD) AS numbers FROM table";

    $item_result = mysql_fetch_array(mysql_query($item_count))
    or die("Couldn't get count");

    if ($item_result['numbers'] == "1") {
    echo "<strong>1</strong> item.";
    } else {
    echo "<strong>$item_result['numbers']</strong> items.";
    }

    good luck,
    scoobydoo
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    12
    Rep Power
    0
    Hi scoobydoo thanks for replying, i tried this and got the following error:

    Parse error: parse error, expecting `STRING' or `NUM_STRING' or `'$'' in


  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    32
    Rep Power
    15
    oops.. sorry marcus.. made a big mistake
    it's $item_result["numbers"]

    " not the '

    scoobydoo
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    12
    Rep Power
    0
    Hi scoobydoo , i made the changes but still get the same error?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Posts
    63
    Rep Power
    15
    The result your getting is the integet id of the result, not the result itself. For example, if I run a query against the database
    $connection = mysql_connnect($host,$user,$pwd);
    echo $connection; // This will be an integer identifier for a php connection
    $result = mysql_query("select * from customers", $connection);
    echo $connection; // This will be an integer identifier for a php result set

    // Now you need to get the data out of the result set....

    $last_name = mysql_result($result, 0, "last_name"); // where 0 is the first row in the result set

    // So to get your query to work properly,
    //you need to read out the result as I did
    //above but remember to name the field
    //otherwise you have to access it by offset
    //and not by name

    $result = mysql_query("select count(*) as mycount from customers", $connection);

    $thecount = mysql_result($result,0,"mycount");

    // or if you leave out the as in the sql:

    $result = mysql_query("select count(*)from customers", $connection);

    $thecount = mysql_result($result,0,0);

    // You can also use mysql_fetch_array which
    //moves a whole result set into an array
    //which you can then manipulate in php.

    mssql_fetch_array($result);


Similar Threads

  1. Replies: 13
    Last Post: June 4th, 2004, 05:49 PM
  2. Need advice designing a usage tracking database (MySQL)
    By Randolpho in forum Database Management
    Replies: 0
    Last Post: February 13th, 2004, 05:11 PM
  3. Replies: 3
    Last Post: February 11th, 2004, 09:30 AM
  4. Replies: 14
    Last Post: February 8th, 2004, 02:28 PM
  5. Replies: 5
    Last Post: February 3rd, 2004, 02:59 PM

IMN logo majestic logo threadwatch logo seochat tools logo