Error when extracting data from MySQL .. ?

Discussion in 'PHP' started by misterdh, Jul 15, 2010.

  1. #1
    Hi

    I am having troubles extracting data from my MySQL database.. The strange thing is that this code has worked before.. I just copied and pasted (changing the nessessary names of cource).

    <?php
    ini_set("display_errors", true);
    error_reporting(-1);
    
    include("connect.php");
    
    $extract = mysql_query("SELECT * FROM suggest");
    $numrows = mysql_num_rows($extract);
    
    while ($row = mysql_fetch_array($extract))
    {
    $url[$i]=$row['url'];
    $i++;
    }
    
    $id=1;
    while($id < count($name))
      {
      echo "The URL is " . $url[$id] . "<br />";
      $id++;
      }
    
    
    
    ?>
    PHP:
    And the error msg I get is:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/haukaas/public_html/new/suggest-search-engine/admin/index.php on line 8
    
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/haukaas/public_html/new/suggest-search-engine/admin/index.php on line 10
    
    Notice: Undefined variable: name in /home/haukaas/public_html/new/suggest-search-engine/admin/index.php on line 17
    PHP:
    Can anyone help me? :(
     
    misterdh, Jul 15, 2010 IP
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Its basically saying your theres a problem with your SQL. Do you have the correct table name specified? (suggest?)

    You might want to try adding or die(mysql_error()); onto the end of the SQL statements for more information into the actual problem.

    Also.

    while($id < count($name))
    PHP:
    $name is not defined..... it looks like that should be

    while($id < count($url))
    PHP:
     
    lukeg32, Jul 15, 2010 IP
  3. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #3
    You might not be connecting to mysql properly, didn't do mysql_seletct_db() or table doesn't exist.
     
    Kaizoku, Jul 15, 2010 IP
  4. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    <?php
        ini_set("display_errors", true);
        error_reporting(-1);
    
        include("connect.php");
    
        // you have not tested what has been returned,
        // don't assume that the functon has worked
        $extract = mysql_query('SELECT * FROM suggest') || die('Error: ' . mysql_error());
        $numrows = mysql_num_rows($extract);
    
        // always declare variables
        $i = 0;
        $url = array();
        while ($row = mysql_fetch_array($extract) {
            $url[$i] = $row['url'];
            $i++;
        }
    
        
        // no idea what you're trying to do here. What is $name?
        $id = 1;
        while ($id < count($name)) {
          echo "The URL is " . $url[$id] . "<br />";
          $id++;
        }
    ?>
    
    PHP:
     
    Deacalion, Jul 15, 2010 IP
  5. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok first of all I was trying to connect to the wrong database (Stupid me! Yes I know!)

    I also made some changes to the code and now all the error msg is gone and it is diplaying the last entry in the mysql db.

    How can I make it display all entries?

    Here is the code:

    <?php
    ini_set("display_errors", true);
    error_reporting(-1);
    
    include("connect.php");
    
    $extract = mysql_query("SELECT * FROM suggest");
    $numrows = mysql_num_rows($extract);
    
    while ($row = mysql_fetch_array($extract))
    {
    $url=$row['url'];
    }
    
    
      echo "The URL is " . $url . "<br />";
    
    ?>
    PHP:
     
    misterdh, Jul 15, 2010 IP
  6. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #6
    Because you are replacing the $url in the while loop. put the echo in the while loop also.
     
    Kaizoku, Jul 15, 2010 IP
  7. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yep.

    
    <?php
        ini_set("display_errors", true);
        error_reporting(-1);
    
        include("connect.php");
    
        $extract = mysql_query("SELECT * FROM suggest");
        $numrows = mysql_num_rows($extract);
    
        while ($row = mysql_fetch_array($extract)) {
            echo 'The URL is ' . $row['url'] . '<br />'; 
        }
    ?>
    
    PHP:
     
    Deacalion, Jul 15, 2010 IP
  8. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Of cource! :)

    Im a newbie so this isnt logic for me yet ... :)

    Thanks alot for the help guys :)
     
    misterdh, Jul 15, 2010 IP