Niixie Posted December 2, 2011 Share Posted December 2, 2011 Hey freaks! I have a problem, i can't figure out how to recieve one single string from the database, i tried alot of things. public function getpass($name){ $q = sprintf("SELECT password FROM database WHERE name='%s'", mysql_real_escape_string($name)); $result = mysql_query($q) or die(mysql_error()); // And here i tried every single way to fetch the data. Wich one should i use when its only one slot in a row i need? } I hope you can help me! Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 2, 2011 Share Posted December 2, 2011 Try fetch row. All explained here - http://uk3.php.net/mysql_fetch_row Quote Link to comment Share on other sites More sharing options...
Niixie Posted December 2, 2011 Author Share Posted December 2, 2011 Thank you very much, but it doesn't work. $query = "SELECT question FROM registertest ORDER BY RAND() LIMIT 1"; $result = mysql_query($query) or die(mysql_error()); if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } $row = mysql_fetch_row($result); $_SESSION['registertestquestion'] = $row[0]; return $row; The session part is because i need it later again. At this point, it returns nothing? Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 2, 2011 Share Posted December 2, 2011 Couple of things: 1) did you start the session? Can;t see that in your code. 2)Before you set the session variable have you tried just echoing the query results to make sure you're getting data? Sorry if i'm stating the obvious. Thank you very much, but it doesn't work. $query = "SELECT question FROM registertest ORDER BY RAND() LIMIT 1"; $result = mysql_query($query) or die(mysql_error()); if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } $row = mysql_fetch_row($result); $_SESSION['registertestquestion'] = $row[0]; return $row; The session part is because i need it later again. At this point, it returns nothing? Quote Link to comment Share on other sites More sharing options...
Niixie Posted December 2, 2011 Author Share Posted December 2, 2011 The function is in a separat file, and when i add session_start() in the very beginning, it gives me an error saying, that the session already is started, therefore, there is session_start() in the top. Is that wrong? Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 2, 2011 Share Posted December 2, 2011 Bit hard to say without seeing the code in it's entirety i'd say. But yeah if you have session start at the top and it's the first thing then that should be ok. First off i would ensure you're getting data from your query. So comment out your session bit and just try echoing your $row[0] to make sure that bit is ok. Can you post more of the code and the function you start your session in? The function is in a separat file, and when i add session_start() in the very beginning, it gives me an error saying, that the session already is started, therefore, there is session_start() in the top. Is that wrong? Quote Link to comment Share on other sites More sharing options...
Niixie Posted December 2, 2011 Author Share Posted December 2, 2011 Alright, check this out. I check the functions with this piece of code: $d = get::registerquestion(); print_r($d); print_r(get::registeranswer($d); The registerquestion will get a random question from the database, heres the code: public function registerquestion(){ $query = "SELECT question FROM registertest ORDER BY RAND() LIMIT 1"; $result = mysql_query($query) or die(mysql_error()); if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } $row = mysql_fetch_row($result); //$_SESSION['registertestquestion'] = $row[0]; return $row; } The print_r($d); prints the array that the function returns. The get::registeranswer(..) gets the answer of the question (get::registerquestion), and should return the answer, but it doesn't. Heres the code for the registeranswer function public function registeranswer($question) { $query = sprintf("SELECT answer FROM registertest WHERE question='%s'", mysql_real_escape_string($question)); // THIS IS LINE 177 $result = mysql_query($query) or die(mysql_error()); if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } $row = mysql_fetch_row($result); //$_SESSION['registertestanswer'] = $row[0]; return $row; } When running the very first piece of code, this returns: Array ( [0] => Forkortelsen for Danmark er? (To bogstaver) ) Array ( ! ) Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in C:\Drive...\class.php on line 177 Call Stack # Time Memory Function Location 1 0.0006 684928 {main}( ) ..\register.php:0 2 0.0057 787072 get::registeranswer( ) ..\register.php:114 3 0.0057 787240 mysql_real_escape_string ( ) ..\class.php:177 Did that give you a better view? Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 2, 2011 Share Posted December 2, 2011 Ok. Well first thing is to fix that error. Have you tried removing the mysql_real_escape_string($question)); // THIS IS LINE 177 from that line of code and seeing if it works then? That line is looking for a variable $question that hasn't been assigned yet (at least that's how the logic goes in my head). I would start by removing that and seeing what you get. Quote Link to comment Share on other sites More sharing options...
Niixie Posted December 2, 2011 Author Share Posted December 2, 2011 No, that would be weird. Then the line above doesn't know what %s is? Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 2, 2011 Share Posted December 2, 2011 Oh sorry didn't see that bit. Mmm then i am a bit stuck on this one too :/ Quote Link to comment Share on other sites More sharing options...
Niixie Posted December 2, 2011 Author Share Posted December 2, 2011 Dammit. If i can get the function get::registerquestion, to return the right string, then i can transfer it with a hidden textbox, so no session needed. But I still have the problem with the function. Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 3, 2011 Share Posted December 3, 2011 Yeah it's really hard one mate. I would just try simplifying the query in your function to try and get it working so you can identify the problem. That's about all i can suggest at the mo :/ There are greater php minds here than mine tho so sure someone can help ya Quote Link to comment Share on other sites More sharing options...
Errant_Shadow Posted December 3, 2011 Share Posted December 3, 2011 Try not escaping the string and seeing what it gives you? Or try escaping the string outside of assembling the query? Or perhaps try another method besides sprintf. $q = mysql_real_escape_string($question); $query = "SELECT answer FROM registertest WHERE question='$q' "; 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.