webguync Posted December 20, 2010 Share Posted December 20, 2010 I need some help with this. A user fills out a form, one of the fields is a zip code field. I need to retrieve that value from MySQL store as a session var and set that value as a variable to use with a weather display API. The ID is being stored from the form page. Here is what I have so far, after the values are submitted into the DB. <?php session_start(); $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Profile WHERE id='{$_SESSION['id']}"); while ($row = mysql_fetch_assoc($result)) { $_SESSION['id'] = $row['id']; $_SESSION['zip'] = $row['zip']; } mysql_close($con); ?> and then for the weather API, I need to set the stored variable to something $zip = 'stored zip code value'; Quote Link to comment https://forums.phpfreaks.com/topic/222246-retrieving-value-from-mysql-dbthen-storing-as-a-session-var/ Share on other sites More sharing options...
Rifts Posted December 20, 2010 Share Posted December 20, 2010 well since you are setting $_SESSION['zip'] from row[zip] why not just use it $zip = $_SESSION['zip']; now use $zip freely Quote Link to comment https://forums.phpfreaks.com/topic/222246-retrieving-value-from-mysql-dbthen-storing-as-a-session-var/#findComment-1149661 Share on other sites More sharing options...
BlueSkyIS Posted December 20, 2010 Share Posted December 20, 2010 $_SESSION['zip'] = ''; $result = mysql_query("SELECT * FROM Profile WHERE id='{$_SESSION['id']}"); if (mysql_numrows($result) > 0) { $row = mysql_fetch_assoc($result); $_SESSION['zip'] = $row['zip']; } // Later.... $zip = $_SESSION['zip']; Quote Link to comment https://forums.phpfreaks.com/topic/222246-retrieving-value-from-mysql-dbthen-storing-as-a-session-var/#findComment-1149667 Share on other sites More sharing options...
webguync Posted December 21, 2010 Author Share Posted December 21, 2010 hmm, why would I be getting this warning? Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in/new_site/Weather/weather.php on line 35 which is this part $_SESSION['zip'] = ''; $result = mysql_query("SELECT * FROM Profile WHERE id='{$_SESSION['id']}"); if (mysql_numrows($result) > 0) { $row = mysql_fetch_assoc($result); $_SESSION['zip'] = $row['zip']; } Quote Link to comment https://forums.phpfreaks.com/topic/222246-retrieving-value-from-mysql-dbthen-storing-as-a-session-var/#findComment-1149752 Share on other sites More sharing options...
suma237 Posted December 21, 2010 Share Posted December 21, 2010 int mysql_num_rows ( resource $result ); check the syntax Quote Link to comment https://forums.phpfreaks.com/topic/222246-retrieving-value-from-mysql-dbthen-storing-as-a-session-var/#findComment-1149875 Share on other sites More sharing options...
webguync Posted December 21, 2010 Author Share Posted December 21, 2010 sorry not sure I follow. I think the syntax looks ok. Quote Link to comment https://forums.phpfreaks.com/topic/222246-retrieving-value-from-mysql-dbthen-storing-as-a-session-var/#findComment-1149905 Share on other sites More sharing options...
trq Posted December 21, 2010 Share Posted December 21, 2010 You haven't checked that $result is a resource. If your query fails (which it must be) $result will be the boolean false, not a resource which is what mysql_num_rows expects. Always check variables are what you expect them to be before using them. Quote Link to comment https://forums.phpfreaks.com/topic/222246-retrieving-value-from-mysql-dbthen-storing-as-a-session-var/#findComment-1149906 Share on other sites More sharing options...
webguync Posted December 21, 2010 Author Share Posted December 21, 2010 so basically no results were returned so I get the warning, correct? Quote Link to comment https://forums.phpfreaks.com/topic/222246-retrieving-value-from-mysql-dbthen-storing-as-a-session-var/#findComment-1149909 Share on other sites More sharing options...
PFMaBiSmAd Posted December 21, 2010 Share Posted December 21, 2010 No your query is failing due to an error. A query that executes successfully but matches zero rows is a successful query and does not produce errors when you access the result set (unless you use mysql_result(), which has the misfortune of producing an error when there are zero rows in the result set.) Quote Link to comment https://forums.phpfreaks.com/topic/222246-retrieving-value-from-mysql-dbthen-storing-as-a-session-var/#findComment-1149911 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.