Onloac Posted August 5, 2010 Share Posted August 5, 2010 I have a database that holds a list of companies. the database includes and ID, and name of each company. Now when an articles gets submitted to my site it can then be related to company. We a relate a company to an article its related through its ID. What I want to do is create a function that would let me easly convert my company ID into company name within having to write out the queries over and over again in my code. Only problem is I can't get it to work. I have two files, index.php and functions.php. functions.php function cname($pubid) { global $db; $company= $db->query("SELECT * FROM my_database WHERE id='$pubid'") or die(mysql_error()); while($company= $db->fetch_array($company)) { $cname = $company['name']; } return $cname; } and within index.php I put echo cname($companyID); Now, when I first looked at index.php I was getting an error. I know it had something to do with me using my variables within the function and after some searching I ended up getting rid of the error by adding "global $db;" to my code. Please note that I include functions.php at the top of index.php. What am I doing wrong and why wont it work? HELP PLEASE! also note that my i've rechecked and all the database info is correct and names spelt right. Just dont know what else to do. Quote Link to comment Share on other sites More sharing options...
dolrichfortich Posted August 5, 2010 Share Posted August 5, 2010 What was the error you are getting? Quote Link to comment Share on other sites More sharing options...
Onloac Posted August 5, 2010 Author Share Posted August 5, 2010 the error I was getting was: Fatal error: Call to a member function query() on a non-object in /my/path/functions.php on line 4 line 4 was: $company= $db->query("SELECT * FROM my_database WHERE id='$pubid'") or die(mysql_error()); but once I added "Global $DB;" it got ride of that error. But its still not returning a result If I get rid of the database stuff and just put a simple echo of the ID it'll work, its just I can't seem to get it to pull the company name. Quote Link to comment Share on other sites More sharing options...
dolrichfortich Posted August 5, 2010 Share Posted August 5, 2010 Try adding this to check if the database class is really returning any data. function cname($pubid) { global $db; $company= $db->query("SELECT * FROM my_database WHERE id='$pubid'") or die(mysql_error()); print_r($db->fetch_array($company)); while($company= $db->fetch_array($company)) { $cname = $company['name']; } return $cname; } Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted August 5, 2010 Share Posted August 5, 2010 Firstly, pass variables into functions, refrain from using global variables. Secondly, if there is only 1 company name to return you do not need to loop over a result set. Example: <?php function cname($db, $companyId) { $result = $db->query("SELECT name FROM compaines WHERE id='".$companyId."'"); $company = $db->fetch_array($result); return $company['name']; } $companyId = 1; print cname($db, $companyId); ?> Quote Link to comment Share on other sites More sharing options...
Onloac Posted August 5, 2010 Author Share Posted August 5, 2010 @dolrichfortich: alright, I updated the code to what you provided and its outputting some results. It displays the below in place of the cname(): Array ( [id] => 3 [name] => company1 [website] => http://www.companywebsite.com ) Of course I replaced the company name and website for privacy. What do I do now to get it to work correctly, and what was I doing wrong to not get no result at all? We can see it is grabbing the results, but how come it won't let me display them? @neil.johnson I also tried the code you replied with and it doesn't show any result in place cname(). no error, no text. Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted August 5, 2010 Share Posted August 5, 2010 I am 100% sure that you have not copied my code correctly or changed the database query to match your tables! If you are still using your original code then here is your error: <?php while($company= $db->fetch_array($company)) { $cname = $company['name']; } ?> You are overwriting $company which contains the result set <?php while($x = $db->fetch_array($company)) { $cname = $x['name']; } ?> Quote Link to comment Share on other sites More sharing options...
dolrichfortich Posted August 5, 2010 Share Posted August 5, 2010 Try this one. I wonder why the code from neil.johnson didnt work. function cname($pubid) { global $db; $company= $db->query("SELECT * FROM my_database WHERE id='$pubid'") or die(mysql_error()); $company= $db->fetch_array($company); return $company['name']; } Quote Link to comment Share on other sites More sharing options...
Onloac Posted August 5, 2010 Author Share Posted August 5, 2010 Okay, I've tried everything suggested. The only one that showed any results were: function cname($pubid) { global $db; $company= $db->query("SELECT * FROM my_database WHERE id='$pubid'") or die(mysql_error()); print_r($db->fetch_array($company)); while($x = $db->fetch_array($company)) { $cname = $x['name']; } return $cname; } The result came from the print_r though. I have (4 columns in the database: id, name, description, website). They are spelled exactly like that. I've triple checked. I seriously don't know where to turn. =( I have no clue whats causing this. Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted August 5, 2010 Share Posted August 5, 2010 There is no way the code posted would fail! I think your database wrapper class is probably garbage. The fact that you are using or die(mysql_error()) after an object method call doesn't make sense. The object should handle query errors in an internal method. Where did you get it from? However, the results are returned in an array. Try scrapping that object and use a general mysql function. Copy this code entirely and change the database query to match your table <?php function cname($pubid) { $result = mysql_query("SELECT * FROM my_database WHERE id='".$pubid."'") or die(mysql_error()); $company = mysql_fetch_assoc($result); return $company['name']; } print cname($pubid); ?> Quote Link to comment Share on other sites More sharing options...
Onloac Posted August 5, 2010 Author Share Posted August 5, 2010 Actually I copied die(mysql_error()) from another part of my site and didn't change it. I know the $db works fine as I use it hundreds of times on my site. It's just now I'm trying to include a function file (functions.php) so I can clean my code up with functions. It's my first attempt and for some reason its not working. I can go and use the above code outside the function and it works perfect. its just once I put it in the function it doesn't work. Quote Link to comment Share on other sites More sharing options...
Onloac Posted August 5, 2010 Author Share Posted August 5, 2010 awwww man, I feel like an idiot. I DIDN'T PUT PRINT BEFORE I CALLED THE FUNCTION!!! I got it working now, thanks for all your help guys and sorry for being an idiot. You did help me though!! lol Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.