Re: Not Quite: how to build an array
| From: | Philip Olson | Date: | Sat, 23 Feb 2002 18:53:19 +0000 |
| Subject: | Re: Not Quite: how to build an array | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-85952@lists.php.net to get a copy of this message | ||
Let's work on some debugging. By using mysql_error()
you'd eventually find out that AS is a reserved word
in mysql, see:
http://www.mysql.com/doc/R/e/Reserved_words.html
That said, here's some example code to play with,
untested but should work.
if (!$conn = mysql_connect($host, $user, $pass)) {
echo 'Cannot Connect: '. mysql_error();
exit;
}
if (!mysql_select_db($dbname)) {
echo 'Cannot Select Db: '. mysql_error();
exit;
}
$sql = "SELECT something FROM $tablename";
// AS is reserved as the following is a valid
// SQL query: SELECT foo AS bar FROM blah
// See: http://www.sqlcourse.com/
// http://www.w3schools.com/sql/
if (!$result = mysql_query($sql)) {
echo "Cannot run query ($sql): ". mysql_error();
exit;
}
while ($row = mysql_fetch_assoc($result)) {
$authors[] = explode(';', $row['something']);
}
print_r($authors);
Rename your field to something other then AS, like
perhaps "authors" or something ;) Also eventually
learn about "Database Normalization".
Regards,
Philip Olson
> OK. I use:
>
> while ($mydata = mysql_fetch_object($news))
> {
> $authors = explode(";", $mydata->AS);
> }
>
> Then why "Invalid argument supplied for foreach()"
> Am I indeed building an array as I go through my database?
>
> foreach($authors as $author)
> {
> #echo sort($author)".<br>\n";
> echo "$author<br>\n";
> }
>
> http://www.php.net/manual/en/control-structures.foreach.php
> foreach ($arr as $value) {
> echo "Value: $value<br>\n";
> }
>
> I have the right syntax? Did I build the array?
> Newbie, but have to learn it somehow :)
>
> ----------------snip---------------------
> $myconnection = mysql_connect($server,$user,$pass);
> mysql_select_db($db,$myconnection);
>
> $news = mysql_query("select AS from $table");
>
> while ($mydata = mysql_fetch_object($news))
> {
> $authors = explode(";", $mydata->AS);
> }
>
> foreach($authors as $author)
> {
> echo "$author<br>\n";
> }
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>