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.
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: