[help] Php + Mysql Problem

I am having a problem with my PHP code that I created to have news on the front. It opens that mySQL database that has everything for the news. You can see the output here.

Here is the code that I am using:

$link = mysql_connect("{host}","{username}","{password}");
mysql_select_db("{database}");
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

if(array_key_exists("section" , $HTTP_GET_VARS)){}
else{$HTTP_GET_VARS["section"] = "front";}
switch($HTTP_GET_VARS["section"])
{
 case "front";
   echo "<br />\n";
   $intX = 0;
   $result = mysql_query("SELECT * FROM `news` WHERE imp=1");
   echo $result;
   if (!$result)
   {
     die('Invalid query: ' . mysql_error());
   }
   while ($row = mysql_fetch_array($result))
   {
     dispNews($row["date"], $row["title"], $row["body"], $row["section"], $row["page"], $row["dnld"], $row["imp"], $row["type"]);
     $intX++;
   }
   $query = "SELECT * FROM `news` ORDER BY ID DESC";
   $result = mysql_query($query);
   while ($record = mysql_fetch_object($result))
   {
     if("$record->imp"!=1)
     {
       dispNews("$record->date", "$record->title", "$record->body", "$record->section", "$record->page", "$record->dnld", "$record->imp", "$record->type");
       $intX++;
       if($intX==5){break;}
     }
   }
 break;

...

}
echo "<br />\n";
mysql_close($link);

Apparently it stops at "if (!$result){die('Invalid query: ' . mysql_error());}". It doesn't even go on finishing the rest of the page. Does anybody know how to fix this?

#174209

What do you want it to do? Say "Invalid Query: Error blah blah" and continue loading the page?

Then don't use "die" because when the parser hits the "die" command, it does just that, it dies. Echo it or print it or something of that sort, and then break out of the switch

#174213

Ahh, that's why it wasn't finishing it.

But that wasn't the initial problem. Sorry I didn't clarify this. I do have the database, table and fields, with a couple of records. The problem is it won't read and output it.

Now I am getting a

Connected successfully
Invalid query: No Database Selected
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/wizard/public_html/index.php on line 39

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/wizard/public_html/index.php on line 47

Dammit. I was using those two functions incorrectly. Let me see if I can find out what I am doing wrong.

#174216

Seems like it's not correctly selecting the database... try

mysql_select_db("{database}");
echo 'Database selected.  Error? :' .  mysql_error();

#174224

Yeah your right. I took out a similar line of code prematurely. I had it correct before (I am running a localhost on my computer) so I must have changed something when I moved it over.

Thanks again Cerebral.

#174227

you need to give the $link as parameter to mysql_select_db

look here

this should work:

mysql_select_db("{database}", $link);

greets,

08/15

#174228

I am officially stupid. I forgot to give the user permissions.

Thank you very much for the help you guys.

#174243

Not a prob, Wiz-man. :P

-=edit=- By the way, Null8Fuffzehn, the link isn't a required parameter. Since Wizard left it out, the mysql_select_db function automatically used the last link created, which happened one line previously in mysql_connect. Just FYI. -=/edit=-

#174245

thx for mentioning

#174246