We help IT Professionals succeed at work.

array problems

casit
casit asked
on
254 Views
Last Modified: 2013-11-05
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]
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2006

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

-r-

Author

Commented:
Nope same exact thing.  Nothing.
CERTIFIED EXPERT
Top Expert 2006
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
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

Author

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

Author

Commented:
Thanks I got it.
CERTIFIED EXPERT
Top Expert 2006

Commented:
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-

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.