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 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    99
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Exclamation Displaying mysql results not working

    Hi, I got a page with labels for aquatic species. Next to the labels I want to display the results from the db table for that row, but I cant get it to work.

    Here is what I got


    Code:
    <?php
    $commonName = 'African Butter Catfish';
    $connect_error = 'Sorry, we are currently experiencing database issues. We are working to resolve this and should be up and running again shortly.';
    mysql_connect('localhost', 'username', 'password') or die($connect_error);
    mysql_select_db('equatics_tropics') or die($connect_error);
    mysql_query("SELECT * FROM species WHERE commonName='$commonName'") or die(mysql_error());
    
    
    
    ?>
    
    <section id="content">
      <article id="index">
        <section class="index">
    
    	
    
    	<fieldset title="e-quatics.com">
    		<legend>PHP CODE TO SHOW COMMON NAME</legend>
    		<p><img src="../_images/tropical.png" height="250" width="250">
    		Common Name: <? echo $rows['speciesCommon']; ?><br>
    		Scientific Name: <? echo $rows['speciesScientific']; ?> <br>
    		Synonym: <? echo $rows['speciesSynonym']; ?><br>
    		Origin: <? echo $rows['origin']; ?><br>
    		Size: <? echo $rows['size']; ?><br>
    		Environment: <? echo $rows['environment']; ?><br>
    		Water Chemistry: <? echo $rows['waterChemistry']; ?><br>
    		Temperature: <? echo $rows['temperature']; ?><br>
    		Feeding: <? echo $rows['feeding']; ?><br>
    		Sexing: <? echo $rows['sexing']; ?><br>
    		Compatability: <? echo $rows['compatability']; ?><br>
    		Temperament: <? echo $rows['temperament']; ?><br>
    		Sexing: <? echo $rows['sexing']; ?><br>
    		Breeding: <? echo $rows['breeding']; ?><br>
    		Comments: <? echo $rows['comments']; ?></p>
    		
    		
    	</fieldset>
    			
    </form>
        </section>
      </article>
      
    <?php 
    	include 'includes/overall/footer.php';
    ?>
    
    </body>

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    OK well you have to actually receive the result in PHP. You query the DB but you have to use a function to retrieve the data.

    I suggest mysql_fetch_assoc, which returns an associative array containing the information from the database.
    PHP Code:
    $result mysql_query("SELECT * FROM species WHERE commonName='$commonName'") or die(mysql_error());

    $rows mysql_fetch_assoc($result) or die(mysql_error()); 
    Now you can access the data which is stored in the $rows array

    Edit: However, the mysql extension to PHP is deprecated as of PHP 5.5, so this will not work in the eventual future, as servers are upgraded to the latest version of PHP.
    Recommended instead are the mysqli and PDO_MYSQL extensions:
    http://www.php.net/manual/en/book.mysqli.php
    http://www.php.net/manual/en/ref.pdo-mysql.php
    Last edited by whizard; 07-04-2013 at 11:37 PM.
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • Users who have thanked whizard for this post:

    aquaman (07-04-2013)

  • #3
    New Coder
    Join Date
    Oct 2012
    Posts
    99
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Smile

    Quote Originally Posted by whizard View Post
    OK well you have to actually receive the result in PHP. You query the DB but you have to use a function to retrieve the data.

    I suggest mysql_fetch_assoc, which returns an associative array containing the information from the database.
    PHP Code:
    $result mysql_query("SELECT * FROM species WHERE commonName='$commonName'") or die(mysql_error());

    $rows mysql_fetch_assoc($result) or die(mysql_error()); 
    Now you can access the data which is stored in the $rows array

    Edit: However, the mysql extension to PHP is deprecated as of PHP 5.5, so this will not work in the eventual future, as servers are upgraded to the latest version of PHP.
    Recommended instead are the mysqli and PDO_MYSQL extensions:
    http://www.php.net/manual/en/book.mysqli.php
    http://www.php.net/manual/en/ref.pdo-mysql.php
    Thank you, just what i was looking for


  •  

    Posting Permissions

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