neex1233 Posted June 25, 2009 Share Posted June 25, 2009 I made a script (Below). <?php $con = mysql_connect("localhost","Username","Password"); mysql_select_db("Database", $con); $username = mysql_real_escape_string($_POST["username"]); $sql = 'SELECT * FROM `users` WHERE username = \'$username\' LIMIT 0, 30 '; $echo = mysql_query($sql) or die (mysql_error()); echo "$echo"; ?> But I get this error: Resource id #3 Can somebody help me? By the way, the $username variable is always set through a form. Link to comment https://forums.phpfreaks.com/topic/163689-php-error/ Share on other sites More sharing options...
mikesta707 Posted June 25, 2009 Share Posted June 25, 2009 try using the single quote escape characters (\') around users (your table name) Link to comment https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863716 Share on other sites More sharing options...
WolfRage Posted June 25, 2009 Share Posted June 25, 2009 Resource id #3 - no data from simple queries Remember Google is your friend! Link to comment https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863717 Share on other sites More sharing options...
flyhoney Posted June 25, 2009 Share Posted June 25, 2009 <?php $con = mysql_connect("localhost", "Username", "Password") or die('Could not connect: ' . mysql_error()); mysql_select_db("Database", $con); $username = mysql_real_escape_string($_POST["username"]); $sql = " SELECT * FROM `users` WHERE username = '$username' LIMIT 0, 30 "; $result = mysql_query($sql) or die (mysql_error()); while ($row = mysql_fetch_array($result)) { print_r($row); } Link to comment https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863720 Share on other sites More sharing options...
neex1233 Posted June 25, 2009 Author Share Posted June 25, 2009 That works but it shows the result as: (I hid the username and stuff...) Don't Look [2] => Don't Look [password] => Don't Look [3] => Don't Look [userlevel] => Don't Look [4] => Don't Look => Don't Look [5] => Don't Look [tokens] => Don't Look [6] => Don't Look [name] => Don't Look [7] => Don't Look[iP] => Don't Look ) Link to comment https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863730 Share on other sites More sharing options...
WolfRage Posted June 25, 2009 Share Posted June 25, 2009 That is because it made you an array, now what do you intend to do with this array? Link to comment https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863735 Share on other sites More sharing options...
neex1233 Posted June 25, 2009 Author Share Posted June 25, 2009 Show the user information. Link to comment https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863736 Share on other sites More sharing options...
flyhoney Posted June 25, 2009 Share Posted June 25, 2009 That works but it shows the result as: (I hid the username and stuff...) Don't Look [2] => Don't Look [password] => Don't Look [3] => Don't Look [userlevel] => Don't Look [4] => Don't Look => Don't Look [5] => Don't Look [tokens] => Don't Look [6] => Don't Look [name] => Don't Look [7] => Don't Look[iP] => Don't Look ) Thats called an array in PHP. print_r() is an incredibly usefull php function that prints a variable as a string that shows you the structure of the variable. I used it to show you how you can get every row of your query result as an array. Link to comment https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863737 Share on other sites More sharing options...
mikesta707 Posted June 25, 2009 Share Posted June 25, 2009 thats because you are echoing an array. you have to use foreach or something to echo every element of the array foreach ($echo as $key => $value) { echo "Key: ".$key." Value: ".$value."<br />"; } that would show each entry in the array, plus its corresponding key Link to comment https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863738 Share on other sites More sharing options...
WolfRage Posted June 25, 2009 Share Posted June 25, 2009 Sorry Mike beat me to it, but this may help yet. That is exactly what you did though, so now you need to organize the information and display it the way you want it displayed, by accessing the individual values in that array. <?php echo $row['name']; ?> Link to comment https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863742 Share on other sites More sharing options...
neex1233 Posted June 25, 2009 Author Share Posted June 25, 2009 Ok, well this is the code I'm using: <?php $con = mysql_connect("localhost", "Username", "Password") or die('Could not connect: ' . mysql_error()); mysql_select_db("Database", $con); $username = mysql_real_escape_string($_POST["username"]); $sql = " SELECT * FROM `users` WHERE username = '$username' LIMIT 0, 30 "; $result = mysql_query($sql) or die (mysql_error()); while ($row = mysql_fetch_array($result)) echo $row['id']; echo $row['username']; echo $row['password']; echo $row['userlevel']; echo $row['email']; echo $row['tokens']; echo $row['name']; echo $row['IP']; ?> But it only displays the first echo, which is id Link to comment https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863746 Share on other sites More sharing options...
WolfRage Posted June 25, 2009 Share Posted June 25, 2009 Use the foreach method to be sure you have the right key names. Then go from there. <?php foreach ($row as $key => $value) { echo "Key: ".$key." Value: ".$value."<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863749 Share on other sites More sharing options...
Andy-H Posted June 25, 2009 Share Posted June 25, 2009 <?php $con = mysql_connect("localhost", "Username", "Password") or die('Could not connect: ' . mysql_error()); mysql_select_db("Database", $con); $username = mysql_real_escape_string($_POST["username"]); $sql = " SELECT * FROM `users` WHERE username = '$username' LIMIT 0, 30 "; $result = mysql_query($sql) or die (mysql_error()); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo $row['id']; echo $row['username']; echo $row['password']; echo $row['userlevel']; echo $row['email']; echo $row['tokens']; echo $row['name']; echo $row['IP']; } ?> If you dont use curly braces in the statement it assumes the statement to end at the first semicolon after the closing bracket. Also you are selecing more data than needed, I added the MYSQL_ASSOC but I wouldnt use SELECT * unless you need all or most of the data from the table. Link to comment https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863750 Share on other sites More sharing options...
neex1233 Posted June 25, 2009 Author Share Posted June 25, 2009 Thanks everybody! I got it to work! Link to comment https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863756 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.