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 11 of 11
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    32
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Error - Warning: mysql_fetch_array():

    Here is my code: http://pastebin.com/QuxX11fP
    Here is my site: http://msuth.co.uk/simpsons/index2.php

    I have this working Locally but when I tried to get it to work on an external server I get hit with this error
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/msuthco/public_html/simpsons/index2.php on line 32
    Basically, all I want my site to do is display a new Quote everytime the page is refreshed!

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    change this
    PHP Code:
    while ($row mysql_fetch_array($resultMYSQL_NUM)) { 
    to this

    PHP Code:
    while ($row mysql_fetch_array($result) { 

  • Users who have thanked Arcticwarrio for this post:

    Msuth (08-02-2012)

  • #3
    New Coder
    Join Date
    Jun 2011
    Posts
    32
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Nope...

    Parse error: syntax error, unexpected '{' in /home/msuthco/public_html/simpsons/index2.php on line 32

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Assuming you've modified your connection to not refer to localhost.. try

    PHP Code:
    $result mysql_query($query$link); 
    or remove mysql_selectdb and place the database detail in the original connection $link.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    Msuth (08-02-2012)

  • #5
    New Coder
    Join Date
    Jun 2011
    Posts
    32
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Hi Andrew, When I spoke to my host (Zyma) they told me to leave the host as localhost.

    I'll have a look into that aspect thanks!

  • #6
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    make a file called db.php and put this in it

    PHP Code:
    <?php
    // Database File
    function Q($query) {
        
    $Username 'INFO';
        
    $Password 'INFO';
        
    $Database 'rand_text';
        
    $Host 'localhost';
        global 
    $queries$queries_time$queries_count;
        
    $queries_count++;
        
    mysql_connect($Host$Username$Password) or die ( 'Unable to connect to the server.<br>' );
        
    mysql_select_db($Database) or die ( 'Unable to select the database.<br>' );
        
    $queries .= 'Line <b>'__LINE__ '</b> of file <b>' .__FILE__ '</b> requested <b>' $query '</b>';
        
    $time_overall round(microtime(),4);
        if (!
    $result mysql_query($query))    {
            echo 
    '<pre>Error on line <b>'__LINE__ '</b> of file <b>' __FILE__ '</b> requesting <b>' $query '</b></pre><br>';
            
    $queries .= ' <b>ERROR OCCURED</b><br>';
        } else {
            
    $queries .= '<br>';
        }
        
    $time2_overall round(microtime(), 4);
        
    $gen_overall $time2_overall $time_overall;
        
    $queries_time substr($queries_time $gen_overall,0,6) ;
        
    mysql_close() or die ( 'Unable to disconnect from the server.<br>' );
        return 
    $result;
    }
    ?>

    then in your page put this at the top:

    PHP Code:
    include ("db.php");
    <!
    DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"

    now try this:

    PHP Code:
    $Q1  Q("SELECT * FROM quote ORDER BY Rand() LIMIT 1");
     
      
    // Display records from the table
      
    echo "<table>";
       while (
    $row mysql_fetch_array($Q1)) {
          echo 
    "<tr><td>".$row[0]."</td></tr><tr><td>".$row[1]."</td></tr>";
       }
       echo 
    "</table>"
    you can also put this at the bottom of your page if you want it:

    PHP Code:
    <?php echo "<b>$queries_count</b> Database queries in <b>$queries_time</b> seconds"?>
    Last edited by Arcticwarrio; 08-02-2012 at 04:38 PM.

  • Users who have thanked Arcticwarrio for this post:

    Msuth (08-02-2012)

  • #7
    New Coder
    Join Date
    Jun 2011
    Posts
    32
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Thank you so much!

    Full site can be seen at http://msuth.co.uk/hosted/simpsons/

  • #8
    New Coder
    Join Date
    Aug 2012
    Location
    Finland
    Posts
    23
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Msuth View Post
    Thank you so much!

    Full site can be seen at http://msuth.co.uk/hosted/simpsons/
    You should add button which generates another random quote

  • #9
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    Quote Originally Posted by ECoode View Post
    You should add button which generates another random quote
    F5 already does generate a new quote

  • #10
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    p.s not that it really matters but just for your info:
    when your only dealing with 1 row removing the while statement only uses the 1st row

    PHP Code:
    $Q1  Q("SELECT * FROM quote ORDER BY Rand() LIMIT 1");
    $row mysql_fetch_array($Q1); // removed while
     
      // Display 1 record from the table
      
    echo "<table><tr><td>".$row[0]."</td></tr><tr><td>".$row[1]."</td></tr></table>"

  • #11
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    looks good

    if you want people to retrieve a specific quote you could do this too:

    PHP Code:
    <?php
        
    if (isset($_GET['Quote'])){
            
    $Q1  Q("SELECT * FROM quote WHERE `Id` = ".$_GET['Quote']);
        }else{
            
    $Q1  Q("SELECT * FROM quote ORDER BY Rand() LIMIT 1");
        }
         
    $row mysql_fetch_array($Q1);
          
    // Display record from the table
        
    echo "<form><table>";
        echo 
    "<tr><td><input name=\"Quote\" type=\"text\" size=\"3\"/></td><td><input name=\"Submit\" type=\"Submit\" value=\"Get Quote\"/></td></tr>";
        echo 
    "<tr><td>".$row[0]."</td></tr><tr><td>".$row[1]."</td></tr>";
        echo 
    "</table></form>"
    ?>
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month


  •  

    Posting Permissions

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