physaux Posted October 9, 2009 Share Posted October 9, 2009 Ok guys, i made a database with PHPMyAdmin. I connected to it using the following script: $connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS); if (!$connection) { die("Database connection failed: " . mysql_error()); } $db_select = mysql_select_db(DB_NAME,$connection); if (!$db_select) { die("Database selection failed: " . mysql_error()); } echo "yes you actually ran the code without errors"; I ran the php file, and i did NOT get any ERROR, except for the following text: yes you actually ran the code without errors So i know that is not the problem. I need help with the following: I am trying to make a function that will search for a value in the database. I have a 1% clue what i am doing here, and that is probably what is wrong. Anyways, here is what i have so far: function returndataforname($name){ $result = mysql_query("SELECT * FROM 'users'", $connection); if (!$result) { die("Database query failed: " . mysql_error()); } while ($row = mysql_fetch_array($result)) { if($row["name"] == $name){ $returnvalue['language']=$row['language']; $returnvalue['name']=$row['name']; $returnvalue['password']=$row['password']; return $returnvalue; } } } I have no clue what is going on. Could somebody please help me out, thank you so much!! Link to comment https://forums.phpfreaks.com/topic/177045-solved-php-mysql-query-problem-simple-solution-i-just-dont-get-it-help-please/ Share on other sites More sharing options...
johnsmith153 Posted October 9, 2009 Share Posted October 9, 2009 add this code inside the function global $connection; Link to comment https://forums.phpfreaks.com/topic/177045-solved-php-mysql-query-problem-simple-solution-i-just-dont-get-it-help-please/#findComment-933464 Share on other sites More sharing options...
physaux Posted October 9, 2009 Author Share Posted October 9, 2009 add this code inside the function global $connection; Ok i did, and i get this error: Warning: mysql_query() expects parameter 2 to be resource, null given in /Users/.../Sites/test/includes/functions.php on line 87 Database query failed: **Line 87 is the line that says $result = ... Here i'm going to be more specific this time I have 1 file called test.php, which has the following: require_once("includes/functions.php"); require_once("includes/constants.php"); require_once("includes/connection.php"); Among the files in /includes, there is connection.php, which has the following: <?php $connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS); if (!$connection) { die("Database connection failed: " . mysql_error()); } // 2. Select a database to use $db_select = mysql_select_db(DB_NAME,$connection); if (!$db_select) { die("Database selection failed: " . mysql_error()); } ?> Ok now functions.php has the following (I am now trying a shorter function): function hasenamebeenregistered($name){ $result = mysql_query("SELECT * FROM 'users'", $connection); if (!$result) { die("Database query failed: " . mysql_error()); } while ($row = mysql_fetch_array($result)) { if($row["name"] == $name){ return true; } } return false; } //Here i am checking if the name is already in the database. Or atleast trying to.. And lastly, lower down in test.php again, i am calling the function as such: if(hasenamebeenregistered("example")) echo "YES"; else echo "NOOOOOO"; When i run it i get that same ERROR, pointing to the same spot. I think it is worth noting that my database doesn't have any entries yet, could that be the problem? Thanks! Link to comment https://forums.phpfreaks.com/topic/177045-solved-php-mysql-query-problem-simple-solution-i-just-dont-get-it-help-please/#findComment-933465 Share on other sites More sharing options...
Bendude14 Posted October 9, 2009 Share Posted October 9, 2009 you did add global $connection before that line right? Link to comment https://forums.phpfreaks.com/topic/177045-solved-php-mysql-query-problem-simple-solution-i-just-dont-get-it-help-please/#findComment-933467 Share on other sites More sharing options...
johnsmith153 Posted October 9, 2009 Share Posted October 9, 2009 Basically your function isn't 'allowed' to pick up the $connection variable (which is your mysql connection) - so setting global will allow this. It should work. Link to comment https://forums.phpfreaks.com/topic/177045-solved-php-mysql-query-problem-simple-solution-i-just-dont-get-it-help-please/#findComment-933468 Share on other sites More sharing options...
physaux Posted October 9, 2009 Author Share Posted October 9, 2009 I added much more information on my setup in my above comment, be editing it. Please read it to understand whats going on better, thanks. And could you explain what you mean by making it a global$ connection? Should that be in the methods.php or connection.php Thanks Link to comment https://forums.phpfreaks.com/topic/177045-solved-php-mysql-query-problem-simple-solution-i-just-dont-get-it-help-please/#findComment-933471 Share on other sites More sharing options...
whaleyb Posted October 9, 2009 Share Posted October 9, 2009 Here is your code: function returndataforname($name){ $result = mysql_query("SELECT * FROM 'users'", $connection); if (!$result) { die("Database query failed: " . mysql_error()); } while ($row = mysql_fetch_array($result)) { if($row["name"] == $name){ $returnvalue['language']=$row['language']; $returnvalue['name']=$row['name']; $returnvalue['password']=$row['password']; return $returnvalue; } } } They are telling you to add the $global like below function returndataforname($name){ global $connection; $result = mysql_query("SELECT * FROM 'users'", $connection); if (!$result) { die("Database query failed: " . mysql_error()); } while ($row = mysql_fetch_array($result)) { if($row["name"] == $name){ $returnvalue['language']=$row['language']; $returnvalue['name']=$row['name']; $returnvalue['password']=$row['password']; return $returnvalue; } } } This should correct you problem. Link to comment https://forums.phpfreaks.com/topic/177045-solved-php-mysql-query-problem-simple-solution-i-just-dont-get-it-help-please/#findComment-933476 Share on other sites More sharing options...
physaux Posted October 9, 2009 Author Share Posted October 9, 2009 Ah thank you WhaleyB and everyone, now i know where to put it! A) So i understand that it tells the function to look for the variable in the global scale? Is that right? B) Aanyways i ran it again, and i got a different Error. Things are getting better! Now it is telling me that: Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users'' at line 1 C) I don't have any actual values yet in the table, could that be a problem? If so could you tell me how to do that please.. I haven't got to that part of building my code yet, but if I need it for this to work, then could you tell me quickly how i would do that? Thanks!! Link to comment https://forums.phpfreaks.com/topic/177045-solved-php-mysql-query-problem-simple-solution-i-just-dont-get-it-help-please/#findComment-933483 Share on other sites More sharing options...
johnsmith153 Posted October 9, 2009 Share Posted October 9, 2009 $result = mysql_query("SELECT * FROM 'users'", $connection); The above is wrong. Change it to: $result = mysql_query("SELECT * FROM users", $connection); Link to comment https://forums.phpfreaks.com/topic/177045-solved-php-mysql-query-problem-simple-solution-i-just-dont-get-it-help-please/#findComment-933487 Share on other sites More sharing options...
physaux Posted October 9, 2009 Author Share Posted October 9, 2009 $result = mysql_query("SELECT * FROM 'users'", $connection); The above is wrong. Change it to: $result = mysql_query("SELECT * FROM users", $connection); Wahoo!! it works!! Thank you so much everyone, i have never been so happy to see firefox say "NOOO!!" lol Link to comment https://forums.phpfreaks.com/topic/177045-solved-php-mysql-query-problem-simple-solution-i-just-dont-get-it-help-please/#findComment-933489 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.