|
Posted by hackajar on 12/25/06 10:02
This works too:
$random=rand(1,5);
$query="SELECT Message FROM <database>.random_quote WHERE
MessageID=$random"; //Don't forget to include your database!
$message = mysql_result(mysql_query($query),0) or die(mysql_error());
Hackajar
Kim André Akerø wrote:
> Kimmo Laine wrote:
>
> > "Geoff Berrow" <blthecat@ckdog.co.uk> wrote in message
> > news:0g9mo2597of364e5tqafja4e48gr1uvg5s@4ax.com... >Message-ID:
> > <1166741903.577897.67580@80g2000cwy.googlegroups.com> from
> > > Jerim79 contained the following:
> > >
> > > > $random=rand(1,5);
> > > > $query="SELECT Message FROM random_quote WHERE MessageID=$random";
> > > > $message = mysql_query($query) or die(mysql_error());
> > > > echo $message;
> > > >
> > > > But it only returns "Resource #5." I had someone mention
> > > > mysql_fetch_array, but that seems like an unecessary way to go
> > > > about it. To me, logically the code above should work.
> > >
> > > No, it returns a resource, as you have found out. The output you
> > > require may just be one thing, but a query can return many rows and
> > > columns of data. It needs further processing before you can do
> > > anything useful with it. You might only be interested in the
> > > number of rows where you could use mysql_num_rows()
> > >
> > > But in your case, since you know that MessageID is unique you know
> > > it will only return one row.
> > >
> > > $random=rand(1,5);
> > > $query="SELECT Message FROM random_quote WHERE MessageID=$random";
> > > $result = mysql_query($query) or die(mysql_error());
> > > $row= mysql_fetch_array($result);
> > > echo $row['Message'];
> >
> > Better use mysql_fetch_assoc or pass the parameter MYSQL_ASSOC than
> > use mysql_fetch_array as is, there's no need for the indexed elements.
>
> Why go through all the hoops of using arrays when you only need the one
> element?
>
> $random=rand(1,5);
> $query="SELECT Message FROM random_quote WHERE MessageID=$random";
> $result = mysql_query($query) or die(mysql_error());
> echo mysql_result($result, 0);
>
> --
> Kim André Akerø
> - kimandre@NOSPAMbetadome.com
> (remove NOSPAM to contact me directly)
Navigation:
[Reply to this message]
|