Jump to content

Database Connection Not Working


Rick_Browne

Recommended Posts

Hi all,

I hope you can help me

I use the following section of code to build a table of 50 by 50 squares- each square is populated with a name, its coordinates, and its type.

[code]

  $db = mysql_connect("*****", "*****", "*****");
  mysql_select_db("contacts",$db);


for($down=0;$down<50;$down++) {

    echo "<tr>";
    
        for($across=0;$across<50;$across++) {

        $id = "$down,$across";

        $result = mysql_query("SELECT * FROM coordinates WHERE id=$id",$db);
        $myrow = mysql_fetch_array($result);

        $name = $myrow["name"];
        $type = $myrow["type"];

        ?><td Width="100" Height="100">
            <table border="0" align="center">
                <tr><td><center><?php printf("%s",$myrow["name"]) ?></center></td></tr>
                <tr><td nowrap><center><a href="?id=<?php echo $id?>">( <?php echo $id ?> )</a></center></td></tr>
                <tr><td><center><?php echo $type?></center></td></tr>
            </table>
        </td><?php

        }

    echo "</tr>";
    
}

?>[/code]

The table builds itself fine and enters in all the correct coordinates- but the name and type fields remain empty.

As you can see I have tried a different method for both to try and find a solution but my php is a bit rusty these days :(

Im sure its something simple ive missed.....
Link to comment
Share on other sites

yo rick, i think it might be as simple as this....

is your select_db tag wrong? heres what i use:

[code]mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");[/code]

[!--quoteo(post=380233:date=Jun 5 2006, 09:25 AM:name=Rick_Browne)--][div class=\'quotetop\']QUOTE(Rick_Browne @ Jun 5 2006, 09:25 AM) [snapback]380233[/snapback][/div][div class=\'quotemain\'][!--quotec--]

[code]

  $db = mysql_connect("*****", "*****", "*****");
  mysql_select_db("contacts",$db);

[/code]

The table builds itself fine and enters in all the correct coordinates- but the name and type fields remain empty.

As you can see I have tried a different method for both to try and find a solution but my php is a bit rusty these days :(

Im sure its something simple ive missed.....
[/quote]
Link to comment
Share on other sites

okay you have two loops $down and $across, which are numerical values. So you assign them in $id as

$id='$down, $across';

this makes $id a string type variable. so unless your id column is a string type, and it holds information as

"12, 14"
"1,34"
etc..

then it is not going to select anything from the database. if you are trying to build a grid that has names/types in each square, based on your code up there, you have your db table setup wrong.

you are going to have to setup several tables and link them (look into relational databases). also, I would suggest that you pull out all the information you need at one time, and then build the html table, instead of querying the database 2,500 times...
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.