TehChikenHater Posted July 16, 2012 Share Posted July 16, 2012 Hiay folks! So, I'm creating a search for my website, using Mysql_Query to find the user my 'searcher' was looking for, so, I kind of ran in to a problem. My code runs and, will alway's, print 'Resource ID #4', cause that's apparently what my mysql_query would like to 'echo'. Anyway's, what would happen is, when it reads the variable from the top of my url (Using PHP Arguments), it's using mysql_query to search from my database for the name. If it finds the name, it's suppose to echo, 'found him!', if not then echo, 'didnt find him!'. Code is shown below. <!DOCTYPE html> <html> <head> <link REL=StyleSheet HREF="MainCSS.css" TYPE="text/css" MEDIA=screen> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> [code=php:0] <?php $link = mysql_connect('localhost', 'root', ''); if (!$link) { die('Could not connect: ' . mysql_error()); } $name=$_REQUEST['name']; $fetch=mysql_query("SELECT * FROM users.userdata WHERE username='$name'"); if (!$fetch) { die('Invalid query: ' . mysql_error()); } if ($fetch==0) { echo 'Didnt find the user'; } mysql_close($link); ?> </body> </html> [/code] Another question I have, is how do I escape the 's? So I can echo "don't", and stuff? Quote Link to comment https://forums.phpfreaks.com/topic/265761-mysql_query-help/ Share on other sites More sharing options...
TehChikenHater Posted July 16, 2012 Author Share Posted July 16, 2012 Sorry, forget to mention this, the way the search works, it by URL addressing. When a user searches in my search bar, it takes them to: localhost/user.php?name=$searchdata And then the above code, would go through and do the mysql_query-ing and stuff. Quote Link to comment https://forums.phpfreaks.com/topic/265761-mysql_query-help/#findComment-1361897 Share on other sites More sharing options...
KevinM1 Posted July 16, 2012 Share Posted July 16, 2012 You actually have to fetch the data. mysql_query returns, as you can see, a resource. You need to use one of the fetch functions (mysql_fetch_array, mysql_fetch_assoc, or mysql_fetch_object) to actually get at the data. Quote Link to comment https://forums.phpfreaks.com/topic/265761-mysql_query-help/#findComment-1361899 Share on other sites More sharing options...
TehChikenHater Posted July 16, 2012 Author Share Posted July 16, 2012 Hmm. I tried what you said, but I still can't get it. v.v $fetch=mysql_query("SELECT * FROM users.userdata WHERE username='$name'"); $final=mysql_fetch_array($fetch,MYSQL_BOTH); EDIT: Some code would be helpful, as I am 13, and very new to PHP. Quote Link to comment https://forums.phpfreaks.com/topic/265761-mysql_query-help/#findComment-1361900 Share on other sites More sharing options...
KevinM1 Posted July 16, 2012 Share Posted July 16, 2012 Ah, some of it was my my mistake. I thought you wanted to echo the user's name if found. To simply see if database rows were returned, do: $query = "SELECT * FROM users.userdata WHERE username = $name"; $result = mysql_query($query); if (mysql_num_rows($result) > 0) { // found } else { // not found } Of course, you still need to ensure that the query itself worked. I'll leave that for you as homework. If you actually want to do something with the data, then you need to fetch it: $query = "SELECT * FROM users.userdata WHERE username = $name"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); echo $row['username']; Quote Link to comment https://forums.phpfreaks.com/topic/265761-mysql_query-help/#findComment-1361903 Share on other sites More sharing options...
TehChikenHater Posted July 16, 2012 Author Share Posted July 16, 2012 Thanks buddy! Quote Link to comment https://forums.phpfreaks.com/topic/265761-mysql_query-help/#findComment-1361905 Share on other sites More sharing options...
KevinM1 Posted July 16, 2012 Share Posted July 16, 2012 Quote Link to comment https://forums.phpfreaks.com/topic/265761-mysql_query-help/#findComment-1361906 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.