Join 136,576 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,873 people online right now. Registration is fast and FREE... Join Now!
First to solve your issue. Using the variable isn't working because the way you're including it in your query it is looking for a column named $na, which is assume doesn't exist. You need to concatenate the variable into your query, try this
php
<?php $con2 = mysql_connect("localhost","root",""); mysql_select_db("tester",$con2) or die( "Unable to select database");;
$na = $_GET["name"]; $idea = $_GET["idea"];
//echo $na; //echo $idea;
$ideas = mysql_query("SELECT ideas FROM birthdays WHERE name = '.$na.')or die(mysql_error());
echo $ideas;
Hope that helps
Second, this is more of a PHP question so I'm moving this to the PHP forum
LOL thanks, I didn't get that far with my help. I was trying to show him how to get the variable to work first. That was next, but have to write help code in between work code lol
First to solve your issue. Using the variable isn't working because the way you're including it in your query it is looking for a column named $na, which is assume doesn't exist. You need to concatenate the variable into your query, try this
php
<?php $con2 = mysql_connect("localhost","root",""); mysql_select_db("tester",$con2) or die( "Unable to select database");;
$na = $_GET["name"]; $idea = $_GET["idea"];
//echo $na; //echo $idea;
$ideas = mysql_query("SELECT ideas FROM birthdays WHERE name = '.$na.')or die(mysql_error());
echo $ideas;
Hope that helps
Second, this is more of a PHP question so I'm moving this to the PHP forum
Thanks for the response
I tried this though and it just returns "Resource id #3" for the variable $ideas
Unfortunately I'm such a noob I don't even know what that means
I'm not sure what you're trying to do with the $idea variable, but here's the code to echo the result(s) of your query (with a line break after each result row):
$results = mysql_query("SELECT ideas FROM birthdays WHERE name = '$na'")or die(mysql_error());
while($row = mysql_fetch_assoc($results)) { echo $row['ideas']; // echo the "ideas" of the current result row echo "<br />\n"; // echo a line break (in HTML and source) }
This assumes that "ideas" and "name" are both fields within the "birthdays" table.
Using mysql_fetch_assoc() will give you an associative array of the result row, which will allow you to access the result data by their field names.
So is the point then that the query results are a 1x1 array and not a field as I had hoped?
Is there an easy way to return a single field, or do I need to search my 1x1 array for the field? (as I think is what people are suggesting - although I canīt get it to work...)
Oh- and as for what I was trying to do with the $ideas variable, I was trying to concatenate the new idea (passed through GET function) on the end to make a list, i.e.
Correct (at least in concept). MySQL queries return a resource. To access the data within the returned resource, special functions are used. Check out the PHP manual page for mysql_query, for a description of what to expect from the query. Basically though, here's the steps to getting data: - Connect to the database - Query the database (a result set or false will be returned) - Get the result rows, one row at a time - Get the desired data from the current row
It's also a good idea to do this when you're done:
CODE
mysql_free_result($result); mysql_close();
This will free up the server's resources and close the database connection.
To dump your results into a comma-delimited string, just concatenate everything to a string (instead of echo), and replace the line break with a comma and space. Use if statements to determine if the comma is needed. Here's the revised code:
Correct (at least in concept). MySQL queries return a resource. To access the data within the returned resource, special functions are used. Check out the PHP manual page for mysql_query, for a description of what to expect from the query. Basically though, here's the steps to getting data: - Connect to the database - Query the database (a result set or false will be returned) - Get the result rows, one row at a time - Get the desired data from the current row
It's also a good idea to do this when you're done:
CODE
mysql_free_result($result); mysql_close();
This will free up the server's resources and close the database connection.
To dump your results into a comma-delimited string, just concatenate everything to a string (instead of echo), and replace the line break with a comma and space. Use if statements to determine if the comma is needed. Here's the revised code: