Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,674 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,210 people online right now. Registration is fast and FREE... Join Now!




foreach Mysql query

2 Pages V  1 2 >  
Reply to this topicStart new topic

foreach Mysql query

duffsstuff
post 29 Jun, 2007 - 03:48 PM
Post #1


D.I.C Head

**
Joined: 10 Sep, 2006
Posts: 67


My Contributions


My problem is inside the foreach function. I am not getting any errors but the data is not being pulled from the database. I am using Pear db.

The table the info is on is "top_menu" and there is three columns on the table:

ID
NAME
URL

CODE

require 'add-ons/DB/DB.php';
$db=DB::connect('mysql://user:password@localhost/database');
        
print "<ul>";
$links = $db->getALL('SELECT * FROM top_menu');
foreach ( $links as $links )
     {
         print "<li><a href=\"";
        echo "$top_menu[url]";
        print "\"><span>";
        echo "$top_menu[name]";
        print "</span></a></li>";
     }
print "</ul>";


You can see the output at My Webpage
User is offlineProfile CardPM

Go to the top of the page

girasquid
post 29 Jun, 2007 - 05:25 PM
Post #2


Barbarbar

Group Icon
Joined: 3 Oct, 2006
Posts: 1,256



Thanked 14 times

Dream Kudos: 650
My Contributions


If there are no errors but no data is being pulled from the database, could it be that your database query isn't returning anything?

Or if it is(as it seems to be on the page you linked), could you be referring to it wrong?

This post has been edited by girasquid: 29 Jun, 2007 - 05:26 PM
User is offlineProfile CardPM

Go to the top of the page

Styx
post 29 Jun, 2007 - 05:59 PM
Post #3


D.I.C Head

Group Icon
Joined: 4 Mar, 2007
Posts: 192



Dream Kudos: 225
My Contributions


You are calling the data wrong according to what you set in the foreach condition.

If you say foreach ($links as $link), where does $top_menu come from? Unless it has nothing to do with the query you just did, replace $top_menu with $link

Also, you need to put single quotations in the value in the bracket, which is just good coding practice.
User is offlineProfile CardPM

Go to the top of the page

duffsstuff
post 29 Jun, 2007 - 07:07 PM
Post #4


D.I.C Head

**
Joined: 10 Sep, 2006
Posts: 67


My Contributions


So I updated my code to this:

CODE

require 'add-ons/DB/DB.php';
$db = DB::connect('mysql://user:password@localhost/database');
    
print "<ul>";
$links = $db->getALL('SELECT * FROM top_menu');
foreach ( $links as $links )
     {
    print "<li><a href=\"";
    echo "$links['url']";
    print "\"><span>";
    echo "$links['name']";
    print "</span></a></li>";
     }
print "</ul>";


If you go to the link you will see i am now getting an error.

My Webpage

The file path is where the code is present. I have isolated the code so I can figure out this problem. The code is within a function. And this is to be used as page navigation on my CMS
User is offlineProfile CardPM

Go to the top of the page

Styx
post 29 Jun, 2007 - 07:09 PM
Post #5


D.I.C Head

Group Icon
Joined: 4 Mar, 2007
Posts: 192



Dream Kudos: 225
My Contributions


Try changing ($links as $links) to ($links as $link)
Read up on how to use the foreach construct

Also, you don't need quotes around your echo'd variables in the loop.

This post has been edited by Styx: 29 Jun, 2007 - 07:14 PM
User is offlineProfile CardPM

Go to the top of the page

duffsstuff
post 29 Jun, 2007 - 07:15 PM
Post #6


D.I.C Head

**
Joined: 10 Sep, 2006
Posts: 67


My Contributions


Did what you said and got no change:

here is my current code:

CODE

require 'add-ons/DB/DB.php';
$db = DB::connect('mysql://user:password@localhost/database');
        
