Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-12-2013, 06:54 PM   PM User | #1
Remix919
Regular Coder

 
Join Date: Jan 2006
Posts: 193
Thanks: 29
Thanked 0 Times in 0 Posts
Remix919 is an unknown quantity at this point
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.
Remix919 is offline   Reply With Quote
Old 01-12-2013, 07:18 PM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 01-12-2013, 07:30 PM   PM User | #3
Remix919
Regular Coder

 
Join Date: Jan 2006
Posts: 193
Thanks: 29
Thanked 0 Times in 0 Posts
Remix919 is an unknown quantity at this point
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 :\
Remix919 is offline   Reply With Quote
Old 01-12-2013, 07:40 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,639
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
PHP Code:
var_dump($info[2]); 
What's that result in?
Fou-Lu is offline   Reply With Quote
Old 01-12-2013, 07:44 PM   PM User | #5
Remix919
Regular Coder

 
Join Date: Jan 2006
Posts: 193
Thanks: 29
Thanked 0 Times in 0 Posts
Remix919 is an unknown quantity at this point
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
Remix919 is offline   Reply With Quote
Old 01-12-2013, 07:46 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,639
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 01-12-2013, 07:48 PM   PM User | #7
Remix919
Regular Coder

 
Join Date: Jan 2006
Posts: 193
Thanks: 29
Thanked 0 Times in 0 Posts
Remix919 is an unknown quantity at this point
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..
Remix919 is offline   Reply With Quote
Old 01-12-2013, 07:52 PM   PM User | #8
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
Remix919 (01-12-2013)
Old 01-12-2013, 07:55 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,639
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Remix919 (01-12-2013)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:55 PM.


Advertisement
Log in to turn off these ads.