1

I am trying to get a value from database using the code below. I want to save the value in the variable category so I can give this as parameter to a function. The id is dynamically given. Is the code below correct? because when trying this nothing works...

$thecategory = mysql_query("SELECT TYPE FROM lists WHERE id =" . this.id);
2
  • 3
    this.id - is not corrent PHP syntax Commented Mar 9, 2014 at 18:54
  • I guess it's not inside a class either because that way you should've syntax problems earlier than this, But it seems you're using javascript, if that's the case you can't use it this way. Commented Mar 9, 2014 at 19:04

3 Answers 3

1

The use of '$id' provides a little security against sql injection

$thecategory = mysql_query("SELECT `TYPE` FROM `lists` WHERE `id` ='$id'");
$associate = mysql_fetch_assoc($thecategory);
$TYPE = $associate['TYPE'];
Sign up to request clarification or add additional context in comments.

3 Comments

I have this php code and I want to embed the above code into a jquery part of code. The id is dynamically passed. I try creating a function that get the id and pass it to the query but I failed. I am not so familiar with mysql.
I didn't really know how you get the id with jquery but when I do this: <script> var id = '1'; </script> <?php $id = '<script> document.write(id) </script>'; echo $id; ?> that kind of works for me but don't know if this is the answer you are looking for. I hope this points you in the right direction
It doesn't work. Tested it and got nothing. I think I know the problem. It what the other answer says, I should use ajax...
0

Use mysql_result:

$thecategory = mysql_query("SELECT TYPE FROM lists WHERE id =" . $id); // changed $id
$type = mysql_result($thecategory, 0);

I suggest that you use PDO instead of mysql_* functions, they are deprecated.

4 Comments

I have this php code and I want to embed the above code into a jquery part of code. The id is dynamically passed. I try creating a function that get the id and pass it to the query but I failed. I am not so familiar with mysql.
If you're using AJAX with jquery, you can post the id by it, and get data in php like: $id = $_POST['id'];
Id is ok. I can retrieve it, the problem actually is the query. For some reason it doesn't work. I notice tha mysql_query needs the connection, but I am already connected to the database and I tried $(".statrow").click(function() { var $theID = this.id; var $query = ("SELECT TYPE FROM lists WHERE id = '$theID'"); var $result = mysql_query($query); var $TYPE = mysql_fetch_assoc($result); dopclick(this.id, $TYPE); }); Nothing. When having values instead of queries it works fine. In the other files the queries are done by simply putting sqlQuery or sqlStatement
Looks like that you are using PHP and jQuery together. jQuery runs at client, PHP at server. So you can't use them together in code. Use AJAX to communicate PHP with jQuery. api.jquery.com/jQuery.ajax
0

Try:

$thecategory = mysql_query("SELECT TYPE FROM lists WHERE id =" . this->id);

1 Comment

You could try to isolate the query string as a variable and add the this.id or this->id part to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.