mysql_fetch_array returns wrong rows count - php

I have this code:
public function getRecurringEventsByGrouped($grouped){
$query = "SELECT * FROM `event` AS e
WHERE e.`grouped` = " . $grouped . " ORDER BY EventId DESC";
$result = mysql_query($query);
while ($ids[] = mysql_fetch_array($result, MYSQL_NUM)) ;
return $ids;
}
mysql_fetch_array() doesn't return the first row. mysql_num_rows() returns correct row count.
I also tried this query in HeidiSQL and it gave the same rows number as mysql_num_rows().

you need to iterate through the data in while loop.
$ids = '';
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
//check for proper indexing of table rows and specify $row[value] accordingly.
$ids[] = $row[0];
}
return $ids;
this should fetch you the correct content.

Related

why COUNT function gives wrong input inside php and right result inside phpmyadmin sql

I'm trying to count the rows which are not NULL inside a table when exucting the query inside Phpmyadmin it gives me the right output.
SELECT COUNT(`column_name`) FROM `Table_name`
but when I'm trying to execute it inside Php it always returns one I tried 2 methods both returning one for some reasons any ideas ?
method 1
$query = "SELECT COUNT(`column_name`) FROM `Table_name`";
if ($result = $mysqli->query($query)) {
$field1name = $rowcount=mysqli_num_rows($result);
echo '<tr>
<td>English</td>
<td>'.$field1name.'</td>
</tr>';
$result->free();
}
method 2
$query = "SELECT COUNT(`column_name`) FROM `Table_name`";
if ($result = $mysqli->query($query)) {
while ($rowcount = $result->fetch_assoc()) {
$field1name = $rowcount=mysqli_num_rows($result);
echo '<tr>
<td>Bahdini</td>
<td>'.$field1name.'</td>
</tr>';
}
$result->free();
}
The SELECT COUNT Query returns a resultset of 1 row, in that row you get the number of rows: 10228 as you stated.
the function mysqli_num_rows returns the number of rows in the RESULTSET, that's why it returns 1.
You have several assigment in a row .. which is your expected result ??
$field1name = $rowcount =mysqli_num_rows($result);
Instaed You should use a proper column alias for your count and then query, fecth, loop over the result and show
$query = "SELECT COUNT(`column_name`) my_count FROM `Table_name`";
$result = mysqli_query($conn, $query );
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row["my_count"]. "<br>";
}
} else {
echo "0 results";
}

Fetch multiple records from php/mysql

I am fetching records as follows:
$aResult = array();
$sql = "SELECT notes FROM table WHERE user_id = '$user_id'";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$aResult['query_result'] = $row;
}
This returns only the last record in the table. I need to return all records from the sql statement.
change you $aResult['query_result'] = $row; to $aResult['query_result'][] = $row;
You've override the result each time, so you just get one.
It seems your loop constantly overwrites the value and hence you will only ever seen the last row. I think you might see better results if you do something like:
while($row = mysqli_fetch_array($result))
{
$aResult[] = $row;
}
so that each new row gets appended to your array
Try with following code, currently You are initiating the values to the same array key :
$aResult = array();
$sql = "SELECT notes FROM table WHERE user_id = '$user_id'";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$aResult['query_result'][] = $row;
}
for more Detail On Array

PHP function is not returning all the data in MySQL table

I have a function
function get_table_content(){
$sql = "SELECT * FROM my_table WHERE id > 0";
$result = $this->query($sql);
while ($row = $this->fetch($result)){
return ($row);
}
}
The problem is, when i call this function it only return the first entry in my database.
How can i make this work it suppose to return about all the data in the table.
I have also tried this and it is not working too
function get_table_content(){
global $database_connection;
$sql = "SELECT * FROM my_table WHERE id > 0";
$result = mysqli_query($database_connection, $sql);
while ($row = mysqli_fetch_assoc($result)){
return ($row);
}
}
I think the best way to go about this is to return an array and do my foreach operation on it but i can not get it to work.
Can someone help me out Please.
P.S i will preffer it to return an array of all the data in the data so i can do a foreach operation on it.
Thank you
return immediately stops execution of your function during the first iteration of the loop. To get all records and return them:
$result = mysqli_query($database_connection, $sql);
return mysqli_fetch_all($result);
Side note: This is less efficient then returning a result set resource and looping through it as needed outside your function. fetch_all must convert all results to array, then presumably you're looping through this array later.
The problem is, in the while loop after fetching one row from the result set you're returning the result from the function, and hence you're getting only the first row from the result set. Either use mysqli_fetch_all() function or an array in the while loop to store the result.
Method(1):
function get_table_content(){
global $database_connection;
$sql = "SELECT * FROM my_table WHERE id > 0";
$result = mysqli_query($database_connection, $sql);
return mysqli_fetch_all($result);
}
Method(2):
function get_table_content(){
global $database_connection;
$sql = "SELECT * FROM my_table WHERE id > 0";
$result = mysqli_query($database_connection, $sql);
$r = array();
while ($row = mysqli_fetch_assoc($result)){
$r[] = $row;
}
return $r;
}
You have a return inside of your while loop, which is terminating that loop and returning the first row of data.

display all cells in a mysql column

I need all values from a table column put into an assoc array. I am having trouble finding the answer.
the table is only six rows deep.
example:
|--id--|--name--|--A--|--B--|--C--|
|--01--|--xl33--| 1.30| 2.45| 4.40|
i would like to get an assoc array for column B
name[xl33]=2.45
name[xl34]=....and so on
The trick is that the form will tell the script which column to fetch. A,B,C,D,E,F OR G
I know i could re-format the table and accomplish what i want but I need it structured the way i have it.( i have left out some columns for simplicity)
function return_column($letter){
$result = mysql_query("SELECT name, $letter FROM example") or die(mysql_error());
while($row = mysql_fetch_assoc( $result )) {
$return[$row['name']] = $row[$letter];
}
return $return;
}
$results = return_column('A');
print_r($results);
Something like :
$col = 'B';
$name = array();
$result = mysql_query("SELECT * FROM table") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$name[$row['name']] = $row[$col];
}
This creates an array $name and uses the name column as the key and the $col column for the value ...
You are looking for the mysql_fetch_assoc() function.
Example/
$query = $this->query($YOUR_QUERY);
$returnMap = array();
while ($row = mysql_fetch_assoc($query)) { array_push($returnMap, $row); }
Use mysql_fetch_assoc — Fetch a result row as an associative array.
while ($row = mysql_fetch_assoc($result)) {
$data[$row["column1"]]= $row["column2"];
..............
.........
}

Num Rows always ='s 1 for some reason. QUICK FIX

I don't know if i am doing it right, this is what I got:
while($row = mysqli_fetch_assoc($result)) {
$items = mysqli_num_rows($row);
}
It always sets $items = to 1 for some reason.
Here is my mysqli_query...
$top10_query = "SELECT * FROM users WHERE userid='$userid'";
$result = mysqli_query($cxn, $top10_query) or die("Couldn't execute query.");
$row = mysqli_fetch_assoc($result);
Well, $row only contains one row so....
$items = mysqli_num_rows($result)
should give you the correct number of items
Anyway, why are you doing that in a loop? The number of rows is constant...

Categories