Current location: Hot Scripts Forums » Programming Languages » PHP » Getting a function to return a MySQL table row..


Getting a function to return a MySQL table row..

Reply
  #1 (permalink)  
Old 12-15-03, 12:41 AM
Axe Axe is offline
New Member
 
Join Date: Dec 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Getting a function to return a MySQL table row..

Okies, so here's the basic idea..

PHP Code:

function myfunc() {

  
$sql 'select * from table where field1="1"';
  
$result mysql_query($sql);
  
$row mysql_fetch_assoc($result);
  return 
$row;
}

$somevar myfunc(); 
Now, you can guess what I want to do... I want the results of that row stored in $somevar, so that I can call $somevar['fieldname1'], $somevar['fieldname2'], etc..

But it's telling me...

Code:
Fatal error: Call to undefined function: array() in /home/blah/blah/blah/blah/whatever.php on line 9
Line 9 being $somevar = myfunc();

I've not tried doing this before, so I'm not sure quite how to go about it, or exactly what to hit google with to find the results I desire.

Does anybody know how to write this so I can have a function pull a row from a MySQL table, then have that function pass it back to a $variable, so that I can just call the values by $variable['whatever'] ?

Thanks.
__________________
John
http://www.reptilerooms.com
Reply With Quote
  #2 (permalink)  
Old 12-15-03, 01:02 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

I've found this:

http://www.phpbuilder.com/lists/php-...99911/0889.php

Also when you get this fatal error, the first thing you want to do is to check if you've mistyped the function's name upon calling/defining.

Hope this helps.
__________________
Blavv =|
Reply With Quote
  #3 (permalink)  
Old 12-15-03, 01:11 AM
Axe Axe is offline
New Member
 
Join Date: Dec 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks, this is running on a Linux box.

Apache 1.3.28/PHP 4.3.3
__________________
John
http://www.reptilerooms.com
Reply With Quote
  #4 (permalink)  
Old 12-15-03, 01:43 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
are you naming your function array ?
you can't override a reserved function name that the core php use .. try renaming it to another name like array_ for example ..
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #5 (permalink)  
Old 12-15-03, 01:51 AM
Axe Axe is offline
New Member
 
Join Date: Dec 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Never mind, I'm an idiot

The example there isn't EXACTLY like my live-script, and the problem isn't with the function. The function works flawlessly, the problem was with the way my script calls it...

My real function has a parameter that is passed to the MySQL statement, no problem. I was incorrectly passing the session variable to the function so that's why it was messing up...

I was calling $_SESSION('whatever') instead of $_SESSION['whatever']

Fixed that and all is working
__________________
John
http://www.reptilerooms.com
Reply With Quote
  #6 (permalink)  
Old 12-15-03, 06:58 AM
jv2222 jv2222 is offline
The Freshmaker
 
Join Date: Jun 2003
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

It may help you to tie up a number of sql functions into a class.. kinda like ezSQL.

Cheers,
Justin
__________________
Author of <a href=http://www.hotscripts.com/Detailed/18290.html target=_blank>ezSQL</a> (makes life soooo easy when working with databases)
Reply With Quote
  #7 (permalink)  
Old 09-07-04, 06:46 AM
axelmangr axelmangr is offline
New Member
 
Join Date: Aug 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Axe
Okies, so here's the basic idea..

PHP Code:

function myfunc() {

  
$sql 'select * from table where field1="1"';
  
$result mysql_query($sql);
  
$row mysql_fetch_assoc($result);
  return 
$row;
}

$somevar myfunc(); 
Now, you can guess what I want to do... I want the results of that row stored in $somevar, so that I can call $somevar['fieldname1'], $somevar['fieldname2'], etc..

But it's telling me...

Code:
Fatal error: Call to undefined function: array() in /home/blah/blah/blah/blah/whatever.php on line 9
Line 9 being $somevar = myfunc();

I've not tried doing this before, so I'm not sure quite how to go about it, or exactly what to hit google with to find the results I desire.

Does anybody know how to write this so I can have a function pull a row from a MySQL table, then have that function pass it back to a $variable, so that I can just call the values by $variable['whatever'] ?

Thanks.

Hi!

I am trying to do something similar to what you where trying to do. I wouldlike to create a procedure that takes as input a table name and returns the data contained on this table. It seems that I cannot do that. I've tried almost everything.... I could use some help from how you solved your problem!
I would appreciate any help you could provide me with

thanx a lot
axelmangr
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
changing a specific dateformat in tigra bitesize JavaScript 0 10-30-03 08:58 AM
Autonum in mysql table pk_jsp PHP 1 10-11-03 04:32 PM
More frickin' table help H-a-V-o-C_88 HTML/XHTML/XML 1 09-14-03 07:04 PM
Help trim code down TheLaughingBandit JavaScript 0 09-02-03 09:50 AM
edit php in Squirrelmail jrcortrightiii PHP 1 07-10-03 02:08 PM


All times are GMT -5. The time now is 07:33 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.