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
https://forums.phpfreaks.com/topic/71138-solved-basic-question/
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
https://forums.phpfreaks.com/topic/71138-solved-basic-question/#findComment-357756
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
https://forums.phpfreaks.com/topic/71138-solved-basic-question/#findComment-357757
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
https://forums.phpfreaks.com/topic/71138-solved-basic-question/#findComment-357759
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
https://forums.phpfreaks.com/topic/71138-solved-basic-question/#findComment-357760
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
https://forums.phpfreaks.com/topic/71138-solved-basic-question/#findComment-357764
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.