Mutley Posted September 30, 2006 Share Posted September 30, 2006 I did this:[code]?user=1[/code]...and it works but if I change my database entry to a word like "test" instead of "1", it doesn't work and just displays this:[code]Unknown column 'test' in 'where clause'[/code]Any ideas why? Quote Link to comment https://forums.phpfreaks.com/topic/22614-url-variables-only-work-as-numbers/ Share on other sites More sharing options...
extrovertive Posted September 30, 2006 Share Posted September 30, 2006 Post your query. Quote Link to comment https://forums.phpfreaks.com/topic/22614-url-variables-only-work-as-numbers/#findComment-101569 Share on other sites More sharing options...
Mutley Posted September 30, 2006 Author Share Posted September 30, 2006 [code]$userid = $_GET['user'];if($userid) { $sql = "SELECT * "; $sql .= "FROM style "; $sql .= "WHERE user_id=".$userid." "; $result = mysql_query($sql) or die (mysql_error()); if(mysql_num_rows($result) == 1) { $row = mysql_fetch_array($result);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22614-url-variables-only-work-as-numbers/#findComment-101575 Share on other sites More sharing options...
extrovertive Posted September 30, 2006 Share Posted September 30, 2006 [code=php:0]$userid = $_GET['user'];if($userid) { $sql = "SELECT * "; $sql .= "FROM style "; $sql .= "WHERE user_id = '$userid' "; $result = mysql_query($sql) or die (mysql_error()); if(mysql_num_rows($result) == 1) { $row = mysql_fetch_array($result); }[/code]What happens now? Quote Link to comment https://forums.phpfreaks.com/topic/22614-url-variables-only-work-as-numbers/#findComment-101579 Share on other sites More sharing options...
JasonLewis Posted October 1, 2006 Share Posted October 1, 2006 if i were u i would re-write this as this.[code]$userid = $_GET['user'];if($userid) {$sql = "SELECT * FROM `style` WHERE `user_id`='".$userid."'"; //Put all this in one line and add the ` and the ' $result = mysql_query($sql) or die (mysql_error()); if(mysql_num_rows($result) == 1) { $row = mysql_fetch_array($result);[/code]try it. c if it works Quote Link to comment https://forums.phpfreaks.com/topic/22614-url-variables-only-work-as-numbers/#findComment-101732 Share on other sites More sharing options...
Daniel0 Posted October 1, 2006 Share Posted October 1, 2006 Even better: [code]if(!empty($_GET['user'])){ $result = mysql_query("SELECT * FROM style WHERE user_id='{$_GET['user']}'") or die (mysql_error()); if(mysql_num_rows($result) == 1) { $row = mysql_fetch_array($result);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22614-url-variables-only-work-as-numbers/#findComment-101737 Share on other sites More sharing options...
JasonLewis Posted October 1, 2006 Share Posted October 1, 2006 yes but some ppl dont like putting the query straight into mysql_query. i used to do it. ur way is just neater, well its my way as well. but i use OOP so mine would be $DB->query Quote Link to comment https://forums.phpfreaks.com/topic/22614-url-variables-only-work-as-numbers/#findComment-101749 Share on other sites More sharing options...
Daniel0 Posted October 1, 2006 Share Posted October 1, 2006 I don't see the point in first making a variable with the query and then pass the variable to the query function except if you are going to use the query variable more times (like a debug page or something). Quote Link to comment https://forums.phpfreaks.com/topic/22614-url-variables-only-work-as-numbers/#findComment-101753 Share on other sites More sharing options...
extrovertive Posted October 1, 2006 Share Posted October 1, 2006 Even better II.[code=php:0]if(preg_match("/^[0-9]+$/", $_GET['user'])){$user_id = $_GET['user']; $sql = "SELECT * FROM style WHERE user_id='{$user_id}'"; $result = mysql_query($sql) or die (mysql_error()); if(mysql_num_rows($result)) { $row = mysql_fetch_array($result);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22614-url-variables-only-work-as-numbers/#findComment-101757 Share on other sites More sharing options...
Daniel0 Posted October 1, 2006 Share Posted October 1, 2006 Instead of [code]if(preg_match("/^[0-9]+$/", $_GET['user']))[/code] you could just use [code]if(is_numeric($_GET['user']))[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22614-url-variables-only-work-as-numbers/#findComment-101759 Share on other sites More sharing options...
extrovertive Posted October 1, 2006 Share Posted October 1, 2006 Ok, that's even better III. Quote Link to comment https://forums.phpfreaks.com/topic/22614-url-variables-only-work-as-numbers/#findComment-101760 Share on other sites More sharing options...
Mutley Posted October 1, 2006 Author Share Posted October 1, 2006 Why is it better? I'm not ace at PHP so can't just see how it is better. THanks alot though. :) Quote Link to comment https://forums.phpfreaks.com/topic/22614-url-variables-only-work-as-numbers/#findComment-101764 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.