Jump to content

Using loops and dynamic arrays


dink87522

Recommended Posts

$query = "SELECT * FROM Companies WHERE owner = '$userName'";
$result = mysql_query($query) or die("Error: " . mysql_error());
$count = mysql_num_rows($result);
echo("Count: $count<p>");

$result = mysql_query("SELECT name FROM Companies WHERE owner = '$userName'") or die("Error:  " . mysql_error());
$result = mysql_fetch_row($result);

for ($i=0; $i<=$count; $i++){ // $i<=$count means do while $i is less than or equal to $count
	/$companyName[$i] = $result[$i]; 
	echo("$i: $companyName[$i]<br />");
	echo("$i: $a<br />");
}

 

I am trying to check the number of entries of company names the user has in the table Companies. I am then trying to use a for loop to loop through and store each of those names into an array. It doesn't work and I don't know why, can anyone see my problem?

Link to comment
https://forums.phpfreaks.com/topic/220037-using-loops-and-dynamic-arrays/
Share on other sites

Yes, I can see a few error..

 

why not:

  $query = "SELECT * FROM Companies WHERE owner = '$userName'";
    $result = mysql_query($query) or die("Error: " . mysql_error());
    $count = mysql_num_rows($result);
    echo"<p>Count: $count</p>";
    $i=1;
while($data=mysql_fetch_array($result)){
        echo $i.":".$data['name']."<br />\n";
        $i++;
    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.