Welcome to Dream.In.Code
Getting PHP Help is Easy!

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!




SELECT query using variable

2 Pages V  1 2 >  
Reply to this topicStart new topic

SELECT query using variable

deej_jinks
29 Sep, 2008 - 10:46 AM
Post #1

New D.I.C Head
*

Joined: 28 Sep, 2008
Posts: 12

Hi there

I've had a bit of a look around, and tried a few things, but I can't seem to get this to work... Can anyone help?

CODE
<?php
$con2 = mysql_connect("localhost","root","");
mysql_select_db("tester",$con2);

$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;


It works fine if I use a fixed value instead of the variable..

I know it'll be something stupid, but I've been stuck on this for a while now sad.gif

Any help much appreciated
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: SELECT Query Using Variable
29 Sep, 2008 - 01:08 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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 smile.gif

Second, this is more of a PHP question so I'm moving this to the PHP forum smile.gif
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: SELECT Query Using Variable
29 Sep, 2008 - 01:29 PM
Post #3

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 580



Thanked: 59 times
Dream Kudos: 50
My Contributions
You need to extract the data from the resultset.
php
$ideas = mysql_query("SELECT ideas FROM birthdays WHERE name = '.$na.')or die(mysql_error());
$i = 1;
while (($row = mysql_fetch_row) !== FALSE)
{
echo "Idea $i: {$row[0]}\n");
++$i;
}

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: SELECT Query Using Variable
29 Sep, 2008 - 01:31 PM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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
User is offlineProfile CardPM
+Quote Post

deej_jinks
RE: SELECT Query Using Variable
29 Sep, 2008 - 01:45 PM
Post #5

New D.I.C Head
*

Joined: 28 Sep, 2008
Posts: 12

QUOTE(PsychoCoder @ 29 Sep, 2008 - 02:08 PM) *

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 smile.gif

Second, this is more of a PHP question so I'm moving this to the PHP forum smile.gif


Thanks for the response smile.gif

I tried this though and it just returns "Resource id #3" for the variable $ideas sad.gif

Unfortunately I'm such a noob I don't even know what that means sad.gif
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: SELECT Query Using Variable
29 Sep, 2008 - 01:50 PM
Post #6

D.I.C Regular
***

Joined: 8 Aug, 2008
Posts: 383



Thanked: 23 times
My Contributions
QUOTE(JackOfAllTrades @ 29 Sep, 2008 - 02:29 PM) *

php
$ideas = mysql_query("SELECT ideas FROM birthdays WHERE name = '.$na.')or die(mysql_error());
$i = 1;
while (($row = mysql_fetch_row) !== FALSE)
{
echo "Idea $i: {$row[0]}\n");
++$i;
}


You don't need to check if it's not false:
while (($row = mysql_fetch_row) !== FALSE)

You can use:
while ($row = mysql_fetch_row)

wink2.gif

This post has been edited by CTphpnwb: 29 Sep, 2008 - 01:50 PM
User is offlineProfile CardPM
+Quote Post

deej_jinks
RE: SELECT Query Using Variable
29 Sep, 2008 - 02:03 PM
Post #7

New D.I.C Head
*

Joined: 28 Sep, 2008
Posts: 12


Thanks for all the responses guys smile.gif

I still can't get it to work, but I think I'm gonna sleep on it... Maybe it'll make more sense to me tomorrow...
User is offlineProfile CardPM
+Quote Post

DilutedImage
RE: SELECT Query Using Variable
29 Sep, 2008 - 06:56 PM
Post #8

D.I.C Addict
Group Icon

Joined: 20 Nov, 2006
Posts: 642



Thanked: 6 times
Dream Kudos: 25
My Contributions
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):

CODE
$con2 = mysql_connect("localhost","root","");
mysql_select_db("tester",$con2);

$na = $_GET["name"];

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


User is offlineProfile CardPM
+Quote Post

deej_jinks
RE: SELECT Query Using Variable
30 Sep, 2008 - 12:41 AM
Post #9

New D.I.C Head
*

Joined: 28 Sep, 2008
Posts: 12


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.

$ideas = $ideas . ", " . $idea

Any help greatly appreciated smile.gif
User is offlineProfile CardPM
+Quote Post

DilutedImage
RE: SELECT Query Using Variable
30 Sep, 2008 - 09:05 AM
Post #10

D.I.C Addict
Group Icon

Joined: 20 Nov, 2006
Posts: 642



Thanked: 6 times
Dream Kudos: 25
My Contributions
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:
CODE
$con2 = mysql_connect("localhost","root","");
mysql_select_db("tester",$con2);

$na = $_GET["name"];
$idea = $_GET["idea"];
$ideas = '';

$results = mysql_query("SELECT ideas FROM birthdays WHERE name = '$na'")or die(mysql_error());
while($row = mysql_fetch_assoc($results)) {
    if($ideas != '') { $ideas .= ', '; }
    $ideas .= $row['ideas'];
}

mysql_free_result($result);
mysql_close();

if($idea != '') { $ideas .= ', ' . $idea; }
echo $ideas;

I don't have time to test this code right now, but it should all work fine. If it doesn't, then just look for a basic punctuation mistake. smile.gif
User is offlineProfile CardPM
+Quote Post

deej_jinks
RE: SELECT Query Using Variable
30 Sep, 2008 - 10:32 AM
Post #11

New D.I.C Head
*

Joined: 28 Sep, 2008
Posts: 12

QUOTE(DilutedImage @ 30 Sep, 2008 - 10:05 AM) *

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:
CODE
$con2 = mysql_connect("localhost","root","");
mysql_select_db("tester",$con2);

$na = $_GET["name"];
$idea = $_GET["idea"];
$ideas = '';

$results = mysql_query("SELECT ideas FROM birthdays WHERE name = '$na'")or die(mysql_error());
while($row = mysql_fetch_assoc($results)) {
    if($ideas != '') { $ideas .= ', '; }
    $ideas .= $row['ideas'];
}

mysql_free_result($result);
mysql_close();

if($idea != '') { $ideas .= ', ' . $idea; }
echo $ideas;

I don't have time to test this code right now, but it should all work fine. If it doesn't, then just look for a basic punctuation mistake. smile.gif


Well that worked perfectly, thanks!

No I just need to figure out in my head why wink2.gif

Thanks again everyone for all the help...
User is offlineProfile CardPM
+Quote Post

DilutedImage
RE: SELECT Query Using Variable
30 Sep, 2008 - 10:39 AM
Post #12

D.I.C Addict
Group Icon

Joined: 20 Nov, 2006
Posts: 642



Thanked: 6 times
Dream Kudos: 25
My Contributions
Happy to help.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 12:14AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month