designanddev Posted January 1, 2013 Share Posted January 1, 2013 I keep getting this error ----> Warning: mysql_fetch_assoc() expects parameter 1 to be resource, Code below $sql = "SELECT id FROM users WHERE first_name =".$this->first_name; $result = mysql_query($sql,$database->connection); while($row = mysql_fetch_assoc($result)){ echo $row['firstname']; } Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/ Share on other sites More sharing options...
atticus Posted January 1, 2013 Share Posted January 1, 2013 I don't know, but does MySQL need an additional " at the end of line 1? Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/#findComment-1402622 Share on other sites More sharing options...
designanddev Posted January 1, 2013 Author Share Posted January 1, 2013 I don't know, but does MySQL need an additional " at the end of line 1? i tried that it breaks my code Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/#findComment-1402625 Share on other sites More sharing options...
designanddev Posted January 1, 2013 Author Share Posted January 1, 2013 when i change my query to select everything it works i am trying to pass in a post variable from my form to check if that name already exists. Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/#findComment-1402626 Share on other sites More sharing options...
devilsvein Posted January 1, 2013 Share Posted January 1, 2013 $firstname = $_POST['firstname']; $sql = "SELECT id FROM users WHERE first_name =" . $firstname . " LIMIT 1"; $result = mysqli_query($mysqli, $sql); $row = mysqli_fetch_assoc($result); echo $row['Firstname']; $mysqli is the database connection. Sorry I did it procedure style, I never learnt object style. Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/#findComment-1402630 Share on other sites More sharing options...
designanddev Posted January 1, 2013 Author Share Posted January 1, 2013 Its still coming up with that error Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/#findComment-1402632 Share on other sites More sharing options...
devilsvein Posted January 1, 2013 Share Posted January 1, 2013 then its something else in your code Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/#findComment-1402633 Share on other sites More sharing options...
designanddev Posted January 1, 2013 Author Share Posted January 1, 2013 I manage to get the error message for when fields have not been filled out, now i am beginning to write the process of checking to see if a current user already exists, is my method query being past the post? i am not sure if it is that why it may have the error Kind Regards Heres my function... // Check for an email first name: public function Reg() { global $database; if(empty($this->last_name)){ $this->errors['one'] = "You forgot to enter your last name."; } if(empty($this->first_name)){ $this->errors['two'] = "You forgot to enter your first name."; } elseif(empty($this->errors)){ // Register the user in the database... $sql = "SELECT id FROM users WHERE first_name =" . $first_name . " LIMIT 1"; $result = mysql_query($sql,$database->connection); while($row = mysql_fetch_assoc($result)){ echo $row['first_name']; } } }//End of Reg Heres the call... if(isset($_POST['submit'])) { $new_user = new User(); $new_user->first_name = $_POST['first_name']; $new_user->last_name = $_POST['last_name']; if($new_user->reg()){ } else { $first_name = ""; $last_name = ""; } } Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/#findComment-1402635 Share on other sites More sharing options...
devilsvein Posted January 1, 2013 Share Posted January 1, 2013 shouldnt it all be mysqli and not mysql Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/#findComment-1402637 Share on other sites More sharing options...
designanddev Posted January 1, 2013 Author Share Posted January 1, 2013 shouldnt it all be mysqli and not mysql im not sure if that matters... i need this resolving Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/#findComment-1402646 Share on other sites More sharing options...
Jessica Posted January 1, 2013 Share Posted January 1, 2013 im not sure if that matters... i need this resolving It does matter. Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/#findComment-1402647 Share on other sites More sharing options...
PFMaBiSmAd Posted January 1, 2013 Share Posted January 1, 2013 (edited) It does matter. +1 In programming, EVERYTHING matters, because computers only do EXACTLY what your code tells them to do. $this->first_name contains a string and must be enclosed in single-quotes within the query statement so that it will be treated as a literal string instead of a mysql keyword or a column/table/database name. If you had some error checking logic in your code to get the mysql error message that is occurring, it would be complaining about an unknown column name that is the value of the entered first_name. Edited January 1, 2013 by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/#findComment-1402652 Share on other sites More sharing options...
designanddev Posted January 1, 2013 Author Share Posted January 1, 2013 +1 In programming, EVERYTHING matters, because computers only do EXACTLY what your code tells them to do. $this->first_name contains a string and must be enclosed in single-quotes within the query statement so that it will be treated as a literal string instead of a mysql keyword or a column/table/database name. If you had some error checking logic in your code to get the mysql error message that is occurring, it would be complaining about an unknown column name that is the value of the entered first_name. ok could you show me an example, i can not structure this logic Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/#findComment-1402663 Share on other sites More sharing options...
designanddev Posted January 2, 2013 Author Share Posted January 2, 2013 (edited) Sorry ladies and gents i am still having trouble passing the posted form values into my method query, i can select all and it works fine but when i start to use WHERE clauses it goes all wrong here's my code please take a look. Kind Regards Submit if(isset($_POST['submit'])) { $new_user = new User(); $first_name = $new_user->first_name = $_POST['first_name']; $last_name = $new_user->last_name = $_POST['last_name']; if($new_user->reg()){ } else { $first_name = ""; $last_name = ""; } } Method public function Reg() { global $database; if(empty($this->last_name)){ $this->errors['one'] = "You forgot to enter your last name."; } if(empty($this->first_name)){ $this->errors['two'] = "You forgot to enter your first name."; } elseif(empty($this->errors)){ // Register the user in the database... $sql = "SELECT id FROM users WHERE first_name =".$this->first_name." LIMIT 1"; $result = mysql_query($sql,$database->connection); while($row = mysql_fetch_assoc($result)){ echo $row['first_name']; } } }//End of Reg Edited January 2, 2013 by designanddev Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/#findComment-1402712 Share on other sites More sharing options...
designanddev Posted January 2, 2013 Author Share Posted January 2, 2013 I fixed the problem i by changing my query syntax to this... $sql = "SELECT * FROM users WHERE first_name ='{$this->first_name}' LIMIT 1"; But i am not sure why this works and the way i normally do my variables in queries did not work, would be nice to hear that explanation. Kind Regards Leo Scott Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/#findComment-1402717 Share on other sites More sharing options...
PFMaBiSmAd Posted January 2, 2013 Share Posted January 2, 2013 Don't start new threads for the same problem. Merging this thread with your previous one... In your previous thread, someone specifically stated you needed single quotes around the value in the query and they also specifically stated why you needed them - $this->first_name contains a string and must be enclosed in single-quotes within the query statement so that it will be treated as a literal string instead of a mysql keyword or a column/table/database name. Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/#findComment-1402747 Share on other sites More sharing options...
designanddev Posted January 2, 2013 Author Share Posted January 2, 2013 Don't start new threads for the same problem. Merging this thread with your previous one... In your previous thread, someone specifically stated you needed single quotes around the value in the query and they also specifically stated why you needed them - What i dnt get is that i have been using double quoutes for my queries all along, can someone explain why? Kind Regards Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/#findComment-1402748 Share on other sites More sharing options...
PFMaBiSmAd Posted January 2, 2013 Share Posted January 2, 2013 Your use of double-quotes was terminating the string you were building (which was started with the opening double-quote), concatenating a value with that string (the first dot), then concatenating (the second dot) another double-quoted string onto the the end of that. $sql = "SELECT id FROM users WHERE first_name =".$this->first_name." LIMIT 1"; This is equivalent to doing - $sql = "SELECT id FROM users WHERE first_name ={$this->first_name} LIMIT 1"; Quote Link to comment https://forums.phpfreaks.com/topic/272588-query-problem/#findComment-1402751 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.