954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

mysql_fetch_array Not working at all

Hi,
I have this strange problem, I select some data from the database, there is no problem in the query and even when I use mysql_num_rows on the query it gives me the right number of entries in the table I'm selecting from, but when I try to echo every row it echos empty values... what is the problem??

$sql = "SELECT * FROM table";
		$query = mysql_query($sql) or die(mysql_error());
		$row_number = mysql_num_rows($query);
		while ($row = mysql_fetch_array($query))
		{
			echo "row= ".$row["id"];
                         }
theighost
Junior Poster
103 posts since Jan 2009
Reputation Points: 8
Solved Threads: 15
 

is it Giving any Erro?
what if u try this

while ($row = mysql_fetch_array($query))
{
$id=$row['id'];
echo "row=  $id ";

}
mrcniceguy
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 17
Solved Threads: 8
 

it is the same result, it writes "row=" as if the value is empty!

theighost
Junior Poster
103 posts since Jan 2009
Reputation Points: 8
Solved Threads: 15
 

what is ur table structure,i mean the fields?

mrcniceguy
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 17
Solved Threads: 8
 

theighost,
Post complete source code.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 
mysql_connect('server','user','pass');
        	mysql_select_db('database');
$sql = "select * from table";
        	$query = mysql_query($sql) or die(mysql_error());
        	while($row = mysql_fetch_array($query))
        	{
        		$search_row = $row['id'];
                        echo $search_row;
             }
theighost
Junior Poster
103 posts since Jan 2009
Reputation Points: 8
Solved Threads: 15
 

Try changing "$row['id']" to "$row[0]" and see if you still get nothing.

Menster
Junior Poster
175 posts since Jun 2009
Reputation Points: 49
Solved Threads: 22
 

I solved it in a really strange way guys.. wtf is this version of PHP that i'm using, that obliges me to declare the variables before using.

I just put $row=0; $search_row=0; before making the query and it worked, really strange!!!!

thanks anyway :)

theighost
Junior Poster
103 posts since Jan 2009
Reputation Points: 8
Solved Threads: 15
 

Thats good you solved it. But this $row is an array. So making it 0 is a bit strange. What other thing you can do is, you can declare it as an empty array:

$row = array();

$row = mysql_fetch_array();
// Proceed with rest of the code
sikka_varun
Junior Poster in Training
94 posts since Dec 2008
Reputation Points: 11
Solved Threads: 12
 

Yes I used some arrays in my script and had to declare some of the variable as $variable = array();

theighost
Junior Poster
103 posts since Jan 2009
Reputation Points: 8
Solved Threads: 15
 

Dear Guys,
I am getting this error on ther server side.
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL

Following Codes are working fine on localhost:-

$result = mysql_query("SELECT * FROM $txt2 WHERE $txt3 LIKE '%$txt%'");
 
echo "<table border='2' bordercolor='black' cellspacing='2' cellpadding='2'>
<tr>

<TD bgcolor='pink' align='center' width='10'>ID</TD>
<TD bgcolor='pink' align='center' width='10'>SUBJECT</TD>
<TD bgcolor='pink' align='left' width='10'>AUTHOR</TD>
<TD bgcolor='pink' align='left' width='10'>TITLE</TD>
<TD bgcolor='pink' align='left' width='10'>PUBLISHER</TD>
<TD bgcolor='pink' align='left' width='10'>ACCNO</TD>
<TD bgcolor='pink' align='left' width='10'>KEYWORD</TD>

</tr>";



while($row = mysql_fetch_array($result,MYSQL_BOTH))
   {
  echo "<tr>";
  echo "<td>" . $row['ID'] . "</td>";
  echo "<td>" . $row['SUBJECT'] . "</td>";
  echo "<td>" . $row['AUTHOR'] . "</td>";
  echo "<td>" . $row['TITLE'] . "</td>";
  echo "<td>" . $row['PUBLISHER'] . "</td>";
  echo "<td>" . $row['ACCNO'] . "</td>";
  echo "<td>" . $row['KEYWORD'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);

Jay G

Jay G
Newbie Poster
1 post since May 2011
Reputation Points: 10
Solved Threads: 0
 

See the sticky at the top of this forum on how to determine the error in your query. Next time please post a new thread, instead of reviving an old one.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You