print "<ul>";
$top_menu = $db->getALL('SELECT * FROM top_menu');
foreach ( $top_menu as $links )
    {
    print "<li><a href=\"";
    echo "$links['url']";
    print "\"><span>";
    echo "$links['name']";
    print "</span></a></li>";
     }
print "</ul>";
User is offlineProfile CardPM

Go to the top of the page

girasquid
post 29 Jun, 2007 - 09:43 PM
Post #7


Barbarbar

Group Icon
Joined: 3 Oct, 2006
Posts: 1,256



Thanked 14 times

Dream Kudos: 650
My Contributions


I think you might have misunderstood something he said. You have this:
CODE

foreach ( $top_menu as $links )

And I'm pretty sure he wanted you to do this:
CODE

foreach ( $links as $link )


This post has been edited by girasquid: 29 Jun, 2007 - 09:43 PM
User is offlineProfile CardPM

Go to the top of the page

Styx
post 29 Jun, 2007 - 10:48 PM
Post #8


D.I.C Head

Group Icon
Joined: 4 Mar, 2007
Posts: 192



Dream Kudos: 225
My Contributions


He just changed the variables around.

You need to take the quotations off the variables in the loop.

See this example:
CODE
<?php
$result = array(array('name' => 'Test', 'url' => 'dreamincode.net'),
        array('name' => 'Fred', 'url' => 'website.com'));

print "<ul>";
$top_menu = $result; //$db->getALL('SELECT * FROM top_menu');
foreach ( $top_menu as $links )
    {
    print "<li><a href=\"";
    echo $links['url'];
    print "\"><span>";
    echo $links['name'];
    print "</span></a></li>";
     }
print "</ul>";
?>
User is offlineProfile CardPM

Go to the top of the page

duffsstuff
post 30 Jun, 2007 - 09:02 AM
Post #9


D.I.C Head

**
Joined: 10 Sep, 2006
Posts: 67


My Contributions


I tried switching the variables like you said but it still didn't work and that is where I tried something else.
User is offlineProfile CardPM

Go to the top of the page

Styx
post 30 Jun, 2007 - 10:31 AM
Post #10


D.I.C Head

Group Icon
Joined: 4 Mar, 2007
Posts: 192



Dream Kudos: 225
My Contributions


Had you tried the last example posted?

In any case, did your something else work out for you? What did you do?
User is offlineProfile CardPM

Go to the top of the page

duffsstuff
post 30 Jun, 2007 - 10:42 AM
Post #11


D.I.C Head

**
Joined: 10 Sep, 2006
Posts: 67


My Contributions


yes the code above worked. So it must be that I am not connecting to the data base correctly. How should I re-write the db connection because i have to do it so often. can I make a connection to the database without closing it when the script closes?
User is offlineProfile CardPM

Go to the top of the page

duffsstuff
post 30 Jun, 2007 - 12:28 PM
Post #12


D.I.C Head

**
Joined: 10 Sep, 2006
Posts: 67


My Contributions


CODE

require 'config.php';
    
// Connect to Mysql
$db = mysql_connect($dbhost, $dbusername, $dbpassword)
        or die('Could not connect: ' . mysql_error());
//Select the correct database.
mysql_select_db($dbname,$connect)
    or die ("Could not select database");
    
print "<ul>";

$top_menu = $db->getALL('SELECT * FROM top_menu');

foreach ( $top_menu as $links )
        {
        print "<li><a href=\"";
        echo $links['url'];
    print "\"><span>";
    echo $links['name'];
    print "</span></a></li>";
        }

print "</ul>";


This is what I now have. I am now connecting correctly. I am now getting an error on line 45 which is:
CODE

$top_menu = $db->getALL('SELECT * FROM top_menu');


I can't find what is wrong with it. This is the error:

Fatal error: Call to a member function on a non-object in /homepages/16/d168863731/htdocs/source/functions.php on line 45

Thanks for all the help.
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 06:24AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month