php count function not showing the right count on mysql_fetch_assoc - php

I have got data from mysql_fetch_assoc stored in to an array using this command
if(mysql_num_rows($data) > 1){
while($row = mysql_fetch_assoc($data)){
$ndata[] = $row;
}
} else {
$ndata = mysql_fetch_assoc($data);
}
Now when I use count on $ndata, it retuns 1; although it is empty.
When I run mysql_num_rows on the returned data it retuns 0 rows. But when I convert the data to $ndata and then run count on that it returns 1. I want it to return the number of rows.
Thanks
Can someone please explain why is there a problem and how to fix it?

Your logic is wrong: you don't test for the case that mysql_num_rows($data) == 0. If this is the case, your code executes the same path as when the number of rows is 1 ($ndata = mysql_fetch_assoc($data);). But there are no more rows to return (there are no rows at all), so mysql_fetch_assoc returns false. And count(false) returns 1, because that's how count works.
Do it this way:
$rows = mysql_num_rows($data);
if($rows == 0) {
return null;
}
else if($rows == 1){
$ndata = mysql_fetch_assoc($data);
} else {
while($row = mysql_fetch_assoc($data)){
$ndata[] = $row;
}
}
You can either return null or array() in the first if branch; these are the only two values for which count returns 0.

I think mysql_fetch_assoc() is returning false (see doc) and count(false) returns 1.

$data = mysql_query($query);
while($row = mysql_fetch_assoc($data)){
$ndata[] = $row;
}
if(count($ndata) == 1) $ndata = $ndata[0];
Runing query that returns 1 row and
$data = mysql_query("SELECT * FROM online LIMIT 1");
print_r($ndata);
Result
Array ( [timestamp] => 1301826108 [ip] => 213.186.122.2)
Runing query with multiple rows...
$data = mysql_query("SELECT * FROM online");
print_r($ndata);
Result
Array
(
[0] => Array
(
[timestamp] => 1301848228
[ip] => 67.195.112.29
)
[1] => Array
(
[timestamp] => 1301826108
[ip] => 213.186.122.2
)
[2] => Array
(
[timestamp] => 1301825465
[ip] => 77.88.28.246
)
[3] => Array
(
[timestamp] => 1301763579
[ip] => 69.171.224.251
)
)

It seems I finally got what you need (However, question remains extremely vague and it's hard to tell for sure). But anyway.
$ndata = array();
while($row = mysql_fetch_assoc($data)){
$ndata[] = $row;
}
Now you would get desired result from count($ndata). It will return 0 for no rows, 1 for 1 row and whatever number if more rows returned. Bingo!
But if you're still planning to get different kinds of arrays, I should warn you against doing it this way.
Always state explicitly what kind of result you want to get:
$ndata = array();
if ($type=="row"){
$ndata = mysql_fetch_assoc($data);
} elseif ($type=="array"){
while($row = mysql_fetch_assoc($data)){
$ndata[] = $row;
}
}

Related

Problems with array error

Hello stackoverflow people, can you help me with this error,
Warning: array_combine() expects parameter 2 to be array, null given
in
$check = mysql_query("SELECT user_task_types, user_task_types_id FROM dotp_user_task_type WHERE user_id = '$user_id'");
$row[] = mysql_fetch_array($check);
$check = array_combine($row[0], $row[1]);
Cant understand what is wrong with it. Ive tried doing [] this so it would be array but still nothng. My array looks like this:
Array
(
[0] => TVS darbai
[user_task_types] => TVS darbai
[1] => 14
[user_task_types_id] => 14
)
I want to connect user task type id with user task types it would look like [14] => TVS darbai
$row[] = appends to an array. Your array now looks like:
array(
0 => array(...)
)
As you see, there's no [1]. You probably just want $row = mysql_fetch_array(..), then $row[0] corresponds to the first column and row[1] to the second. It would still not make sense to array_combine those two columns though.
You probably want this:
$result = mysql_query(...);
$check = array();
while ($row = mysql_fetch_array($result)) {
$check[$row[0]] = $row[1];
}
You no need to combine. $row gives your output
$check = mysql_query("SELECT user_task_types, user_task_types_id FROM dotp_user_task_type WHERE user_id = '$user_id'");
while ($row = mysql_fetch_array($check) )
{
echo $row;
}

PHP looping through array values in a query

I'm attempting to loop through an array and use the rows(values) as the conditions for my SQL query but I can't seem to get it loop through the rows. It only outputs the data for the first row where the form_name = ' V243823' then stops. i need all the rows, so 3 arrays total to be returned.
Campus Forms Array
[0] => Array
(
[PQ_Lookup] => V243823
[RL_Lookup] => B3823RL
[MA_Lookup] => F356823
)
Query
foreach( $campus_forms[0] as $key => $row )
{
$this->db->select('form_deadline,form_url,form_fullname');
$this->db->from('form_deadlines');
$this->db->where('form_name', $row);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$campus_forms = $query->result_array();
return $campus_forms;
}
}
In first loop you are returning for this reason it doesn't execute next loop.Please return your data after foreach loop.You can hold your data in an array in every loop.You can do it in this way:
$form_data_array = array();
foreach( $campus_forms[0] as $key => $row )
{
$this->db->select('form_deadline,form_url,form_fullname');
$this->db->from('form_deadlines');
$this->db->where('form_name', $row);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$campus_forms = $query->result_array();
// return $campus_forms;
$form_data_array[] = $campus_forms;
}
}
return $form_data_array;

