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
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++;
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.