astraeuz
02-03-2008, 01:27 PM
I'm newbie to PHP that's why my following function is not returning any result and there is no error neither :(
What is wrong with it?
function getinstalledusers()
{
$lnk = mysql_connect('localhost', 'uid', 'pwd');
mysql_select_db('thedb', $lnk);
$q = "SELECT uid FROM users WHERE installed = 1";
$result = mysql_query($q);
$ret = "";
if($result)
{
for($i = 0; $i < count($result); $i++)
{
if($ret != "")
{
$ret .= ",";
}
$ret .= $result[$i][0];
}
}
else
{
echo('query failed?\r\n');
}
mysql_close($lnk);
return $ret;
}
echo("Installed: ". getinstalledusers());
?>
thanks
Steve_Arm
02-03-2008, 01:44 PM
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
}
Codebird
02-03-2008, 01:51 PM
function getinstalledusers()
{
$lnk = mysql_connect('localhost', 'uid', 'pwd');
mysql_select_db('thedb', $lnk);
$q = "SELECT uid FROM users WHERE installed = 1";
$result = mysql_query($q);
$row=mysql_fetch_assoc($result);
$ret = "";
if($result)
{
for($i = 0; $i < count($result); $i++)
{
if($ret != "")
{
$ret .= ",";
}
$ret .= $row[$i][0];
}
}
else
{
echo('query failed?\r\n');
}
mysql_close($lnk);
return $ret;
}
echo("Installed: ". getinstalledusers());
?>
your code should be something like that
astraeuz
02-03-2008, 02:23 PM
Thanks you steve and Hicham, but instead of returning whole value of the row, it just return first character, and only for the first row. There are two records in my table, but it just returns 1st character of the first row or 'uid' column **confused**
Codelphious
02-03-2008, 02:29 PM
Actually... it should be more like this:
<?php
// somewhere in a config file, so you don't create unwanted duplicate connections to the db...
$db = mysql_connect('localhost', 'uid', 'pws');
mysql_select_db('thedb', $db);
// somewhere else...
function getinstalledusers() {
$q = "SELECT uid FROM users WHERE installed = '1'";
$result = mysql_query($q);
if (mysql_num_rows($result) == 0)
return false; // no results to return
$uids = array();
for ($i=0; $data = mysql_fetch_assoc($result); $i++) {
$uids[$i] = $data['uid'];
}
return $uids;
}
// somewhere else...
include('config.php'); // this is where you created your DB connection...
$uids = getinstalledusers();
if ($uids)
print_r($uids);
else
echo "No users.";
?>
astraeuz
02-04-2008, 01:32 AM
Thank you Codelphious. It's working like a charm :)
Codelphious
02-04-2008, 11:35 PM
You're welcome, astraeuz. I'm Happy to help.:cool: