Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Error while trying code from w3schools.com

    Hi,

    I'm trying out some php/mysql code for the first time, and I'm getting errors while trying some example code from www.w3schools.com.

    Here is the page I'm looking at:

    http://www.w3schools.com/php/php_mysql_select.asp

    and here is the code I'm trying:


    <?php
    $con = mysql_connect("mysql1.***************","a2080062_mpr","test!!");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("stest", $con);

    $result = mysql_query("SELECT * FROM stest2 WHERE Song='Walk This Way'");

    echo "<table border='1'>
    <tr>
    <th>Date</th>
    <th>Song</th>
    </tr>";
    while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    echo "<td>" . $row['Date'] . "</td>";
    echo "<td>" . $row['Song'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";
    mysql_close($con);
    ?>



    The error I get on my site says it's with the
    while($row = mysql_fetch_array($result))
    line, and it says:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a2080062/public_html/test.html on line 21



    The table in the database I'm accessing has four columns: "Date," "Venue," "Location," "Song." So a row in this table might look like:

    1975-04-19 Boston Garden Boston MA Walk This Way

    and I'm trying to print out a table listing all dates that have a 'Walk This Way' in the Song column, with the actual string 'Walk This Way.'

    Any ideas why this isn't working?
    Thanks a lot.

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    It usually means that the query that you did was incorrect so the SQL being "SELECT * FROM stest2 WHERE Song='Walk This Way'" it could be than there is no table called that or no column called song.

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You need to do some error checking. You don't even know if your query is failing or not.
    PHP Code:
    <?php
    $con 
    mysql_connect("mysql1.***************","a2080062_mpr","test!!");
    if (!
    $con)
    {
    die(
    'Could not connect: ' mysql_error());
    }

    mysql_select_db("stest"$con);

    $result mysql_query("SELECT * FROM stest2 WHERE Song='Walk This Way'") or die(mysql_error());

    echo 
    "<table border='1'>
    <tr>
    <th>Date</th>
    <th>Song</th>
    </tr>"
    ;
    while(
    $row mysql_fetch_array($result))
    {
    echo 
    "<tr>";
    echo 
    "<td>" $row['Date'] . "</td>";
    echo 
    "<td>" $row['Song'] . "</td>";
    echo 
    "</tr>";
    }
    echo 
    "</table>";
    mysql_close($con);
    ?>
    What do you get when you run that?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #4
    New to the CF scene
    Join Date
    May 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. I checked those things, but I think they're right. Take a look at this screenshot of my work area:



    You can see at the top and the left that my database's name is 'stest', like in my code (I also tried switching it to a2080062_stest, it didn't make a difference) and that I have two tables, 'songs' and 'stest2.' Also, you can see my four column names, and that the last one is 'Song.' Any more ideas?

  • #5
    New to the CF scene
    Join Date
    May 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    You need to do some error checking. You don't even know if your query is failing or not.

    What do you get when you run that?

    This solved it for me - thanks! I ran your code and got "no database selected" so I changed the database to a2080062_stest, which I previously said didn't make a difference. Thanks again


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •