Jump to content

[SOLVED] Basic Question


LemonInflux

Recommended Posts

Hello. First of all, excuse the simplicity of the question, I'm only just starting :-)

 

Ok, a while back, I built a totally flat forum. It worked, (and works) fine, it's just I decided I can't stay using flatfiles forever. So, I decided I'd make a MySQL forum. So, I've built every page I reckon I'll need, it's just now I'm coding it. So, here's my question unto you:

 

Where's the error?

 

<?php
$sql = 'SELECT * FROM `forums` LIMIT 0, 30 ';
while ($row = mysql_fetch_array($sql) {
     echo ('Forum 1: '. $row['forum_name'] .'<br><br>'. $row['forum_desc'] .'<br><br>');
}
?>

 

I'm trying to list each forum like this:

 

Forum Name

 

Forum Description

 

Basic for now, I'll build it up later. I'm sure it's a very simple error.

 

Thankyou kindly, Tom.

Link to comment
Share on other sites

<?php
$sql = 'SELECT * FROM `forums` LIMIT 0, 30 ';
$result = mysql_query($sql) or die(mysql_error()); // <-- inser this line
while ($row = mysql_fetch_array($result) {
     echo ('Forum 1: '. $row['forum_name'] .'<br><br>'. $row['forum_desc'] .'<br><br>');
}
?>

Link to comment
Share on other sites

didn't work. If anyone's interested, here's the code I connect to the db with:

 

<?php
$dbHost = "*";        //Location Of Database usually its localhost
$dbUser = "*";            //Database User Name
$dbPass = "*";            //Database Password
$dbDatabase = "*";       //Database Name

$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");
mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");
?>

Link to comment
Share on other sites

Hello,

 

There are 2 Basic mistakes in your code which let you error.

 

- Missing with the closing brace for while condition. See your code..

while ($row = mysql_fetch_array($sql) {

and it should be..

while ($row = mysql_fetch_array($sql)) {

 

- Another big mistake is you were fetching records without executing the query. See your code below..

$sql = 'SELECT * FROM `forums` LIMIT 0, 30 ';
while ($row = mysql_fetch_array($sql) {

 

and it should be..

$sql = mysel_query('SELECT * FROM `forums` LIMIT 0, 30 ') or die(mysql_error());
while ($row = mysql_fetch_array($sql)) {

 

Hope this will solve your issue.

Regards

Link to comment
Share on other sites

Well, first of all, what happens? Any error output?

<?php
$sql = 'SELECT * FROM `forums` LIMIT 0, 30 ';
while ($row = mysql_fetch_assoc($sql)) {
     echo 'Forum 1: '. $row['forum_name'] .'<br><br>'. $row['forum_desc'] .'<br><br>';
}
?>

mysql_fetch_assoc() VS. mysql_fetch_array() should not make a difference, I am just used to the former.

-The closing ) of the while is missing.

-Here is a quote form php.net regarding echo:

echo() is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo() (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo(), the parameters must not be enclosed within parentheses.

 

hmm, 3 new replies already, oh well, have mine too.

 

oh nice, I missed the lack of query execution...

Link to comment
Share on other sites

while ($row = mysql_fetch_array($sql) {

and

while ($row = mysql_fetch_array($sql)) {

are very different!

 

My posted code was incorrect, I missed the fact that the query execution was missing.

<?php
$sql = 'SELECT * FROM `forums` LIMIT 0, 30 ';
$sqlresult = mysql_query($sql);
while ($row = mysql_fetch_assoc($sqlresult)) {
     echo 'Forum 1: '. $row['forum_name'] .'<br><br>'. $row['forum_desc'] .'<br><br>';
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.