i have a web page having a search bar in which user will enter years in number and will get results displayed on the same page but the code is not working properly here is the core rectify the mistake <?php $con=mysql_connect('localhost','admin','admin'); //1 database connectivity //if(!$con) die('could not connected to database') mysql_select_db("dvd"); $sql='select * from dvd'; $result=mysql_query($sql); //3 retrive data from table where year is entered by user if(!$result) die('unable to fetch data'); else echo $result; ?>
Well, mysql_query() only sends a MySQL query and returns a resource on success (or false on error). You should retrieve your results with functions like mysql_fetch_assoc(), mysql_result(), mysql_fetch_row() etc to display them on your site.
Please I recieve the error message below from my host review:Fatal error: require_once() [function.require]: Failed opening required 'include/config.php' (include_path='.:/usr/local/lib/php-5.2.17/lib/php') in /hermes/web06/b781/moo.oludirectory/PhpLD/init.php on line 112How do I address this??
i didnt received fatal error but on line $result=mysql_query($sql); here .how to correct this error how can i make my code retrieve data from database when user searches for year ?
I haven't talked about fatal error, just "false on error". The guy above just posted a question in the first thread he bumped into. If you want to retrieve data with certain years, you should change your query. Something like this: $sql = "SELECT * FROM dvd WHERE year = '1999'"; // You can change the year to $_POST['year] or $_GET['year'] PHP: If you accept user submitted data, make sure to sanitize your inputs to prevent SQL injection. As I mentioned it in my first post mysql_query() only returns a resource on success, you should retrieve your results with functions like mysql_fetch_assoc(), mysql_result(), mysql_fetch_row(), mysql_fetch_array() etc to display them on your site.
i know sql and working on my sql.my query was i was getting error on a line in the code and want solutions if possible .no other talks please
What error message did you get?! It would be great if you could explain clearly what's your problem with your code... In your first code you try to echo a resource. You should fetch the results first. But it would be great to know what you want to echo out.
you can try <?php $con=mysql_connect('localhost','admin','admin'); //1 database connectivity //if(!$con) die('could not connected to database') mysql_select_db("dvd"); $sql='select * from dvd'; $result=mysql_query($sql); //3 retrive data from table where year is entered by user while ($row = mysql_fetch_array($result)) { print_r($row); } ?> PHP:
well there is not any issue in your query. but mysql_query() just return resource . You need to use mysql_fetch_object or mysql_fetch_assoc() to retrive result.. $sql="select * from dvd"; $rs =mysql_query($sql); while($row = mysql_fetch_assoc($rs)) { echo $row['columname]; }