MarkT Posted June 10, 2013 Share Posted June 10, 2013 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 comment Share on other sites More sharing options...
justsomeguy Posted June 10, 2013 Share Posted June 10, 2013 Remove the quotes around Friends and id1, identifiers are not supposed to be quoted. You only quote a text value. Link to comment Share on other sites More sharing options...
MarkT Posted June 10, 2013 Author Share Posted June 10, 2013 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 comment Share on other sites More sharing options...
justsomeguy Posted June 10, 2013 Share Posted June 10, 2013 $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 comment Share on other sites More sharing options...
MarkT Posted June 11, 2013 Author Share Posted June 11, 2013 (edited) $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 June 11, 2013 by MarkT Link to comment Share on other sites More sharing options...
jeffman Posted June 11, 2013 Share Posted June 11, 2013 (edited) $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 June 11, 2013 by Deirdre's Dad Link to comment Share on other sites More sharing options...
MarkT Posted June 11, 2013 Author Share Posted June 11, 2013 $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 comment Share on other sites More sharing options...
MarkT Posted June 11, 2013 Author Share Posted June 11, 2013 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 comment Share on other sites More sharing options...
jeffman Posted June 11, 2013 Share Posted June 11, 2013 (edited) 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 June 11, 2013 by Deirdre's Dad Link to comment Share on other sites More sharing options...
MarkT Posted June 11, 2013 Author Share Posted June 11, 2013 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 comment Share on other sites More sharing options...
justsomeguy Posted June 11, 2013 Share Posted June 11, 2013 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 comment Share on other sites More sharing options...
MarkT Posted June 12, 2013 Author Share Posted June 12, 2013 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 comment Share on other sites More sharing options...
justsomeguy Posted June 12, 2013 Share Posted June 12, 2013 Yeah, I don't either. MySQL obviously sees a problem with it though. You should ask MySQL what the problem is. Link to comment Share on other sites More sharing options...
MarkT Posted June 12, 2013 Author Share Posted June 12, 2013 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 comment Share on other sites More sharing options...
justsomeguy Posted June 12, 2013 Share Posted June 12, 2013 Look at the examples here, they call the error function if the query fails: http://www.php.net/manual/en/function.mysql-query.php Link to comment Share on other sites More sharing options...
MarkT Posted June 12, 2013 Author Share Posted June 12, 2013 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 comment Share on other sites More sharing options...
thescientist Posted June 12, 2013 Share Posted June 12, 2013 (edited) 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 June 12, 2013 by thescientist Link to comment Share on other sites More sharing options...
justsomeguy Posted June 12, 2013 Share Posted June 12, 2013 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 comment Share on other sites More sharing options...
MarkT Posted June 13, 2013 Author Share Posted June 13, 2013 I have done this, and it shows the correct record in the database, but won't show it on the page. Link to comment Share on other sites More sharing options...
justsomeguy Posted June 13, 2013 Share Posted June 13, 2013 I don't see anything in the code you posted that would stop the query or not print what it returned. Link to comment Share on other sites More sharing options...
MarkT Posted June 14, 2013 Author Share Posted June 14, 2013 I don't see anything in the code you posted that would stop the query or not print what it returned.Any more suggestions? Link to comment Share on other sites More sharing options...
justsomeguy Posted June 14, 2013 Share Posted June 14, 2013 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 comment Share on other sites More sharing options...
MarkT Posted June 14, 2013 Author Share Posted June 14, 2013 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 comment Share on other sites More sharing options...
jeffman Posted June 14, 2013 Share Posted June 14, 2013 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 comment Share on other sites More sharing options...
MarkT Posted June 16, 2013 Author Share Posted June 16, 2013 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 comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now