Link to home
Start Free TrialLog in
Avatar of casit
casitFlag for United States of America

asked on

array problems

I have the following code.  I just want it to display $count.
[php]
<?php
            require_once 'classes/pg_class.php';
            extract($GLOBALS);
                  $db = new db(0);
    // check if the username exists in the database
    //$connect = $DB->PostgreDB();
    $clean = pg_escape_string($_GET['username']);
    $query = "SELECT count(username) as count FROM users WHERE username= '$clean'";
$data = $db->fetch($query);
    extract($data);
    var_dump($data);
    /*if ($count = 0) {
          $_SESSION['output'] .= "A User already exists with that username.  Please try another.<br />";
          $error = '1';
    } else {
    echo 'this is a good';      
    }*/
    if ($count == '1') {
          echo 'this is good';
    }
    echo $count;
    ?>
[/php]
It generate the following
[ouput]
array(1) { [0]=> array(1) { ["count"]=> string(1) "1" } }
[/output]
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Use
$data = $db->fetch($query);
$count = $data['count'];

-r-
Avatar of casit

ASKER

Nope same exact thing.  Nothing.
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Does this example help you out a bit?

<?php
$res = mysql_query("select * from blah") // a query that returns an empty set
$row = mysql_fetch_array($res); // get's 0 since there's no return
echo count($row); // echos 1 - since $row is not an array
echo $row[0]; // echos "", but casts $row as an array?
echo count($row); // echos 0 now
?>

With kind regards,


HoLy007
Avatar of casit

ASKER

@Roonaan
ok var_export($data); shows the following
array ( 0 => array ( 'count' => '1', ), )
Avatar of casit

ASKER

Thanks I got it.
Which solution did you use?
If you used the plain SELECT * FROM table along with $result->count / mysql_num_rows, then make sure to add a LIMIT 1 to the end of your query. You don't actually care how many rows there are returned, only if there are any.

-r-