Jump to content

Recommended Posts

Hello,I'm trying to get my PHP to select the ID from the table 'users' and use it in an SQL query,I've listed my code;

$id= $_SESSION['id'];$id2 = mysql_query("SELECT * from 'Friends' WHERE 'id1' = '{$id}'"); echo  $id2["id2"] ;?>

this bit works,but I can't "echo $id2["id2"] ;" the above code does not list the id2 that is listed in the database. Any help appreciated!

Link to post
Share on other sites
Remove the quotes around Friends and id1, identifiers are not supposed to be quoted. You only quote a text value.
Still not working,It should at the moment, echo "2", because thats the only record in the database,.
Link to post
Share on other sites

$id2 isn't a record from the database, it's the entire result set. You need to use another function to get a record from the result, like mysql_fetch_assoc if you're using the old mysql extension.

Link to post
Share on other sites
$id2 isn't a record from the database, it's the entire result set. You need to use another function to get a record from the result, like mysql_fetch_assoc if you're using the old mysql extension.
I now have;
$id = mysql_query("SELECT * FROM `users` WHERE `name` = '{$name}' AND `email` = '{$email}'");$_SESSION['id'] = . $id["id"];on the login page ^^^ On the page where i want to use the ID:$id= $_SESSION['id'];$id2 = mysql_query("SELECT * from Friends WHERE id1 = '{$id}'");$eid = mysql_fetch_assoc("{$id2['id2']}");echo  "ID: " . $_SESSION['id'] ;

But it's not picking up $_SESSION['id']

Edited by MarkT
Link to post
Share on other sites
$id = mysql_query("SELECT * FROM `users` WHERE `name` = '{$name}' AND `email` = '{$email}'");$_SESSION['id'] = . $id["id"];

For starters, this won't work. As justsomeguy explained, $id is not an array. It's a resource. You pass it to mysql_fetch_assoc(), and the return value is an array. Same problem below:

$id2 = mysql_query("SELECT * from Friends WHERE id1 = '{$id}'");$eid = mysql_fetch_assoc("{$id2['id2']}");

Pass $id2 to mysql_fetch_assoc(). $eid will then be an array. I don't know the structure of your table, but it looks like the value you want will be returned in $eid['id2']

Edited by Deirdre's Dad
Link to post
Share on other sites
$id = mysql_query("SELECT * FROM `users` WHERE `name` = '{$name}' AND `email` = '{$email}'");$_SESSION['id'] = . $id["id"];

For starters, this won't work. As justsomeguy explained, $id is not an array. It's a resource. You pass it to mysql_fetch_assoc(), and the return value is an array. Same problem below:

$id2 = mysql_query("SELECT * from Friends WHERE id1 = '{$id}'");$eid = mysql_fetch_assoc("{$id2['id2']}");

Pass $id2 to mysql_fetch_assoc(). $eid will then be an array. I don't know the structure of your table, but it looks like the value you want will be returned in $eid['id2']

Can you please edit my code to show me?
Link to post
Share on other sites

I have edited it to this, is it right?

$id = mysql_query("SELECT * FROM `users` WHERE `name` = '{$name}' AND `email` = '{$email}'");  $ida = mysql_fetch_assoc({$id})$_SESSION['id'] = $ida['id2'];

Link to post
Share on other sites

You shouldn't need the '{ }' characters. They are generally used to disambiguate arrays. And add a semicolon to the end of line 2. Other than that, it looks OK. You should probably switch to the mysqli functions. The mysql functions are deprecated and will eventually disappear. You should also add some error handling. The example here shows ways to do that.

Edited by Deirdre's Dad
Link to post
Share on other sites
You shouldn't need the '{ }' characters. They are generally used to disambiguate arrays. And add a semicolon to the end of line 2. Other than that, it looks OK. You should probably switch to the mysqli functions. The mysql functions are deprecated and will eventually disappear. You should also add some error handling. The example here shows ways to do that.
On another page i have this code;
$id= $_SESSION['id'];$id2 = mysql_query("SELECT * from Friends WHERE id1 = '{$id}'");$eid = mysql_fetch_assoc($id2);echo  "ID: " . $eid['id2'] ;?>

But the echo isn't echo'ing what it should be, at the moment there is a entry where id1=1, which is the ID i'm logged in as which should be set in $_SESSION['id']and ID2 = 2, so it should echo 2, however it's not. It's saying;Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/u872116037/public_html/home.html on line 66 ID:

Link to post
Share on other sites

