Error trying to get data type of a mysql 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
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Error trying to get data type of a mysql value

Post by mcccy005 »

Ok I get this error: Notice: Trying to get property of non-object in XXXXX\results.php on line 85
Here's the code:

Code: Select all

//Gets the data from each row of the results from the query and parses each row as a new result_field object into a result_set object
for($i=0; $i<$result->num_rows; $i++) 
{ 
	 $row=$result->fetch_row( ); //Gets an index array of each row
	/*Traverses throuch each column of $row, and creates a new result cell for each column and adds the results
	to a temporary result_field array.
Also gets the data type from the current colum*/
	for($j=0; $j<sizeof($titles); $j++)
	 { 
		$temp_data_type = $row[$j]->type; //LINE 85
In summary, I'm getting each row of the results into a numerical array (as opposed to associative); and then I'm checking each key of the array (ie. each column of each field of the results) to see what mysql data_type it is. I've tried with and without the "[$j]" and it makes no difference to the error.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it would suggest that $result is not an object.
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

Well what is it then???
Heres more code which might help...

Code: Select all

$sql_query="Select Names.Name from Names";
$result=$db_connection->query($sql_query);
If result isnt an object per se; then how can I go about finding out the data type of each column of the returned result fields?

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I have no idea what it is as I have no idea how your query() method is written. I would guess that the query failed. var_dump() could be of use here as a debugging helper.
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

The query method is the default php method- its not something I wrote!

But I'll fiddle around with different queries and look at that var_dump thing - never used so have no idea how to use it but will have a look around at it; thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You're using the MySQLi object? If so, then I'll guess the only value that it would return for the query you supplied is false, which likely means you have an error with your query.
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

Thanks for your help feyd....however....
I've tried using that var_dump (so handy and so easy to use!!!) and the variable $row does in fact get the results of the first row of the database.
I've even modified the query slightly to get 2 variables (id and name); and that works also!!

So I'm guessing its something to do with the way I'm calling MYSQL's "type" which is defined as a "PROPERTY".
ALL that it says about this "TYPE" property is this:
type - returns MYSQLI_STORE_RESULT or MYSQLI_USE_RESULT
And then theres a massive table of constants here which is what "type" returns: http://au2.php.net/mysqli
Its not something stupid I'm missing like the way I'm defining $temp_data_type is it?? If $temp_data_type is going to accept a constant as the returned value, I don't need to define it any differently do i??

Code: Select all

for($i=0; $i<$result->num_rows; $i++) 
{ 
     $row=$result->fetch_row( );
     for($j=0; $j<sizeof($titles); $j++)
     { 
          $temp_data_type = $row[$j]->type;

     .........
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are you sure that $titles has the same number of elements $row will have?
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

Yep - just double checked that and did a var_dump on titles aswell and everything is coming through as it should....
I've double checked the value of $result->num_rows( ) and thats all good aswell!!

Guess I'll keep playing around with bits and pieces then.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

mcccy005 wrote:Yep - just double checked that and did a var_dump on titles aswell and everything is coming through as it should....
try a var_dump() on $row.
according to http://de2.php.net/manual/en/function.m ... ch-row.php mysqli_fetch_row returns an array, not an object.
Use mysqli_fetch_field instead.
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

Thanks Volka; I'm using that particular method because I wanted an array returned!
I've fiddled around with the code all afternoon and it appears that somewhere in a completely different area, I said if($i=X) instead of if($i==X) and that completely buggered EVERYTHING else up and sent the computer into some infinte loop etc etc!!
But it seems to be doing what its supposed now; even though the notice is still there!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

mcccy005 wrote:Thanks Volka; I'm using that particular method because I wanted an array returned!
If it is an array there is no -> operator defined for it. This can't work.
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

OH really???
I kinda assumed that it was $row[$j] was an object of sort which could have methods (like type) called to it!!

Ah well.....its handy to know though thanks.

I've changed all the code to sort that aswell now too (which involved changing a bunch of code in other objects but ah well....in the end most has been sorted out!)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ah yes, you're right. fetch_row could have returned an array of objects. But it's an array of strings.
e.g.

Code: Select all

$row=$result->fetch_row( );
echo "<pre>\n"; var_dump($row); echo "</pre>\n";
<pre>
array(2) {
[0]=>
string(2) "-1"
[1]=>
string(9) "Anonymous"
}
</pre>
Post Reply