which is the fetch function to get an array of value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PHPHelpNeeded
Forum Commoner
Posts: 83
Joined: Mon Nov 17, 2014 8:03 pm

which is the fetch function to get an array of value

Post by PHPHelpNeeded »

Which is the correct PDOStatement:fetch() function to get an array of values.

Can someone provide an example. Please.

I have tried for about 3 hours trying to fetch an array of values and NOTHING happens.

for instance, I do get the array, but NO values in it, wondering what am I doing wrong?

Code: Select all

		$query = $this->db_query("SHOW FULL COLUMNS FROM ".$table." FROM ".$database);
		$query_array = $query->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_GROUP, PDO::FETCH_COLUMN);
		$query->closeCursor();
		echo "query_fetch_results: ";
		print_r($query_array);
I tried putting quotes between $table and $database and that does not work.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: which is the fetch function to get an array of value

Post by Celauran »

What's wrong with plain old fetchAll() without arguments?
PHPHelpNeeded
Forum Commoner
Posts: 83
Joined: Mon Nov 17, 2014 8:03 pm

Re: which is the fetch function to get an array of value

Post by PHPHelpNeeded »

I have another problem...

Thank you for the tip, but now I want to know why is the data being truncated the further you look into the array in the form:

Code: Select all

                 $query = $this->db_query("SELECT * FROM "$database.".".$table);
                 $query_array = $query->fetchAll();
                 $query->closeCursor();

		foreach($query_fetch as $ka => $ta){
			foreach($ta as $kb => $tb){
				if($kb === $column){
					echo "kb==".$kb." => tb==".$tb[$kb]."\n";						
				}
			}
		}
Why is $tb[$kb] is returning 1 character of the full data? For instance, I writing a function that will return the value of a column key indirectly by finding the value of another column key.

For instance, in a user table, you know there is only one user in that table, which stores the password and other information, so there shouldn't be any more then one username in that table.

The function I am writing finds the key that matches the $column variable which is the column key you are using to indirectly find the value of another key lets say, the UserID.

I mean the userID, you cannot find it directly unless you match your data indirectly.

But the problem with the code above, is not in the form of how it is finding the keys versus the values to match. The problem is on the fact that in the second of the foreach loop, the array values are all being truncated and you cannot finally match the value that you are using like the name of a particular user, because the value is truncated, else this form of getting the index userid of that particular user is not possible because of that problem...I never seen that type of problem of the values being truncated, any help please.

PHP is weird and dangerous, it loses your data.

Thanks.
Post Reply