That means your query had an error, you need to check for errors from MySQL like Dad showed. If a query has an error then it will return boolean false instead of a resource, so that's why the error message says it was expecting a resource but got a boolean. After you run the query you need to check if it succeeded before you go on.

Link to post
Share on other sites
That means your query had an error, you need to check for errors from MySQL like Dad showed. If a query has an error then it will return boolean false instead of a resource, so that's why the error message says it was expecting a resource but got a boolean. After you run the query you need to check if it succeeded before you go on.
$id= $_SESSION['id'];$id2 = mysql_query("SELECT * from Friends WHERE id1 = '$id'"); Is my query, but i dont see any problems with it?
Link to post
Share on other sites
Yeah, I don't either. MySQL obviously sees a problem with it though. You should ask MySQL what the problem is.
how do i do that>?
Link to post
Share on other sites
Look at the examples here, they call the error function if the query fails: http://www.php.net/m...mysql-query.php
$id= $_SESSION['id'];$id2 = mysql_query("SELECT * from friends WHERE id1 = '{$id}'");$eid = mysql_fetch_assoc($id2);if (!$id2) { die('Invalid query: ' . mysql_error());}echo "ID: " . $eid['id2'] ; is my current code;It's not displaying any errors, but it's also not displaying "2" because |I have a record where ID 1 = 1 (My session['id']) and ID2 = 2
Link to post
Share on other sites
After you run the query you need to check if it succeeded before you go on.
I would be checking the query first before anything else
$id= $_SESSION['id'];$id2 = mysql_query("SELECT * from friends WHERE id1 = '{$id}'");if (!$id2) {  die('Invalid query: ' . mysql_error());[/font][/color]}

Edited by thescientist
Link to post
Share on other sites

Print out the query so that you can run it directly on phpMyAdmin. If it's printing a blank ID then it sounds like it's not finding a matching record, or the field in that record is blank. Print the query so you can run it on the database and see what it returns.

Link to post
Share on other sites

No, there's no reason why a given query would return one set of results in one script and a different set in another script, that's not how databases work. It's going to return exactly what you tell it to return. If it's not returning anything, then there's nothing to return. If there was something to return then it wouldn't skip it. If you're telling me that you print the query that you're running in PHP, and you run that exact same query in phpMyAdmin, and it returns results there, then it's going to return the same results in PHP. Something's missing, but I don't know what it is. The code that you've shown looks fine to me. Maybe you're assuming incorrect things about what is in the session or something like that, but if you say that you used PHP to print out the query that you are actually running, and that query returns results in phpMyAdmin, then the query is not the problem. If you didn't actually print the query from PHP, but just assumed you knew what it was going to be, then that's not going to help you debug the problem. Don't assume that you know what anything is, always verify.

Link to post
Share on other sites
No, there's no reason why a given query would return one set of results in one script and a different set in another script, that's not how databases work. It's going to return exactly what you tell it to return. If it's not returning anything, then there's nothing to return. If there was something to return then it wouldn't skip it. If you're telling me that you print the query that you're running in PHP, and you run that exact same query in phpMyAdmin, and it returns results there, then it's going to return the same results in PHP. Something's missing, but I don't know what it is. The code that you've shown looks fine to me. Maybe you're assuming incorrect things about what is in the session or something like that, but if you say that you used PHP to print out the query that you are actually running, and that query returns results in phpMyAdmin, then the query is not the problem. If you didn't actually print the query from PHP, but just assumed you knew what it was going to be, then that's not going to help you debug the problem. Don't assume that you know what anything is, always verify.
If you think of anything I could do! Let me know!!!!!
Link to post
Share on other sites

Try temporarily dumping more data. echo var_dump($_SESSION) . '<br>';$id= $_SESSION['id'];echo var_dump($id) . '<br>';$id2 = mysql_query("SELECT * from friends WHERE id1 = '{$id}'");echo var_dump($id2) . '<br>';$eid = mysql_fetch_assoc($id2);echo var_dump($eid);

Link to post
Share on other sites
Try temporarily dumping more data. echo var_dump($_SESSION) . '<br>';$id= $_SESSION['id'];echo var_dump($id) . '<br>';$id2 = mysql_query("SELECT * from friends WHERE id1 = '{$id}'");echo var_dump($id2) . '<br>';$eid = mysql_fetch_assoc($id2);echo var_dump($eid);
array(4) { ["id"]=> NULL Tried that and thats what I got ^^I think it's not saving the ID of the session in the below code;$id = mysql_query("SELECT * FROM `users` WHERE `name` = '{$name}' AND `email` = '{$email}'");$ida = mysql_fetch_assoc($id);$_SESSION['id'] = $ida['id2'];
Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...