hi can anybody help me i need to get the data from db through php script my code is :
<?php
$con=mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("poll",$con);
echo "DB SELECTED<br>";
$result=mysql_query("select question from pollquestions",$con);
echo "QUERY EXECUTED<br>";
echo $result ;
echo "<br>DATA DISPLAYED<br>";
mysql_close($con);
?>
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[0]; 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[0];, B will be $result[1]; 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[0]; however you cannot access the value using $result if you have retrieved the result using mysql_fetch_row.

This article has been dead for over six months. Start a new discussion instead.