1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Pulling records from mySQL...

Discussion in 'PHP' started by SEbasic, Sep 23, 2005.

  1. SEbasic

    SEbasic Peon

    Messages:
    6,317
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #21
    I will need help again soon... I'm playing at the moment, but it could get messy...

    I'll let you know tomorrow ;) :D
     
    SEbasic, Sep 26, 2005 IP
  2. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #22
    Hard coding normally does, give me a yell if you need hand holding (off to get a beer now).
     
    dct, Sep 26, 2005 IP
  3. l234244

    l234244 Peon

    Messages:
    1,225
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    0
    #23
    I think Oliver should buy you that beer, lol
     
    l234244, Sep 26, 2005 IP
  4. SEbasic

    SEbasic Peon

    Messages:
    6,317
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #24
    OK I need some help again

    This is what I have so far...
    
     $mysql_access = mysql_connect("localhost", "user", "pass");
     mysql_select_db( "DBname", $link ) ;
     $sql = "SELECT `asin`, `name` FROM `freebs_items` ORDER BY `name` DESC";
     $result = mysql_query($sql);
     $num = mysql_num_rows($result);
     $i=0;
     while ($i < $num) {
     $book_name = mysql_result($result,$i,"name");
     $book_asin = mysql_result($result,$i,"asin");
     echo ("<a href=\"$book_asin\">$book_name</a><br />");
     $i++;
      }
    PHP:
    This is on the same page as the previous one - just adding something to the bottom...

    The problem is, I'm pulling the data from a different DB, so I guess I need to connect to it first (Hence the top couple of lines)...

    This isn't working though

    Can anyone see anything clearly wrong with this?
     
    SEbasic, Oct 17, 2005 IP
  5. SEbasic

    SEbasic Peon

    Messages:
    6,317
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #25
    I tried messing with it some more but that messed up the whole page... :(

    I'm really f***ed here. :(
     
    SEbasic, Oct 17, 2005 IP
  6. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #26
    What error messages are you getting. Use
    echo mysql_error();
    PHP:
    After possible problem lines.
     
    dct, Oct 17, 2005 IP
  7. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #27
    You don't need to connect again. As long as the database user that you login in with has permissions for both databases, you can specify a database and table name in a query, e.g.
    $sql = "SELECT `asin`, `name` FROM `DBname.freebs_items` ORDER BY `name` DESC";
    PHP:
    .

    Also, dct is right, print out the MySQL error and the query if you detect a failure
     
    johnt, Oct 17, 2005 IP
  8. SEbasic

    SEbasic Peon

    Messages:
    6,317
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #28
    I get this - I've also changed to what you suggested john...
    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/stretch/public_html/oweb/wp-content/plugins/phpinposts.php(40) : eval()'d code on line 4

    That make any sense?
     
    SEbasic, Oct 17, 2005 IP
  9. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #29
    Does the table freebs_items contain any records?
     
    dct, Oct 17, 2005 IP
  10. SEbasic

    SEbasic Peon

    Messages:
    6,317
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #30
    Yeah, about 1,500 of them... :|
     
    SEbasic, Oct 17, 2005 IP
  11. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #31
    Stupid question (maybe) but your database is called DBName and you've changed the login details as it's a public forum to be ("localhost", "user", "pass");
     
    dct, Oct 17, 2005 IP
  12. SEbasic

    SEbasic Peon

    Messages:
    6,317
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #32
    Yep - this is what I have now... (DB name has been changed)...
    $sql = "SELECT `asin`, `name` FROM `DBName.freebs_items` ORDER BY `name` DESC"; 
     $result = mysql_query($sql);
     $num = mysql_num_rows($result);
     $i=0;
     while ($i < $num) {
     $book_name = mysql_result($result,$i,"name");
     $book_asin = mysql_result($result,$i,"asin");
     echo ("<li><a href=\"$book_asin\">$book_name</a></li>");
     $i++;
     echo mysql_error(); 
      }
    PHP:
    That's the lot...
     
    SEbasic, Oct 17, 2005 IP
  13. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #33
    you want to try:

    $num = mysql_affected_rows();

    instead of:

    $num = mysql_num_rows($result);
     
    daboss, Oct 17, 2005 IP
  14. SEbasic

    SEbasic Peon

    Messages:
    6,317
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #34
    Hmmm.. Well it certianly did *something*

    Now I get no errors, but nothing is being output either...
     
    SEbasic, Oct 17, 2005 IP
  15. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #35
    you mind doing a:

    print "number of records found = " . $num;

    after the:

    $num = mysql_affected_rows();

    that way, we'll get to see how many records get returned... if it's 0, then the problem is with your query...
     
    daboss, Oct 17, 2005 IP
  16. SEbasic

    SEbasic Peon

    Messages:
    6,317
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #36
    Hmmm, I must have done something *really* wrong then...

    I get "number of records found = -1"
     
    SEbasic, Oct 17, 2005 IP
  17. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #37
    well, the return values for mysql_affected_rows are:

    "Returns the number of affected rows on success, and -1 if the last query failed."

    looks like you've got a syntax error with your query...
     
    daboss, Oct 17, 2005 IP
  18. SEbasic

    SEbasic Peon

    Messages:
    6,317
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #38
    In the mySQL then?

    Hmmmmmm.

    So In the table 'freebs_items', I have two columns I would like to pull data from - 'asin' and 'name'

    I've been using
    $sql = "SELECT `asin`, `name` FROM `DBname.freebs_items` ORDER BY `name` DESC";

    Can that *really* be wrong?

    I guess it must be, but it seems pretty straightforward...
     
    SEbasic, Oct 17, 2005 IP
  19. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #39
    i'd go back to your original syntax:

    $mysql_access = mysql_connect("localhost", "user", "pass") or die("Cannot connect to DB!");
    mysql_select_db( "DBname") or die("Cannot select DB!");

    $sql = "SELECT asin, name FROM freebs_items ORDER BY name DESC";
    $result = mysql_query($sql);
     
    daboss, Oct 17, 2005 IP
    dct likes this.
  20. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #40
    Put the echo sql_error underneath every database statement you have in the file, especially including the connection ones.

    Edit: or like daboss says use die :)
     
    dct, Oct 17, 2005 IP