2

I know you can't directly use PHP variables inside javascript code, but is there a way around it? I need to use these parameter's in javascript:

username: '<?php echo $user_id;?>.example.co.uk',
password: 'example',

Instead of showing the $user_id variable, it outputs: "Resource id #4.example.co.uk"

I'm quite new to javascript so I'm finding it pretty tricky.

Thanks for any help

Edit: I am taking the $user_id variable from the URL like so:

$user_id = mysql_real_escape_string($_GET["user_id"]);

so shouldn't it just have the set value from the URL?

Daniel H
  • 2,865
  • 8
  • 34
  • 32

5 Answers5

2

I'm a little rusty on PHP, but this may work:

<? //start HTML/Javascript code... 
    echo'var variable = ' . $user_id . '.example.co.uk';
    //finish HTML/Javascript here..
?>
Thor Jacobsen
  • 8,621
  • 2
  • 27
  • 26
2

You can assign that php value in any html object then you use the object value in js page

for example

PHP:

$phpvalue = $_GET["sid"];

HTML:

<input type="text" value="<?php echo $phpvalue ;?>" id="txtsamp" />

JS:

var phpval = document.getElementById('txtsamp').value;
Bryan Drewery
  • 2,569
  • 19
  • 13
Inno
  • 33
  • 7
0

Resource id #4 means u have just not complete the mysql query

mysql_query returns Resource id

u have to fetch the data using while ($row=mysql_fetch_array()) and then use

written here

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

UPDATE

don't use mysql_real_escape string for GET data..instead use (int)$_GET['id']

see here

If no connection is found or established, an E_WARNING level error is generated

xkeshav
  • 53,360
  • 44
  • 177
  • 245
  • Hi diEcho, thanks for your reply. I am taking the $user_id variable like so: $user_id = mysql_real_escape_string($_GET["user_id"]); so I'm not sure how I would use the while... code. Could you show me please? Thanks – Daniel H May 09 '11 at 10:32
  • `mysql_query returns Resource id #4`. `#4` is not true in all the cases – Shakti Singh May 09 '11 at 10:34
  • I have removed the real_escape part so now I have $user_id = $_GET["user_id"]; but I'm not sure what to put in the (int) section. After just removing the real_escape code the output is still the same – Daniel H May 09 '11 at 10:44
  • u are retrieving data from database using that user_id...right?? so then u simple `mysql_query('your query');` and then fetch data using`mysql_fetch_array()` and then do whatever u want...debug one by one – xkeshav May 09 '11 at 11:00
  • I'm not using user_id to touch the database in any way, all I want it to do is display the username on the page so that people can access different URLs to use different usernames (internal thing) – Daniel H May 09 '11 at 11:02
  • I tried setting $user_id like this instead to see if it was the GET that was causing the problem: $user_id = "test"; but this still outputs the same resource error (but it echoes fine elsewhere on the page) – Daniel H May 09 '11 at 11:04
  • but where is that usename?? you r only sending user_id ( i think it's reference to database entry of that user)..or tell me where is the username?? is it also sent by GET ?? – xkeshav May 09 '11 at 11:04
  • ok so your `user_id is `string` not an `integer`...if it is text then try with addslashes($_GET['user_id']` – xkeshav May 09 '11 at 11:07
  • Hi diEcho, I tried moving the $user_id variable into the javascript code as well instead of the area and it now works, not sure why :S. So this is working: username: '.example.co.uk', Thanks for your help :) – Daniel H May 09 '11 at 11:08
0

If you have selected only one column (and expect 1 row), you can use:

$user_id = mysql_result($user_id, 0);

which would fetch the 1st column of the 1st row of the result set $user_id.

But for clarity's sake you should rename the variable $user_id, because it's not an ID but a resource (so call it $userQuery or something =)).

Rudie
  • 52,220
  • 42
  • 131
  • 173
0

You can't use the result of a mysql_query() directly. Instead, you have to use mysql_result() or something similar (depending on your SQL query).

abaumg
  • 2,083
  • 1
  • 16
  • 24