/    Sign up×
Community /Pin to ProfileBookmark

foreach problem

Now, I have never been exactly good with the foreach function, But I have managed to get along with it fairly well. However, I am not sure what is wrong with this.

[code=php]<?
$sqlconn = “localhost”;
$sqluser = “username”;
$sqlpass = “password”;
$sqldb = “database”;
$username = $_SESSION[‘username’];

$conn = mysql_connect($sqlconn, $sqluser, $sqlpass);
if(!$conn) {
die(“Could not connect to: “.$sqlconn.”:.:”.mysql_error());
}

mysql_select_db($sqldb, $conn)
or die(“Could not select:.:”.$sqldb.” – “.mysql_error());

$query = mysql_query(“SELECT * FROM nav”) or die(“Query Failed: “.mysql_error());

$row = mysql_fetch_assoc($query);

echo “<table border=”1″ cellpadding=”3″ cellspacing=”0″>n”;
while($line = mysql_fetch_array($query, MYSQL_ASSOC)) {
echo “t<tr>n”;
foreach($line as $col_value) {
echo “tt<td>$col_value</td>n”;
}
}
echo “</table>”;

mysql_free_result($query);
mysql_close($conn);
?>[/code]

Now this code, is supposed to connect to the database, run a query to select everything from a table, and then print out each item in database table into a table on the page. There is one row in the database table, only when I go to the page, It isn’t printing out the row. It is showing the html that I have added other than that(I didn’t post it in with it as its unrelavent) when I view the source code, so it is the right page and everything, and It has the opening and close table tags. It just isn’t printing it out, I immagine that it would be some simple problem, but as I say, try as I might, I cannot get the hang of this function. Your Help is appreciated.

Thanks,
[B]~[/B][I]Travis[/I]

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@MstrBobJul 18.2006 — Hmm, I don't see anything jumping out at me. Are the <table> and <tr> tags in the source? Try print_r($line) inside your while loop to see if there's actually anything inside the array, as you seem to be using foreach() correctly.
Copy linkTweet thisAlerts:
@Heavy_MetalauthorJul 18.2006 — Hmm, I don't see anything jumping out at me. Are the <table> and <tr> tags in the source? Try print_r($line) inside your while loop to see if there's actually anything inside the array, as you seem to be using foreach() correctly.[/QUOTE]
To your question: No the <tr> </tr> tags are not appearing in the source, but as I said the open and close table tables are, just everything in the foreach statement isn't appearing, I just tried the print_r($line); statement, and I still get the same result, but I have just double checked and there is definatly something in the database. Lastly, wow, I am glad to see I have used foreach correctly.
Copy linkTweet thisAlerts:
@chazzyJul 18.2006 — why are you retrieving that first row separate..?

what happens if you do this...

[code=php]
$query = mysql_query("SELECT * FROM nav") or die("Query Failed: ".mysql_error());

echo "<table border="1" cellpadding="3" cellspacing="0">n";
while($line = mysql_fetch_assoc($query)) {
echo "t<tr>n";
foreach($line as $col_value) {
echo "tt<td>$col_value</td>";
}
echo "nt</tr>n";
}
echo "</table>";
[/code]
Copy linkTweet thisAlerts:
@Heavy_MetalauthorJul 18.2006 — what do you mean, why am I retrieving the first row seperate, I do not understand what you mean
Copy linkTweet thisAlerts:
@chazzyJul 18.2006 — in your code above you do this

[code=php]
mysql_select_db($sqldb, $conn)
or die("Could not select:.:".$sqldb." - ".mysql_error());

$query = mysql_query("SELECT * FROM nav") or die("Query Failed: ".mysql_error());

$row = mysql_fetch_assoc($query);
[/code]


when you call mysql_fetch_assoc you are reading the first row from the table after you select it. why are you doing this? this might be why nothing appears - there are no other rows maybe.
Copy linkTweet thisAlerts:
@Heavy_MetalauthorJul 18.2006 — Oh, I see what you are saying, I took that one out and it works fine now, one problem, I just realized a few secounds ago that this wasn't what I was trying to do at all o.o' Sorry, but at least I understand the foreach function now.
Copy linkTweet thisAlerts:
@Heavy_MetalauthorJul 18.2006 — Hold up, Turns out, I am needing something like that, But, I need to echo out each of the columns, in a different text box, which will be in a form, which will go to another page which I am going to have problems with also, because I need the other page to insert each of them in there. I am also going to need something where I can add three more textboxes to the bottom but I can worry about this later. I am not sure if I am way overshooting with this here or what, but In a nutshell, I need some method of managing a navigation, thats why I am doing all of this. There might be a much simpler way of acomplishing this kind of script, But I really need to get this done. Thanks.
Copy linkTweet thisAlerts:
@chazzyJul 18.2006 — are you trying to create something like a web based spreadsheet?
Copy linkTweet thisAlerts:
@Heavy_MetalauthorJul 18.2006 — ummm, no, i quote myself from above but In a nutshell, I need some method of managing a navigation[/quote]
×

Success!

Help @Heavy_Metal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...