casit
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['us ername']);
$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]
[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['us
$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]
ASKER
Nope same exact thing. Nothing.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
<?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
ASKER
@Roonaan
ok var_export($data); shows the following
array ( 0 => array ( 'count' => '1', ), )
ok var_export($data); shows the following
array ( 0 => array ( 'count' => '1', ), )
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-
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-
$data = $db->fetch($query);
$count = $data['count'];
-r-