Usarjjaco Posted March 31, 2010 Share Posted March 31, 2010 Hey Guys,Back with another rookie question I'm sure. Got my login script working properly; however I'm trying to now access data information for a sepcific user. Here's what code I'm using: session_start();$uname = $_SESSION['username']; $con = mysql_connect("localhost","username","pass");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("theripti_jml5", $con);$result =("SELECT * FROM ranchers WHERE username=$uname");echo "<table border='1'><tr><th>Owner ID</th><th>RegNum</th><th>Breed</th></tr>";while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['ownerID'] . "</td>"; echo "<td>" . $row['regnum'] . "</td>"; echo "<td>" . $row['breed'] . "</td>"; echo "</tr>";} echo "</table>";mysql_close($con);?> It should bring up 2 results from the Table "ranchers" because of the query to pull all data with the signed in username.However I get this error:Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home5/theripti/public_html/virtuarancher/myranch.phpI put the @ sign in front of the query and that loads the table without any data. Your help would be appreciated. Also worth noting: When I'm doing a query for something like "SELECT * from ranchers" it works fine; but when I try to link it with the sessions user it gives me the error.Thanks in advance guys,JJ Link to comment Share on other sites More sharing options...
justsomeguy Posted March 31, 2010 Share Posted March 31, 2010 Unless the username field is a number field, you'll need to surround the field value in quotes. Link to comment Share on other sites More sharing options...
Usarjjaco Posted March 31, 2010 Author Share Posted March 31, 2010 Unless the username field is a number field, you'll need to surround the field value in quotes.Ok to clarify (because of my ignorance heh) Which field value should I surround from the code? username is not a number value. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 31, 2010 Share Posted March 31, 2010 $result =("SELECT * FROM ranchers WHERE username=$uname");When MySQL gets the query, it should be formed like this:SELECT * FROM ranchers WHERE username='xxx' Link to comment Share on other sites More sharing options...
Usarjjaco Posted March 31, 2010 Author Share Posted March 31, 2010 $result =("SELECT * FROM ranchers WHERE username=$uname");When MySQL gets the query, it should be formed like this:SELECT * FROM ranchers WHERE username='xxx'Ok; when I perform the line$result =(SELECT * From ranchers WHERE username='$uname');I get the error:Parse error: syntax error, unexpected T_STRING in /home5/theripti/public_html/virtuarancher/myranch.php on line 15=/I feel ridiculous not having a clue here .. but thanks for your help Link to comment Share on other sites More sharing options...
justsomeguy Posted March 31, 2010 Share Posted March 31, 2010 You still need double quotes around the entire query, the query is a string. The value for the username field in the query needs single quotes around it. Link to comment Share on other sites More sharing options...
Usarjjaco Posted March 31, 2010 Author Share Posted March 31, 2010 You still need double quotes around the entire query, the query is a string. The value for the username field in the query needs single quotes around it.Ok got past that error; and got one last thing holding me up:Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home5/theripti/public_html/virtuarancher/myranch.php on line 24That line of code is:while($row = mysql_fetch_array($result)) Link to comment Share on other sites More sharing options...
ShadowMage Posted March 31, 2010 Share Posted March 31, 2010 Its looking for a resource. The way you have it set up $result is a string, not a resource. Your query line should look like this:$result = mysql_query("SELECT * FROM ranchers WHERE username='$uname'"); Link to comment Share on other sites More sharing options...
Usarjjaco Posted March 31, 2010 Author Share Posted March 31, 2010 Answered my own question after reviewing; simple case of forgetting the "my" in the username field.Thanks for all your help guys!JJ Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.