CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Very Weird Problem with mysql_fetch_assoc()? (http://www.codingforums.com/showthread.php?t=285685)

Remix919 01-12-2013 06:54 PM

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.

AndrewGSW 01-12-2013 07:18 PM

PHP Code:

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


Remix919 01-12-2013 07:30 PM

Quote:

Originally Posted by AndrewGSW (Post 1305783)
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 :\

Fou-Lu 01-12-2013 07:40 PM

PHP Code:

var_dump($info[2]); 

What's that result in?

Remix919 01-12-2013 07:44 PM

Quote:

Originally Posted by Fou-Lu (Post 1305788)
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

Fou-Lu 01-12-2013 07:46 PM

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.

Remix919 01-12-2013 07:48 PM

Quote:

Originally Posted by Fou-Lu (Post 1305791)
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 :\

AndrewGSW 01-12-2013 07:52 PM

A brief search suggests

PHP Code:

    $info = array();
    foreach(
$html->find('span[class=value]') as $e){
            
$info[] = $e->plaintext;
    } 


Fou-Lu 01-12-2013 07:55 PM

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.


All times are GMT +1. The time now is 10:58 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.