0

I'll try to explain this as best as I can. I have a table with 100 values, two fields: ID and Name. For the purposes of this example, ID isn't important.

What I would like to do is select all records and display them in five columns but I need them to be in alphabetical order from top to down:

Example: Table Data has the following:

AAAA
AAAB
AAAC
AAAD
AAAE
AAAF

I need to display the data like the following:

AAAA     AAAD
AAAB     AAAE
AAAC     AAAF

Does that make sense?

If that isn't possible then I'll take it: AAAA AAAB AAAC AAAD AAAE AAAF

Either way, whichever is easiest.

Ok, Here is the error I'm getting now:

[Mon Dec 02 22:55:19 2013] [error] [client xx.xx.xx.xx] PHP Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in /usr/home/user/www/data/test/testing.php on line 35
[Mon Dec 02 22:55:19 2013] [error] [client xx.xx.xx.xx] PHP Notice: Undefined variable: result in /usr/home/user/www/data/test/testing.php on line 42

Here is the relevant code:

<?php
    $username = "user";
    $password = "password";
    $hostname = "localhost";

    //connection to the database
    $dbhandle = mysql_connect($hostname, $username, $password)
      or die("Unable to connect to MySQL");

    $selected = mysql_select_db("equine",$dbhandle)
      or die("Could not select Database");

    $query = 'select * from breeds';

    while ($row = mysql_fetch_assoc($query)) {
        $result[] = $row;
    }

    ?>
    <table><tr><td>
    <?php
    for ($i = 0; $i < count($result); $i++) {
        if ($i % (count($result) / 5) == 0) {
            echo "</td><td>";
        }
        echo "$result <br />";
    }
?>
</td></tr></table>

Ok here is the error now, and I made sure I cut and pasted what you had directly....

[Tue Dec 03 11:43:12 2013] [error] [client xx.xx.xx.xx] PHP Parse error: syntax error, unexpected '[',
expecting ']' in /usr/home/user/www/data/test/testing.php on line 45

Well I got a little further, this is the output:

Array[name]Array[name]Array[name]Array[name]Array[name]Array[name]Array[name]

Ok I got it figured out, I'll post my code in a few minutes, I need to clean it up some.

Well I thought I had it figured out. Its printing but its not limiting itself to 5 data elements per row.

Here is the code:

<?php
    $username = "username";
    $password = "password";
    $hostname = "localhost";

    //connection to the database
    $link = mysqli_connect($hostname, $username, $password,"equine")
     or die("Error " . mysqli_error($link));

    // connect to database, etc ...
    $query = "SELECT name FROM breeds" or die("Error in the consult..." . mysqli_error($link));

    $result = $link->query($query);

    while($row = mysqli_fetch_array($result)) {
             // echo $row["name"] . "<br>";
       $myResult[] = $row;
    }

    ?>
    <table border="1"><tr><td>
    <?php
    for ($i = 0; $i < count($myResult); $i++) {
       if ($i % (count($myResult) / 5) == 0) {
          echo "</td><td>";
       }
       echo $myResult[$i]['name']. "<br />";
    }
    ?>
</td></tr></table>

However the output looks like this:

<table border="1"><tr><td>
</td><td>Brittinee<br /></td><td>Pit Bull<br /></td><td>Black Lab<br /></td><td>Mutt<br /></td><td>Great Dane<br /></td><td>Doberman<br /></td><td>Shepard<br /></td></tr></table>

Except the data elements are in a straight row as it should be, I know it doesn't display right here.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • is it working? if it is, can i get my answer accepted? – will Dec 04 '13 at 18:55
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – John Conde Dec 19 '13 at 01:47

2 Answers2

0

Your second option is the easiest.

To achieve your secondary option here is the SQL Fiddle that demonstrates the below query:

SELECT GROUP_CONCAT(m.Name ORDER BY m.Name SEPARATOR ' ')  AS NameConcat
FROM mytable as m
Linger
  • 14,942
  • 23
  • 52
  • 79
0

I'm not entirely sure what you're asking, but if you retrieve a query set from the database, then you can display them in columns like

<?php
// connect to database, etc ...
$query = mysql_query('select * from `breeds`');
while ($row = mysql_fetch_assoc($query)) {
    $result[] = $row;
}
?>
<table><tr><td>
<?php
$divider = count($result) / 5;
for ($i = 0; $i < count($result); $i++) {
    if ($i > $start) {
        echo "</td><td>";
        $divider += ceil(count($result) / 5);
    }
    echo "$result[$i]['name'] <br />";
}
?>
</td></tr></table>

when you're querying the data like

select * from `table` order by `name` asc

It should be noted that mysql_query is deprecated, as is mysql_connect and mysql_select_db. These functions are not safe and will lead to a SQL injection attack.

will
  • 1,491
  • 1
  • 19
  • 28
  • Ok this is irritating, I can't format my comment, but I basically included what you had but now I'm getting an error: [Mon Dec 02 22:01:03 2013] [error] [client xx.xx.xx.xx] PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_W HITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /usr/home/user/www/data/test/testing.php on line 44 – user3059786 Dec 03 '13 at 03:57
  • That's what I figured, but this isn't letting me format the code right '?php for ($i = 0; $i < count($result); $i++) { if ($i % (count($result) / 5) == 0) { echo ""; } echo $results['name']
    "; } // Close the connection mysql_close($dbhandle); ?> '
    – user3059786 Dec 03 '13 at 04:07
  • you're missing a " but i've changed the code. also, just add future information about the project to the question. – will Dec 03 '13 at 04:12