Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    199
    Thanks
    30
    Thanked 0 Times in 0 Posts

    Very Weird Problem with mysql_fetch_assoc()?

    Ok, so I have absolutely no idea what is going on, but it seems that my code is refusing to parse a simple mysql_fetch_assoc() when I use a certain variable.

    First off, here's the code, i'm using php html dom to scrape a small amount of text and put it into the $info array:

    PHP Code:
    $info $html->find('span[class=value]');

    echo 
    "
    <b>Year:</b> $info[0]<br>
    <b>Make:</b> $info[1]<br>
    <b>Model:</b> $info[2]<br>
    <b>Trim:</b> $info[3]<br>
    <b>Gas Mileage:</b> $info[8]/$info[9]<br>
    <b>Combined:</b> $info[10]<br>
    "
    ;

    $catquery mysql_query("SELECT * FROM categories WHERE name = '$info[2]'");
    $getcat mysql_fetch_assoc($catquery);

    echo 
    "Category ID: $getcat[category_id]"
    Problem is, $getcat keeps coming up empty if I use the $info[2] variable, but if I put in the actual name, such as "Silverado 1500", it works fine so I know it's connecting and finding the name just fine, just comes up empty when using the $info[2] variable.

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    PHP Code:
    $catquery mysql_query("SELECT * FROM categories WHERE name = '{$info[2]}'"); 
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    199
    Thanks
    30
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by AndrewGSW View Post
    PHP Code:
    $catquery mysql_query("SELECT * FROM categories WHERE name = '{$info[2]}'"); 
    Just tried putting the brackets around $info[2] but no luck, still doesn't read :\

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    PHP Code:
    var_dump($info[2]); 
    What's that result in?

  • #5
    Regular Coder
    Join Date
    Jan 2006
    Posts
    199
    Thanks
    30
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    PHP Code:
    var_dump($info[2]); 
    What's that result in?
    Pretty long...

    PHP Code:
    object(simple_html_dom_node)#348 (9) { ["nodetype"]=> int(1) ["tag"]=> string(4) "span" ["attr"]=> array(1) { ["class"]=> string(5) "value" } ["children"]=> array(0) { } ["nodes"]=> array(1) { [0]=> object(simple_html_dom_node)#349 (9) { ["nodetype"]=> int(3) ["tag"]=> string(4) "text" ["attr"]=> array(0) { } ["children"]=> array(0) { } ["nodes"]=> array(0) { } ["parent"]=> *RECURSION* ["_"]=> array(1) { [4]=> string(14) "Silverado 1500" } ["tag_start"]=> int(0) ["dom:private"]=> object(simple_html_dom)#2 (23) { ["root"]=> object(simple_html_dom_node)#3 (9) { ["nodetype"]=> int(5) ["tag"]=> string(4) "root" ["attr"]=> array(0) { } ["children"]=> array(2) { [0]=> object(simple_html_dom_node)#4 (9) { ["nodetype"]=> int(6) ["tag"]=> string(7) "unknown" ["attr"]=> array(0) { } ["children"]=> array(0) { } ["nodes"]=> array(0) { } ["parent"]=> *RECURSION* ["_"]=> array(2) { [0]=> int(1) [4]=> string(15) "" } ["tag_start"]=> int(0) ["dom:private"]=> *RECURSION* } [1]=> object(simple_html_dom_node)#6 (9) { ["nodetype"]=> int(1) ["tag"]=> string(4) "html" ["attr"]=> array(4) { ["xmlns"]=> string(28) "http://www.w3.org/1999/xhtml" ["lang"]=> string(5) ... 
    It just keeps going

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    And that's what you are comparing to.
    Don't know what simple_html_dom_node class is, but I'd presume it has a way to fetch the value. I can see it there nested a couple of down, but you'll need to consult the documentation for the simple_html_dom_node class to see how to fetch out out of there.

  • #7
    Regular Coder
    Join Date
    Jan 2006
    Posts
    199
    Thanks
    30
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    And that's what you are comparing to.
    Don't know what simple_html_dom_node class is, but I'd presume it has a way to fetch the value. I can see it there nested a couple of down, but you'll need to consult the documentation for the simple_html_dom_node class to see how to fetch out out of there.
    Is there anyway to pull the value from $info[2] to another variable and then query the new variable?

    This way seems to work:

    PHP Code:
    $test "{$info[2]}";
    var_dump($test); 
    Well...no, it didn't, the var_dump shows the right variable, but when I plug $test into the query, still pops up nothing :\
    Last edited by Remix919; 01-12-2013 at 07:54 PM.

  • #8
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    A brief search suggests

    PHP Code:
        $info = array();
        foreach(
    $html->find('span[class=value]') as $e){
                
    $info[] = $e->plaintext;
        } 
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    Remix919 (01-12-2013)

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I don't really see the need to pull out the offset to a different variable. The problem here is you are trying to pass an object to the sql string which doesn't appear to have a way to interpret it properly. My expectation is that the echo also will not work, but PHP's toString is flaky over different versions for where it is interpreted. If it shows properly within the echo, you can force the __toString() call on it to return the string representation.
    Consult the documentation for the simple_html_dom_node to see what you need to do to extract it out of there.

  • Users who have thanked Fou-Lu for this post:

    Remix919 (01-12-2013)


  •  

    Posting Permissions

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