Results 1 to 5 of 5

Thread: Echo won't work

  1. #1
    Join Date
    Dec 2008
    Location
    Netherlands
    Posts
    58

    Unanswered: Echo won't work

    I'm using code to know how many rows there, if I put the query in phpmyadmin I get an good result, 5 at this moment. For some reason the number won't show up when I echo the code as shown below.
    I get an error: Undefined variable
    Code:
    <?php
    $con = mysql_connect("******", "*****", "*******");
           mysql_select_db("*****");
        return $con;
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    $query = "SELECT COUNT(*)
    AS
    aantal
    FROM offertenummers
    WHERE status_id =1";
    $result = mysql_query($query);
    
    if($result) {
      if(mysql_num_rows($result) == 1) {
        $rij = mysql_fetch_assoc($result);
        echo $rij['aantal'];
      } else {
        echo 'Geen records gevonden.';
      }
    } else {
      echo mysql_error().' in query: '.$query;
    }
    ?>
    Code:
    <?php echo $rij['aantal']; ?>
    Any ideas???

    This code is in header.php taht is included in different php files that also have an connection to my database.

  2. #2
    Join Date
    Nov 2003
    Posts
    2,933
    Provided Answers: 12
    That's a PHP problem, not a SQL or MySQL problem!

  3. #3
    Join Date
    Dec 2008
    Location
    Netherlands
    Posts
    58
    Sorry about the wrong forum and thank you for moving it to the right one.

  4. #4
    Join Date
    Sep 2003
    Location
    So. Cal. USA
    Posts
    142
    I could be mistaken, but seems to me that it will echo only if there is 1 record selected. If there are 5 records expected, you are probably seeing 'Geen records gevonden.'

    Try...
    if(mysql_num_rows($result) > 0)

  5. #5
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    This line is wrong

    line3: "return $con;"

    You're not inside a function, therefore you can't return anything.

    Also, you need to perform a loop on the resultset to get them all. Try the following to see the results:
    Code:
    ...
    if($result) {
      while($rij = mysql_fetch_assoc($result)){
         print_r($rij);
      }
      
    } else {
      echo mysql_error().' in query: '.$query;
    }

Posting Permissions

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