hi can anybody help me i need to get the data from db through php script my code is :
die('Could not connect: ' . mysql_error());
echo "DB SELECTED<br>";
$result=mysql_query("select question from pollquestions",$con);
echo "QUERY EXECUTED<br>";
echo $result ;
echo "<br>DATA DISPLAYED<br>";
but its returning me only resource id#3 why i could not get the actual data from the database
because you are not specifying the password for root!
e.g in my site
$host = 'localhost'; $user = 'root'; $pass = 'letmein'; $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect!'); mysql_select_db('databasename') or die ('Unable to select database.'); $query = "SELECT * FROM customers" $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
You don't get data, as you don't retrieve it. Now, executing a query does not fetch the result itself.
after you submit your query using mysql_query, you have to get your results in some way using either mysql_fetch_row or mysql_fetch_array.
In your case, if you execute $finalresult = mysql_fetch_row($result); echo $finalresult; will show what you need.
Note that both functions bring only one row from the result, not all rows. The difference is that fetch_row creates an number-indexed array, while fetch_array creates an associative array where indexes are the column names from the database.
e.g. - you have a table with 4 columns named A, B, C, D
using $result = mysql_fetch_row will create an array, where the value of column A will be accessible via $result;, B will be $result; etc...
Useing $result=mysql_fetch_array will create an array, where you will be able to retrieve value of A using $result;, B will be $result, etc.
Note that using fetch_array allows to access the values in the array using $result; however you cannot access the value using $result if you have retrieved the result using mysql_fetch_row.