1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Array Problem....I think

Discussion in 'PHP' started by tarrx3, Oct 22, 2010.

  1. #1
    OK let me first say that I am a noob when it comes to PHP and I am trying to figure something out and after some tedious testing I decided to come here. I fear it is some very easy solution that I am missing or don't yet understand so here it is.

    I want to echo some values throughout my webpage and the values are in a sql database. So for testing if I say:

    
    <?php
    $test = 'This is a test';
    ?>
    
    <body>
    <?php echo $test; ?>
    
    PHP:
    This works just fine the value of $test will echo on the page. But what I am trying to do is slightly more complicated and the way I am trying to go about it is below, the values have been changed for this purpose.

    
    <?php
    include('database.php');
    $getTitle = 'test';
    $results = queryDB("SELECT * FROM mytable WHERE title = '$getTitle'");
    $title = $results['title'];
    ?>
    
    <body>
    <?php echo $title; ?>
    PHP:
    This does not work for me, the spot that it should echo is blank. The queryDB is a function that gets my data from the database and it works just fine.
    function queryDB($sql) {
     $con = connect();
     $result = mysql_query($sql, $con);
     if(!$result){
      die('Could not retrieve data: ' . mysql_error());
     }
     while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
      $results[] = $row;
     }
     disconnect($con);
     return $results;	
    }
    PHP:
    The array is populated with the info and I can get it to print to the screen using print_r(array_values($results));

    So my question is..

    How do I get the selected item from the array that is returned by the sql query to echo out on the screen?

    Sorry if this seems like an uber noob question but I have just starting trying to code my own php.

    Forgot to mention that I am pulling only 1 row from the database because I am searching on a unique value.
     
    Last edited: Oct 22, 2010
    tarrx3, Oct 22, 2010 IP
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #2
    The problem is with your queryDB function; well, the way you are handling data.

    What you are doing here is putting hash into an array - i.e it becomes an array of hashes. However, you are trying to access it through an array when you assign it to $title.

    This should fix it;

    $title = $results[0]['title'];
    PHP:
     
    lukeg32, Oct 22, 2010 IP
    tarrx3 likes this.
  3. tarrx3

    tarrx3 Peon

    Messages:
    124
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Awesome you are my new favorite person. That fixed my problem perfectly. +rep
     
    Last edited: Oct 22, 2010
    tarrx3, Oct 22, 2010 IP