Forum Moderators: coopster

Message Too Old, No Replies

Newbie q's re connecting and error problems

Newbie q's re connecting and error problems

         

hugotobi

5:23 am on Aug 19, 2004 (gmt 0)

10+ Year Member



Here is my script:

<?php

$dbh=mysql_connect("localhost", "my_username", "my_password") or die ('No can do!');
mysql_select_db ("my_databasename");
$query = "SELECT * FROM contacts";
while($i = mysql_fetch_array($query)){
echo $i[first];
echo $i[last];
echo $i[email];
}
?>

Get this error message:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home2/my_username/my_domain/my_databasename/test.php on line 6

what am I doing wrong?

Birdman

6:51 am on Aug 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld.com!

You missed one crucial function in your script...mysql_query() [php.net].

You will need to add that function just before the while() loop.

$query = "SELECT * FROM contacts";
$result = mysql_query($query);
while($i = mysql_fetch_array($result)){

hugotobi

3:21 am on Aug 24, 2004 (gmt 0)

10+ Year Member



thanks Birdman

I fixed the problem. My database output is working just great.

However, I cant get around one problem: there are about 2600 rows in my database - all get sent to the output as a continous table. How do I make the output say 30 rows and then a button/link to display the next 30 and so on...

lazydog

12:03 pm on Aug 24, 2004 (gmt 0)

10+ Year Member



If you want to output 30 rows at a time, then

on page 1 your query will be - SELECT * FROM contacts limit 0,30
on page 2 your query will be - SELECT * FROM contacts limit 30,30
on page 3 your query will be - SELECT * FROM contacts limit 60,30
...
.. and so on :)

Hope that helps.

Saurabh.

hugotobi

3:04 pm on Aug 24, 2004 (gmt 0)

10+ Year Member



ok. so how do i place a link on "page 1" for the next page and on page 2 for the next and previous page? Also what code is required to calculate how many total pages in the output?
Thanks!

hugotobi

3:04 pm on Aug 24, 2004 (gmt 0)

10+ Year Member



ok. so how do i place a link on "page 1" for the next page and on page 2 for the next and previous page? Also what code is required to calculate how many total pages in the output?
Thanks!

Birdman

3:12 pm on Aug 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here ya go: Pagination [webmasterworld.com]

hugotobi

3:27 pm on Aug 24, 2004 (gmt 0)

10+ Year Member



Birdman
thanks. being a new"bee" to PHP I dont understand where about in my script that pagination script part should be included... arrgh!

should I post my script so you can tell me where to insert this pagination script?

lazydog

8:17 pm on Aug 24, 2004 (gmt 0)

10+ Year Member



Hi hugotobi!

To calculate the no. of pages just divide the total number of rows in the table by 30. If you get returned a fraction, "round-up" i.e. if you get 50.12, no. of pages become 51.

previous_page = current_page - 1 (unless current_page is one)
next_page = current_page + 1 (unless current_page is last page)

Just some pointers ;-)

Saurabh.