...

View Full Version : mysqli problem



mike182uk
03-04-2010, 02:11 PM
is there any reason why this wouldnt work? because i cant get it to work?



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;
}



<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



$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.

kbluhm
03-04-2010, 05:49 PM
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:


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;
}

mike182uk
03-05-2010, 09:19 AM
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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum