monkeytooth Posted March 4, 2010 Share Posted March 4, 2010 The function function queryTableAll($tableName, $appndQ){ $display_query = "SELECT * FROM ".$tableName." ".$appndQ.""; $display_result = mysql_query($display_query) or die('Error #SQL0004: Query Failed'); $display_results = array($display_result); return $display_results; } The intended use $display_result = queryTableAll("members", "WHERE id LIKE '". mysql_real_escape_string($qID) ."'"); The issue at hand, I don't know why its not working.. I've used similar before in the past but im stuck at the moment. trying to figure out whats going on. when I print_r on the $display_result i get Array ( [0] => Resource id #8 ) Anyone have an idea of what i might have done wrong? Link to comment https://forums.phpfreaks.com/topic/194124-building-a-function-to-query-mysql-tables/ Share on other sites More sharing options...
cags Posted March 4, 2010 Share Posted March 4, 2010 What do you expect the result to be? As far as I can tell the function works and does what you've told it to. Upon success mysql_query returns a resource object. Your function simply tells it to put the Resource object in an array, and return it, which is exactly what is happening. Link to comment https://forums.phpfreaks.com/topic/194124-building-a-function-to-query-mysql-tables/#findComment-1021400 Share on other sites More sharing options...
monkeytooth Posted March 4, 2010 Author Share Posted March 4, 2010 Thats what Im not understanding. It should work but its not for me.. It has worked in similar instances for me, but not this one. I am just tryin to figure it out is all :-) Link to comment https://forums.phpfreaks.com/topic/194124-building-a-function-to-query-mysql-tables/#findComment-1021595 Share on other sites More sharing options...
cags Posted March 5, 2010 Share Posted March 5, 2010 My point was that the output you said you got is exactly what I would expect you to get. The fact you have posted indicates it's not what you want. Perhaps if you told us what the desired output should be we could steer you in the right direction. Assuming it's the data returned by the quey you are after, at the very least I would expect you to require mysql_fetch_row or similar and some kind of loop. Link to comment https://forums.phpfreaks.com/topic/194124-building-a-function-to-query-mysql-tables/#findComment-1021772 Share on other sites More sharing options...
monkeytooth Posted March 6, 2010 Author Share Posted March 6, 2010 Sorry for the late reply.. What I want to do is output the information pooled from the mysql query in the form of a function. So if i did functionName("tablename", "AppendedQurey"); in my code it will query the database as if I were to have put the 3 or 4 lines of code to do so otherwise.. the idea of the function: function queryTableAll($tableName, $appndQ){ $display_result = mysql_query("SELECT * FROM ".$tableName." ".$appndQ."") or die('Error #SQL0004: Query Failed'); $display_row = mysql_fetch_array($display_result); return $display_row; } (i have been toying with it since my first post trying diffrent ideas to make it work..) the idea of useage: $crusername = queryTableAll("members", "WHERE id= '". mysql_real_escape_string($qID) ."'"); I do print_r on $crusername and my result is Array ( [0] => ) so I am a little confused, stuck more like it as to what I should do to make this work. Im open to ideas Link to comment https://forums.phpfreaks.com/topic/194124-building-a-function-to-query-mysql-tables/#findComment-1022306 Share on other sites More sharing options...
cags Posted March 6, 2010 Share Posted March 6, 2010 Try this... $crusername = queryTableAll("members", "WHERE id= '". mysql_real_escape_string($qID) ."'"); var_dump($crusername); function queryTableAll($tableName, $appndQ){ $display_result = mysql_query("SELECT * FROM ".$tableName." ".$appndQ."") or die('Error #SQL0004: Query Failed'); if(mysql_num_rows($display_result) !== 0) { $display_row = mysql_fetch_array($display_result); return $display_row; } return FALSE; } Link to comment https://forums.phpfreaks.com/topic/194124-building-a-function-to-query-mysql-tables/#findComment-1022314 Share on other sites More sharing options...
monkeytooth Posted March 6, 2010 Author Share Posted March 6, 2010 Well that works lovely, but not im stuck again.. and have an extra question for this as well if I want to display only specific info from the array produced how would I do that? The output of the function is now. Array ( [0] => Array ( [0] => 9 [id] => 9 [1] => chacia@campusr........... ) ) (omited the full brunt of the output but thats the idea of it.) the next question is would this function work if I wanted to use it on something else like lets say I want to see how many gmail users I have and pull there id numbers, full emails, and member name up, would I still be able to use this function or would I have to revise it so it can handle more then one row output at a time.. Similar to running the same code in a script but not as a function and putting it through a foreach or while loop Link to comment https://forums.phpfreaks.com/topic/194124-building-a-function-to-query-mysql-tables/#findComment-1022320 Share on other sites More sharing options...
monkeytooth Posted March 6, 2010 Author Share Posted March 6, 2010 Alright I answered my own question I think. I noticed it output 2 arrays.. One being the output itself, and 2 being the mysql query for a lack of better words. I found if I did something like $userinfo = $crusername[0]; $user_name = $userinfo[2]; echo $user_name; I get what I desire for my results. Also with that if the query yielded more than one row I am assuming that I would have a situation where I can do in a for each or while loop or something.. that would output to the effect of $userinfo = $crusername[0]; $userinfo = $crusername[1]; $userinfo = $crusername[2]; and so on.. Link to comment https://forums.phpfreaks.com/topic/194124-building-a-function-to-query-mysql-tables/#findComment-1022323 Share on other sites More sharing options...
cags Posted March 6, 2010 Share Posted March 6, 2010 Generally speaking assoc arrays are simpler to use for the developer as you access the value by name. $crusername['something']; Since you aren't providing a second parameter for mysql_fetch_row this should work, though you are actually receiving two sets of the information, one indexed by number the other indexed by field name. Personally I'd change it to use mysql_fetch_assoc. In order to get more than one rows you need to iterate through the results set. This is how you could do it... $crusername = queryTableAll("members", "WHERE id= '". mysql_real_escape_string($qID) ."'"); var_dump($crusername); function queryTableAll($tableName, $appndQ){ $display_result = mysql_query("SELECT * FROM ".$tableName." ".$appndQ."") or die('Error #SQL0004: Query Failed'); $results = array(); if(mysql_num_rows($display_result) !== 0) { while($row = mysql_fetch_assoc($display_result)) { $results[] = $row; } return $results; } return FALSE; } You would then access the row information using either $crusername[0]['something'] or loop through the information using... foreach($cusername as $key=>$value) { echo $value['something']; } Disclaimer: I typed that out pretty quick don't be too surprised if there's a few minor errors with the code such as typos. Link to comment https://forums.phpfreaks.com/topic/194124-building-a-function-to-query-mysql-tables/#findComment-1022324 Share on other sites More sharing options...
monkeytooth Posted March 6, 2010 Author Share Posted March 6, 2010 again good stuff, I will give that a go, see how that works. thank you Link to comment https://forums.phpfreaks.com/topic/194124-building-a-function-to-query-mysql-tables/#findComment-1022501 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.