0

Here is my code:

$campagin_id = $_SESSION['campagin_id_for_camp'];

$query = "SELECT * FROM survey_result where campagin_id = ".$campagin_id;

$conn=mysql_connect($dbconfig['db_hostname'],$dbconfig['db_username'],$dbconfig['db_password']) or die(mysql_error());

mysql_select_db($dbconfig['db_name'],$conn);

$exec_query =mysql_query($query) or die(mysql_error());

$row=mysql_fetch_array($exec_query);
echo "<br> row = ".$row;
while ($row=mysql_fetch_array($exec_query)){
    echo "I am In";

}

The Problem is that I am not getting anything in $row I cant get into the while loop, nothing shows up when I try to echo the value of $row, No error Nothing. Can you help me to find a problem in my code ?

Ps : The database is their. I have checked for the query for the corresponding value of $campagin_id. and also when i tried to echo $exec_query it echoed this : Resource id #8

PPS : The database have more than 7 record for each id so it doesn't matter if I call mysql_fetch_array($exec_query) more than once before going in to the while loop. and for the $campagin_id in the session their are many records present in the database.

8
  • $_SESSION['campagin_id_for_camp'] is a string or number? Commented Dec 10, 2012 at 7:22
  • @MuthuKumaran It is a Number Commented Dec 10, 2012 at 7:27
  • If you get I am in string then you have correctly executed your query. If you want to fetch the entire row that you get from you id: then try this while ($row=mysql_fetch_array($exec_query)){ echo $row['column_name_you_want']; Commented Dec 10, 2012 at 7:31
  • @TNC I dont go in to the while loop. That is the problem. Commented Dec 10, 2012 at 7:37
  • @HussainNagri - let me know what exactly you want to echo. So i can help you. And check my answer. Commented Dec 10, 2012 at 7:46

3 Answers 3

3

You have written $row=mysql_fetch_array($exec_query) and then you are echoing something. and you are using the same in while.

Instead of:

$row=mysql_fetch_array($exec_query);
echo "<br> row = ".$row;
while ($row=mysql_fetch_array($exec_query)){
    echo "I am In";
}

Use this (as per my knowledge you should not use $row=mysql_fetch_array() once you have used before while):

while ($row=mysql_fetch_array($exec_query)){
    echo "I am In";
}
Sign up to request clarification or add additional context in comments.

Comments

2

If the query returns Resource id #8 then that means it was successful - ie there were no errors. There were probably no rows returned by that query, so no rows in your table that match the given campagin_id.

You are also calling mysql_fetch_array() twice separately, you shouldn't do that because your while loop will skip the first row because calling this moves the pointer in the result set forward by one.

Also you can't echo an array as you are trying to, if you want to see the contents of an array use print_r() or var_dump().

I suggest adding some code to handle no rows found:

if($exec_query && mysql_num_rows($exec_query) > 0)
{
    while ($row=mysql_fetch_array($exec_query)){
        echo "Row: " . print_r($row, true);
    }
}
else
{
    echo 'None found';
}

4 Comments

No, I tried to run the code with an ID that exist but nothing. with your code in it it showed None found.
That means there's no rows that match the ID that you passed. You need to echo out the $query SQL to see what ID you are passing, then check it in a DB tool such as phpMyAdmin.
I did that already. The query has a id that DO EXIST in the database and the query works fine when ran in MySQL directly. Query is not the issue.
Verify that you are looking at the correct database and table.
-1

Try this code.

<?
$campagin_id = $_SESSION['campagin_id_for_camp'];
$query = "SELECT * FROM survey_result where campagin_id = ".$campagin_id;

mysql_connect($dbconfig['db_hostname'],$dbconfig['db_username'],$dbconfig['db_password']) or die(mysql_error());
mysql_select_db($dbconfig['db_name']);

$exec_query =mysql_query($query) or die(mysql_error());
while ($row=mysql_fetch_assoc($exec_query)) {
    echo "<br/> row = <pre>".print_r($row)."</pre><br/>";
}
?>

3 Comments

-1, you have not explained what you changed, why you changed it, how the changes work, and how this solves the problem stated in the OP.
Echoing it with some html tags wont make any difference. would it ?
It's not only echoing, but also rewritten that code to be more short. also removed some pieces of unrequired code. If this code doesn't work, then the only reason is query returns no result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.