delimit PHP/PDO execute result on multiple unrelated tables

Have a PHP factory with a sub class that queries either one or more unrelated tables in a database. The class takes a table name as a parameter and returns an array object.
When more than one table is required, I would like a way to delimit each tables result set in to its own array. The class would then return a multi-dim array.
I would prefer to not instantiate another instance of the factory. Here is the current query/result code block. I've left out all the other non-essential code for brevity
// if array has more than one table to query,
// run queries on each table
$count = count($tname);
if ($count>1) {
foreach ($tname as $value) { //each table name in array
/* $query = "SELECT s.* FROM $value s"; tried with table alias */
$query = "SELECT * FROM $value";
if ($stmt = self::$_conn->prepare($query)) {
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result[] = $row;
}
} else {
return false;
}
}
return $result;
// else if only one table to query
} else {
$string = $tname; //table name
$query = "SELECT * FROM $string";
if ($stmt = self::$_conn->prepare($query)) {
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result[] = $row;
}
return $result;
} else {
return false;
}
}
With more than one table, would return something like:
Array
(
[0] => Array
(
[team_id] => 3
[team_name] => Maverics
)
[1] => Array
(
[team_id] => 4
[team_name] => Stallions
)
[3] => Array
(
[fld_id] => 1
[fld_name] => 6v6-1
)
[4] => Array
(
[fld_id] => 2
[fld_name] => 8v8-2
)
)
Where 0,1 are from one table and 3,4 are from another.
Thank you in advance
Figured it out... Simple actually, just didn't see it before I asked the questions. I changed:
$result[] = $row;
to
$result[$value][] = $row; //line 11 in code block example
This now returns the array with each table name as the array name for the result set.
Thanks anyway,

php getting values out of an array with same 'id' number

I'm having trouble getting the value out of an array
Array
(
[0] => id
[column_name] => id
)
Array
(
[0] => businessType
[column_name] => businessType
)
Array
(
[0] => name
[column_name] => name
)
Array
(
[0] => city
[column_name] => city
)
...
I'm getting those values from
mysql_query("
SELECT column_name
FROM information_schema.columns
WHERE table_name='businessUpgrade'
")
And in my while loop
while($row = mysql_fetch_array($result)){
while($column = mysql_fetch_array($colName)){
if($row[$i] == 1){
//here comes the missing part
}
$i++;
}
}
I tried diffrent things, but according to print_r, the values I need to get have the same number of id (0). Is there any way, I can get the values out of this array.
I found that I should do it with foreach, but somehow everything I try it fails.
There is no need to use 2 while loops.
The $row array is an associative array, so you can use...
$columns = array();
// {query}
while($row = mysql_fetch_array($result)) {
$columns[] = $row['column_name'];
}
var_dump($columns);
Your $columns array now contains an index of all the columns. It's a zero-index, so these can be pulled individually using:
echo $columns[0]; // The first column from the query "id"
echo $columns[2]; // The third column from the query "name"
You can also loop this array as needed.
foreach ($columns as $id => $column) {
if ($id == 0) {
// Do something with the first column:
echo $column;
} elseif ($id == 2) {
// Do something with the third column:
echo $column;
}
}
try this
$res = mysql_query("
SELECT column_name
FROM information_schema.columns
WHERE table_name='businessUpgrade'
");
while($r = mysql_fetch_row($res)){
$arr[]=$r;
}
NOTE . do not use mysql. Try mysqli or PDO instead.
And with this:
while($row = mysql_fetch_array($result)){
while($column = mysql_fetch_assoc($colName)){
if(($column['0'] == $row[$i]) && ($row[$i] == 1)){
//something like this???
}
}
$i++;
}
Your question is a bit messy XD.....i hope this helps anyway.

Return MySQL rows in array

I've been working on a OOP method that is supposed to return the rows of a MySQL query. I have to have the data array in the format:
$rows = array('row'=> rownum, 'fld1'=> fldval .... 'fldn' => fldval);
What I have encountered are the two problems of either:
returns
$rows = array('0'=>fldval, 'fld1'=> fldval .... 'n'=> fldval, 'fldn' => fldval);
or single row of
$rows = array('fld1'=> fldval .... 'fldn' => fldval);
Little frustrated as every PHP mysql function I have tried has some sort to goofy crap flaw and will not do a straight out process.
I assume there is a good example somewhere, that can get me past the crap limitations, but haven't found anything useful yet!
I've tried all of the following:
$row = mysql_result($db_res,$n);
$row = mysql_fetch_array($db_res);
$row = mysql_fetch_assoc($db_res);
$row = mysql_fetch_object($db_res);
$row = mysql_fetch_row($db_res);
None have worked successfully! For getting out the bogus "numeric" array entries. I wrote:
foreach ($row as $k => $v)
if (is_numeric($k)) { continue; }
$result[$k] = $v;
} // end foreach $row
$row = array_push($row, 'row'=>$rownum, $result);
Hoping someone has a link.
$list = array();
$query = "SELECT value FROM table";
$resource = mysql_query($query);
while($row = mysql_fetch_assoc($resource))
{
$list['fld' . (1 + count($list))] = $row['value'];
}
$list = array('row' => count($list)) + $list;
if table have 3 row, the code above is going to give you a array like:
array(
'row' => 3,
'fld1' => 12,
'fld2' => 34,
'fld3' => 56
);

Categories