FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


mysql_fetch_array().... pissing me off!





DjMilez
I'm using this code as an index page so far
Code:
<?php
/* Glacier Board
By ProwerBot of SkySoft Net
Started June 24, 2008
*/

//Connect to the DB and make the skin//
include("functions/connect.php");
include("functions/themename.php");
include("themes/$skin/header.php");

//Create the thing with your user panel//
echo("<hr />
<div class='menubar'>User CP</div>");
if (isset($_COOKIE['username_gb']))
echo("Welcome back, " .$_COOKIE['username_gb']. ".");
else
echo("Welcome Guest. <a href='?view=sign_in'>Sign in</a> or <a href='?view=register'>Register</a>");
echo("</div>
</td>
<td>
<div class='titlebar'>Main Content</div>
<div class='content'>");

//Get a few other things ready//
$view = $_GET['view'];
if (empty($view)) {
$view = home;
}


//Call the switch function//
switch($view) {




//Index page//
case "home":
$sql = "SELECT * FROM news ORDER BY id DESC";
$result = mysql_query($sql);
while($rows = mysql_fetch_array($result)){
 echo (" <div class='box_container'><div class='box'> " . $rows['newstitle'] ." </div><div class='box_content'>" . $rows['newspost'] . "</div> <br>");
}
break;
}

echo("</div>");
mysql_close();
?>



And this part of the code
Code:
case "home":
$sql = "SELECT * FROM news ORDER BY id DESC";
$result = mysql_query($sql);
while($rows = mysql_fetch_array($result)){
 echo (" <div class='box_container'><div class='box'> " . $rows['newstitle'] ." </div><div class='box_content'>" . $rows['newspost'] . "</div> <br>");
}


ends up with
Quote:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/djmilez/public_html/index.php on line 43


Lol can someone help?
Magicman
I'd have to check back to look up some MYSQL/php syntax to completely figure out what you are doing wrong but the first thing I would check would be that your SQL command (what you have stored as $sql) works properly and actually returns results.
kv
1. Check if your connect.php is connecting properly to database and selecting a database and those calls are going through fine
2. Do a var_dump on $result to see if it is a valid object
3. Do a mysql_row_count to see if you are getting non-zero results for your query.

Your sql query syntax looks fine.
Fire Boar
Code:
case "home":
$sql = "SELECT * FROM news ORDER BY id DESC";
if (!$result = mysql_query($sql)) die(mysql_error());
while($rows = mysql_fetch_array($result)){
 echo (" <div class='box_container'><div class='box'> " . $rows['newstitle'] ." </div><div class='box_content'>" . $rows['newspost'] . "</div> <br>");
}


Change the third line in that section so that it looks like the one above. The problem is that $result is returning null, so this should give you some insight into what the actual problem is.
kv
Fire Boar wrote:
Code:

if (!$result = mysql_query($sql)) die(mysql_error());



Such an easy and simple thing Smile didn't occur to me.
Fire Boar
kv wrote:
Fire Boar wrote:
Code:

if (!$result = mysql_query($sql)) die(mysql_error());



Such an easy and simple thing Smile didn't occur to me.


Hehe, ever since I saw phpBB do that, I've made a habit of doing it myself. Of course, using database classes is even better - that way you only need to code it in once.
DjMilez
kv wrote:

2. Do a var_dump on $result to see if it is a valid object


omg, thanks so much for telling me about that function... It said "row id doesn't exist in 'order clause'" or something, and I realized I forgot to add the ID row! xD


Also, thanks for that awesome little code Fire Boar. =D
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.