PHP Error - Help Please

Discussion in 'PHP' started by . Jordan ., May 22, 2010.

  1. #1
    I'm trying to list the emails and names contained within an sql database table. However, I'm constantly getting this error. Any ideas what the problem is.


    Error:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/jordanca/public_html/email-signup/admin.php on line 38
    Code (markup):
    PHP code:
    <?php
    	
    	$sqlConnect = mysql_connect($sqlHost, $sqlUser, $sqlPass);
    	mysql_select_db($sqlEmailsTable, $sqlConnect);
    	
    	$getEmails = mysql_query('SELECT * FROM emails ORDER BY id DESC', $sqlConnect);
    	
    	while($emails = mysql_fetch_array($getEmails)) {
    		
    		echo('
    			 <tr>
    			 
    			 	<td>' . $emails['name'] . '</td>
    				
    				<td>' . $emails['email'] . '</td>
    				
    			 </tr>
    			 ');
    		
    	}
    	
    	?>
    PHP:

    and yes, this is wrapped in a table tag.
     
    . Jordan ., May 22, 2010 IP
  2. muzaffar

    muzaffar Peon

    Messages:
    31
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It seems that there is some problem with your SQL query. Try to execute this query first using phpmyadmin. This error normally appears when some coloumn/fieled name is missing or wrong.
     
    muzaffar, May 22, 2010 IP
  3. . Jordan .

    . Jordan . Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Just tried the query in PHPMyAdmin and it displayed the rows fine. Any further thoughts...
     
    . Jordan ., May 22, 2010 IP
  4. muzaffar

    muzaffar Peon

    Messages:
    31
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    can you paste your table structure so that I dig out problem.
     
    muzaffar, May 22, 2010 IP
  5. exceedwebhost

    exceedwebhost Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Have you checked to make sure your SQL connection information is correct (Host, username, and password). Also make sure the case in your query matches the case of all column and table names.
     
    exceedwebhost, May 22, 2010 IP
  6. . Jordan .

    . Jordan . Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Here you go mate:

    [​IMG]

    The sql connection is fine because users can still submit their email address. Also, the cases are fine as you can see from the above image. :\
     
    . Jordan ., May 22, 2010 IP
  7. . Jordan .

    . Jordan . Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    ***bump***
     
    . Jordan ., May 22, 2010 IP
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    try the following, and reply with errors (if any) - in their exact form...

    <?php
        error_reporting(E_ALL);
        $sqlConnect = mysql_connect($sqlHost, $sqlUser, $sqlPass);
        mysql_select_db($sqlEmailsTable, $sqlConnect);
       
        $getEmails = mysql_query('SELECT * FROM emails ORDER BY id DESC', $sqlConnect) or trigger_error('Query Failed: '.mysql_error(), E_USER_ERROR);
       
        while($emails = mysql_fetch_array($getEmails)) {
           
            echo('
                 <tr>
                 
                    <td>' . $emails['name'] . '</td>
                   
                    <td>' . $emails['email'] . '</td>
                   
                 </tr>
                 ');
           
        }
       
        ?>
    PHP:
     
    danx10, May 22, 2010 IP
  9. lordspace

    lordspace Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I'd add the limit clause e.g. 'SELECT * FROM emails ORDER BY id DESC LIMIT 0, 1000' but you'll have to have another loop

    I know that 1and1 had such limit. For example if you tried to retrieve more than 1 000 records you'd get some nasty errors.
     
    lordspace, May 22, 2010 IP
  10. Trikun3

    Trikun3 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    <?php
    
    error_reporting(E_ALL);
    
    if(!($sqlConnect = mysql_connect($sqlHost, $sqlUser, $sqlPass)))
    	die('Connection failed: '.mysql_error());
    
    if(!mysql_select_db($sqlEmailsTable, $sqlConnect))
    	die('Database Select failed: '.mysql_error());
    
    $sql = "SELECT * FROM `emails` ORDER BY `id` DESC;";
    $getEmails = mysql_query($sql, $sqlConnect);
    
    while($emails = mysql_fetch_assoc)
    {
    	echo '
    		<tr>
    			<td>',$emails['name'],'</td>
    			<td>',$emails['email'],'</td>
    		</tr>
    		';
    }
    Code (markup):
    I've had a similar issue like yours before. Make sure the tablename is exactly the same as the actual tablename. Capitals are different than lowercase in MySQL. (tblOne is different than tblone)
     
    Trikun3, May 22, 2010 IP
  11. . Jordan .

    . Jordan . Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Fatal error: Query Failed: No database selected in /home/jordanca/public_html/email-signup/admin.php on line 38
     
    . Jordan ., May 22, 2010 IP
  12. Trikun3

    Trikun3 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Make sure your $sqlEmailsTable variable is set.
     
    Trikun3, May 22, 2010 IP
  13. . Jordan .

    . Jordan . Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Thanks everyone,

    In the end, it was because I was trying to select a DB using the variable holding the table name rather than the db name. However, I now have a new problem...

    The page is only loading a small amount of the css. If you would like to see what I mean, look here and here. As you can see only a little amount of the css is there. Below is the php and the css.

    @charset "utf-8";
    /* CSS Document */
    
    body {
    	background: #666;
    	margin: 0;
    	padding: 0;
    	font-family: Arial, Helvetica, sans-serif;
    	font-size: 12px;
    	color: #FFF;
    }
    
    .statusMessage {
    	width: 400px;
    	margin: 50px auto;
    	background: #222;
    	text-align: center;
    	border: solid 1px #888;
    	border-radius: 5px;
    	-moz-border-radius: 5px;
    	-webkit-border-radius: 5px;
    	padding: 10px;
    }
    
    .adminPanel {
    	width: 700px;
    	margin: 50px auto;
    	background: #222;
    	text-align: center;
    	border: solid 1px #888;
    	border-radius: 5px;
    	-moz-border-radius: 5px;
    	-webkit-border-radius: 5px;
    	padding: 10px;
    }
    
    form {
    	width: 400px;
    	margin: 50px auto;
    	background: #222;
    	text-align: center;
    	border: solid 1px #888;
    	border-radius: 5px;
    	-moz-border-radius: 5px;
    	-webkit-border-radius: 5px;
    }
    
    label {
    	display: block;
    	margin-bottom: 5px;
    }
    
    fieldset {
    	margin: 10px 0px;
    	border: none;
    }
    
    .txtBox {
    	width: 300px;
    	margin: 0 auto;
    	background: #FFF;
    	color: #444;
    	border: none;
    	border-radius: 5px;
    	-moz-border-radius: 5px;
    	-webkit-border-radius: 5px;
    	height: 20px;
    	text-align: center;
    }
    
    .button {
    	width: 160px;
    	border: none;
    	background: #FFF;
    	color: #444;
    	border-radius: 5px;
    	-moz-border-radius: 5px;
    	-webkit-border-radius: 5px;
    	text-align: center;
    	height: 25px;
    }
    
    table {
    	width: 600px;
    	margin: 0 auto;
    	background: none;
    	border: none;
    	text-align: center;
    }
    
    .tableTop {
    	border: solid 1px #444;
    	background: #333;
    	margin: 0;
    	padding: 5px;
    	font-weight: bold;
    	text-transform: uppercase;
    }
    Code (markup):
    <?php include('core.php') ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Send Message</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    </head>
    
    <body>
    
    <div class="adminPanel">
    
    <h1>Send Message</h1>
    
    <p>Welcome to the message send area. This file is used to send out a mass message to all signed up emails. In order to send a message, first enter a message below and then click send.</p>
    
    <p style="color:#900; font-weight: bold;">NOTE: REMOVE THIS FILE AFTER USE FOR SECURITY</p>
    
    <table>
    
    	<tr>
        
        	<td class="tableTop">Name</td>
            
            <td class="tableTop">Email</td>
        
        </tr>
        
        <?php
    	
    	$sqlConnect = mysql_connect($sqlHost, $sqlUser, $sqlPass);
    	mysql_select_db($sqlDB, $sqlConnect);
    	
    	$getEmails = mysql_query('SELECT * FROM ' . $sqlEmailsTable . ' ORDER BY id DESC', $sqlConnect);
    	
    	while($emails = mysql_fetch_array($getEmails)) {
    		
    		echo('
    			 <tr>
    			 
    			 	<td>' . $emails['name'] . '</td>
    				
    				<td>' . $emails['email'] . '</td>
    				
    			 </tr>
    			 ');
    		
    	}
    	
    	
    	
    	?>
    
    </table>
    
    </div>
    
    </body>
    </html>
    PHP:
     
    . Jordan ., May 22, 2010 IP
  14. phpSiteMinder

    phpSiteMinder Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    In future try
    
    $getEmails = mysql_query('SELECT * FROM emails ORDER BY id DESC', $sqlConnect);
    
    if ($getEmails === false){
        exit(mysql_error());
    }
    
    PHP:
    then you will at least find out if the query is failing, and can handle the error
     
    phpSiteMinder, May 22, 2010 IP