Jump to content

Recommended Posts

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!

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?

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?

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?

 

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?

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?

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.

 

 

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 :(

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' ";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.