royarellano Posted April 28, 2010 Share Posted April 28, 2010 Greetings All!I've created a PHP webpage. In it I have a basic PHP code to connect to a localhost MySQL database and retrieve a desired string. When I view the webpage, instead of the string, I get "Resource Id #4" in it's place. Please see the code below that I'm using, and if you have any ideas as to why it won't work please let me know.Environment: Apache HTTP Server 2.2.11PHP v5.3.0MySQL DBWindows XP Pro box <?php$con = mysql_connect("localhost:3306","admin****","p******");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("t6*****", $con);$sql = "SELECT title FROM wm_minister_msg where sysdate() >= pubdate and sysdate() < expiredate";$min_msg_title = mysql_query($sql);echo $min_msg_title;mysql_close($con);?> Link to comment Share on other sites More sharing options...
justsomeguy Posted April 28, 2010 Share Posted April 28, 2010 mysql_query returns a result resource, not a field value. Whenever you use a select query it always sends a result resource, regardless of whether you selected a single value from a single row or multiple values from multiple rows, or even if it returned no rows at all. You can use the fetch functions to get rows from the result, like mysql_fetch_array, mysql_fetch_assoc, or mysql_fetch_object. Link to comment Share on other sites More sharing options...
royarellano Posted April 29, 2010 Author Share Posted April 29, 2010 mysql_query returns a result resource, not a field value. Whenever you use a select query it always sends a result resource, regardless of whether you selected a single value from a single row or multiple values from multiple rows, or even if it returned no rows at all. You can use the fetch functions to get rows from the result, like mysql_fetch_array, mysql_fetch_assoc, or mysql_fetch_object.Thanks for the response. I tried using those three other functions and they did not work at all. Instead of returning the Resource string it returned nothing at all. Can you show me using my code as an example of what is the proper way to do return data in PHP using SQL?v/rRoy Link to comment Share on other sites More sharing options...
royarellano Posted April 29, 2010 Author Share Posted April 29, 2010 Ok, I updated my code to include the first reply suggestion (see code). For function _array and _assoc, the return is Array. When I add the function _object nothing shows (total blank page). I'm not sure what else I can be doing wrong. <?php$con = mysql_connect("localhost:3306","a******","p******");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("t*****", $con);$result = mysql_query("SELECT title FROM wm_minister_msg where sysdate() >= pubdate and sysdate() < expiredate");$row = mysql_fetch_assoc($result);echo $row;mysql_close($con);?> Link to comment Share on other sites More sharing options...
justsomeguy Posted April 29, 2010 Share Posted April 29, 2010 This page has some examples:http://www.w3schools.com/php/php_mysql_select.aspYou can also look up any PHP function in the manual on php.net, all of the details are there:http://www.php.net/manual/en/function.mysql-fetch-assoc.php Link to comment Share on other sites More sharing options...
royarellano Posted April 29, 2010 Author Share Posted April 29, 2010 Oh Wait!!!!I get it! My bad everyone, I finally see what I was doing wrong, well what I was not understanding. It works now; thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.