473,324 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

fetch_array()

I am trying to incorporate a random quote feature on my website. I have
a database table called random_quote with two columns. Column 1 is
MessageID and column 2 is Message. Message contains several sentences,
and MessageID contains a number. I have 5 entries in the table. Here is
the code I am using to query the database and write the quote to the
screen:

$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. It should pull the
message from the database and assign it to $message. And then write
$message to the screen. So what's wrong with it and how can I fix it?

Dec 21 '06 #1
7 5057
On 21 Dec 2006 14:58:23 -0800, "Jerim79" <my***@hotmail.comwrote:
>I am trying to incorporate a random quote feature on my website. I have
a database table called random_quote with two columns. Column 1 is
MessageID and column 2 is Message. Message contains several sentences,
and MessageID contains a number. I have 5 entries in the table. Here is
the code I am using to query the database and write the quote to the
screen:

$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,
Yes.
but that seems like an unecessary way to go about
it. To me, logically the code above should work. It should pull the
message from the database and assign it to $message. And then write
$message to the screen.
No.
>So what's wrong with it and how can I fix it?
You've posted your own answer.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Dec 21 '06 #2
Message-ID: <11*********************@80g2000cwy.googlegroups.c omfrom
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'];
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Dec 22 '06 #3
"Geoff Berrow" <bl******@ckdog.co.ukwrote in message
news:0g********************************@4ax.com...
Message-ID: <11*********************@80g2000cwy.googlegroups.c omfrom
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.

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Dec 22 '06 #4
Kimmo Laine wrote:
"Geoff Berrow" <bl******@ckdog.co.ukwrote in message
news:0g********************************@4ax.com... >Message-ID:
<11*********************@80g2000cwy.googlegroups.c omfrom
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ĝ
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Dec 23 '06 #5
Message-ID: <4v*************@mid.individual.netfrom Kim André Akerĝ
contained the following:
$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?
For the novice, the less functions you have to learn the easier it
becomes. My way, you get rather more than you need, but it works in
every instance.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Dec 23 '06 #6
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" <bl******@ckdog.co.ukwrote in message
news:0g********************************@4ax.com... >Message-ID:
<11*********************@80g2000cwy.googlegroups.c omfrom
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ĝ
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Dec 25 '06 #7
ha******@gmail.com wrote:
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());
Or even better:

$query = "SELECT Message FROM random_quote ORDER BY RAND() LIMIT 1";
$message = mysql_result(mysql_query($query),0) or die(mysql_error());

--
Kim André Akerĝ
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Dec 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tom D | last post by:
I'm rewriting a database interface that our company currently has. Currently it's using the Pear::DB interface, but we found that that was introducing a bit too much overhead. I'm rewriting the...
4
by: Gary | last post by:
Hi, I get this error " " when my web page run, what does it mean? Hope someone can help!!! Gary
26
by: Dodger | last post by:
Okay, background... yes, I am another of those evil, spurned, damnable Perl mongers, but I'm not trying to start a flamewar, I'm juust tryung to understand something... I can write a script in...
1
by: Akhenaten | last post by:
Any suggestion as to why my fetch_array is bad? Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/jkokko/public_html/ask/test.php on line 11...
21
by: Leena P | last post by:
i want to basically take some information for the product and let the user enter the the material required to make this product 1.first page test.php which takes product code and displays...
30
by: SSG001 | last post by:
Hi, I'm using scriptaculous autocomplete for my text box and it works perfectly ok i have made server.php for fetching the values in the ul list and it is shown correctly on the main form but after...
3
by: Andrey | last post by:
Hi just a quick question about using MySQL module... are there any api / class available to give a higher level in working with Mysql in python? such as db.fetch_array(), db.fetch_rows(),...
2
by: Iain Adams | last post by:
I have a db class that sets up a connection. It then has methods to query the db and fetch results etc that encapsulate the normal mysql functions (i.e. mysql_query($sql)). I seem to get this...
1
by: Frank Moyles | last post by:
I have two simple function in PHP that I want to convert to C# - function Doit($enc_user_name, $enc_password) { $result = $mysqli->query("call sp_userAuth('$enc_user_name','$enc_password')"); ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shĉllîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.