Forum Moderators: coopster

Message Too Old, No Replies

getting mysql data into an array

         

surrealillusions

5:47 pm on Sep 4, 2008 (gmt 0)

10+ Year Member



Hi all,

I need to know how to go about getting certain data (namely the first 3 entries) from a mysql database, and putting it into an array, to then echo around various parts of the page.

I found a few things on mysql_fetch_array and the like but cant seem to get them to work fully.

The current script (which i dont have access to here at home) is setup to fetch the latest mysql entry and echo it using variables, setup into an array itself, from the various columns in that database table row. Does that make sense?

So, What i need to do is get the first 3 entries seperate, into an array, so i can then control where they appear on the page.

Any help would be appreciated

:)

cameraman

6:01 pm on Sep 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is just one way of many to do this. How you select out three records is app dependent:
$qry = mysql_query("SELECT name, address, city FROM table");
for($i = 0; $i < 3; $i++)
$answer[$i] = mysql_fetch_array($qry);
?>
<p><div>The first winner is <?php echo $answer[0]['name']; ?></div>
<div>The winner lives in beautiful <?php echo $answer[0]['city']; ?></div></p>
<p>Third but not least, we have <?php echo $answer[2]['name']; ?> who lives in beautiful <?php echo $answer[2]['city']; ?></p>

surrealillusions

6:00 pm on Sep 5, 2008 (gmt 0)

10+ Year Member



Doesnt seem to work properly.

Its pulling the first entries out of the database and not the most recent. Although with this i added ORDER BY to the sql query to solve that problem.

Also, noticed its not pulling in the correct data. I change the number in the echo statement, and the stuff it displays either stays the same, or nothing at all, despite the stuff in the database been different.

This might be due to the way the backend backend framework is setup...will have another go with a basic page and database to see if it works...

surrealillusions

11:15 am on Sep 6, 2008 (gmt 0)

10+ Year Member



just tried it with a basic database and php script and get the following error...

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

With no data from the database echod onto the page...

:/

jatar_k

12:26 pm on Sep 6, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that error usually means there was an error from mysql

try this change

$qry = mysql_query("SELECT name, address, city FROM table") or die ("select died: " . mysql_error());

that should give you the error from mysql

surrealillusions

1:07 pm on Sep 6, 2008 (gmt 0)

10+ Year Member



Thanks

The error is You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table'

But i cant see any problems with it. Ive checked several times against php.net's mysql functions and that but i cant spot anything. Using the above code, this is what i have been using -

$qry = mysql_query('SELECT * FROM table')
or die ("select died: " . mysql_error());

for($i = 0; $i < 3; $i++)
$answer[$i] = mysql_fetch_array($qry);

These are the versions that i have -
Apache Version 2.2.8
PHP Version 5.2.6
MySQL Version 5.0.51b

jatar_k

1:26 pm on Sep 6, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what is the name of your table? if it is actually 'table' then you should change it as it's a reserved word

surrealillusions

2:56 pm on Sep 6, 2008 (gmt 0)

10+ Year Member



ahh right..it works now. Changed the name of the table from table to something else.

thanks

:)