Question:

PHP and MySQL Problem on Yahoo! Web Hosting

by  |  earlier

0 LIKES UnLike

I am a little new to design and management of webpages, and even more so with databases. However, I have learned a lot in my short time working with it because I am a really fast learner :-) However, I cannot seem to solve this problem as I have looked all over Yahoo! Answers and all over the internet and I cannot find what I am doing wrong.

First, let me tell you my problem, then I will show you the code and maybe someone can help me. I don't really know what I am doing wrong cause no error messages are showing up. It just comes up blank, no error messages or results or anything, just blank. My code is as follows:

<?php

$maincon = mysql_connect("mysql","username","passwo...

if (!$maincon)

{die ("Unable to connect to database: " . mysql_error() };

mysql_select_db("fsg",$maincon);

$result = mysql_query("SELECT * FROM mlb");

if(!$result) die("Query Failed.");

while($row = mysql_fetch_array($result))

{

echo "<a href="" . $row['Link'] . "">" . $row['City'] . " " $row['Name'] . "</a>";

}

mysql_close($maincon);

?>

The "mysql" name for the server is what is required by Yahoo! for their webhosting. The code is to simply disply the teams of the MLB with a link to their page. I think the problem is somewhere in the database related code though, cause nothing is showing up.

Any thoughts would be greatly appreciated. Thank you in advance.

 Tags:

   Report

2 ANSWERS


  1. Right, we need to find out what the error is first!

    Where you execute (!$result) I assume it always kills the page and says query failed?

    Instead of what you have try this so that you can see the error

    if(!$result) die(mysql_error());

    Let me know what is returned and I can help further, if you dont figure yourself that is ;)


  2. It looks to me line this line is causing the trouble:

    echo &quot;&lt;a href=&quot;&quot; . $row[&#039;Link&#039;] . &quot;&quot;&gt;&quot; . $row[&#039;City&#039;] . &quot; &quot; $row[&#039;Name&#039;] . &quot;&lt;/a&gt;&quot;;

    try escaping the double quotes that are supposed to show up in the code on the end page, and get a period just before $row[&#039;Name&#039;]:

    echo &quot;&lt;a href=\&quot;&quot; . $row[&#039;Link&#039;] . &quot;\&quot;&gt;&quot; . $row[&#039;City&#039;] . &quot; &quot; . $row[&#039;Name&#039;] . &quot;&lt;/a&gt;&quot;;

    In general, when I run into a problem like this, my first step is some poor-man&#039;s (er, woman&#039;s) debugging which does a &quot;print&quot; statement every couple of lines so I can see where things go wrong.  It helps focus on the exact problem.  So I&#039;d put in lines like

    print &quot;a&quot;;

    &lt;a couple of lines of code&gt;

    print &quot;b&quot;;

    &lt;a couple of lines of code&gt;

    and so on, and then you know exactly where the problem is.  (There are the goofiest connection errors sometimes...) Then you can take that line and break it down and figure out exactly which part is the problem (if it is the line I targeted above, try

    print &quot;$row[&#039;Link&#039;]&quot;;

    and if that works, try

    print &quot;&lt;a href=&quot;&quot; . $row[&#039;Link&#039;]&quot;;

    whoops -- you&#039;d see an error on that one, and you&#039;ve have narrowed down the problem.

    Good luck!

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.