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 03-04-2010, 02:11 PM   PM User | #1
mike182uk
Regular Coder

 
Join Date: May 2008
Posts: 135
Thanks: 13
Thanked 10 Times in 10 Posts
mike182uk is an unknown quantity at this point
mysqli problem

is there any reason why this wouldnt work? because i cant get it to work?

PHP Code:
function getAttrVals($sql){
    
$result $db_myDB->query($sql);
    while (
$row $result->fetch_object() ){
        
$html .= "<option value='"$row->id ."'>"$row->name ."</option>";
    }
    
$result->close();
    return 
$html;

Code:
<select>
<?php echo getAttrVals("SELECT * FROM products"); ?>
</select>

<select>
<?php echo getAttrVals("SELECT * FROM products2"); ?>
</select>
there is a db table called products and products 2, and there are columns call id and name.

i init the connection like so

PHP Code:
$db["myDB"]["host"] = "localhost";
$db["myDB"]["user"] = "root";
$db["myDB"]["pass"] = "xxxxxxxx";
$db["myDB"]["db"] = "myDB";
$db_myDB = new mysqli($db["myDB"]["host"],$db["myDB"]["user"],$db["myDB"]["pass"],$db["myDB"]["db"]); 
for some reason it does not return anything when i run the page.
mike182uk is offline   Reply With Quote
Old 03-04-2010, 05:49 PM   PM User | #2
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
You should be checking for any errors, making sure they are displaying and not being visibly obscured inside HTML tags (ie: view the output as text)... but I would venture a guess that $db_myDB is outside the function's scope:
PHP Code:
function getAttrVals($sql){
    global 
$db_myDB;
    
$html '';
    
$result $db_myDB->query($sql);
    while (
$row $result->fetch_object() ){
        
$html .= "<option value='"$row->id ."'>"$row->name ."</option>";
    }
    
$result->close();
    return 
$html;

__________________
ZCE
kbluhm is offline   Reply With Quote
Old 03-05-2010, 09:19 AM   PM User | #3
mike182uk
Regular Coder

 
Join Date: May 2008
Posts: 135
Thanks: 13
Thanked 10 Times in 10 Posts
mike182uk is an unknown quantity at this point
oh man of course!

that gets me everytime.

im so use to asp, i forget about scope.

so i pass the myslqi object to the function it works.

is there any way of the function being able to access variables on the page, that are not session variables or declared in the scope of the function?
mike182uk is offline   Reply With Quote
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 10:11 PM.


Advertisement
Log in to turn off these ads.