/    Sign up×
Community /Pin to ProfileBookmark

Mysql Query Results Problem

Hi I have look for this and just cant figure it out (Newbie).

I am using this code to query my MYSQL DB, the query string works, but how do I get the results into a variable? or echo them out to the screen?

I know this is probly really simple but its driving me nuts

[code=php]

$theRef = $_GET[‘ref’];

$DBhost = “100.100.100.101”;
$DBuser = “user”;
$DBpass = “pass”;
$DBName = “DB_Name”;

$table = “table_name”;

$field1 = “field1”;
$field2 = “field2”;

//Connecting to DB
mysql_connect($DBhost,$DBuser,$DBpass) or die(“Unable to connect to database”);

//Selecting Databse
@mysql_select_db(“$DBName”) or die(“Unable to select database $DBName”);

//Create the MySQL Query String
$stmt = “SELECT $field1 FROM $table where $field2 =’$theRef'”;

//Run Query
$results = mysql_query($stmt) or die(mysql_error());

$row = mysql_fetch_assoc($results);

echo(“Row: $row <br>”);

//Close DB Connection
mysql_close();

[/code]

This is not working for me I get the value: Row: array

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@bokehOct 12.2006 — [code=php]echo("Row: {$row[$field1]} <br>");[/code]Also you need some error checking.[code=php]//Create the MySQL Query String
$stmt = "SELECT $field1 FROM $table where $field2 ='$theRef'";

//Run Query
$results = mysql_query($stmt) or die(mysql_error());

if(mysql_num_rows($results))
{
$row = mysql_fetch_assoc($results);

echo("Row: {$row[$field1]} <br>");
}
else
{
#no result
}

//Close DB Connection
mysql_close();[/code]
And if you are expecting multiple results you need a loop:[code=php]//Create the MySQL Query String
$stmt = "SELECT $field1 FROM $table where $field2 ='$theRef'";

//Run Query
$results = mysql_query($stmt) or die(mysql_error());

if(mysql_num_rows($results))
{
while($row = mysql_fetch_assoc($results))
{
echo("Row: {$row[$field1]} <br>");
}
}
else
{
#no result
}

//Close DB Connection
mysql_close();[/code]
Copy linkTweet thisAlerts:
@Phill_PaffordauthorOct 12.2006 — Thanks that worked, but now how can I get the value into a string format, not an array format?

Let me explain more:

[CODE]
$dir_val = $row[$field1];
[/CODE]


is what I did from your suggestion. I have another part of the script that reads this value as a directory path, but I think its still in the array format value. how do I convert it into a string value?

like if I were just to type:

[CODE]
$dir_val = "/path/to/here";
[/CODE]


Thanks again
Copy linkTweet thisAlerts:
@bokehOct 12.2006 — [code=php]$row[$field1];[/code]That variable contains a string. Either use it like that or save it to another variable if it makes you feel more comfortable:[code=php]$new = $row[$field1];[/code]
Copy linkTweet thisAlerts:
@Phill_PaffordauthorOct 12.2006 — Thanks, Thats what I had. does it have to be $new or can it be $anything ?
Copy linkTweet thisAlerts:
@bokehOct 12.2006 — Any variable name starting with a letter or underscore, followed by any number of letters, numbers, or underscores.
×

Success!

Help @Phill_Pafford spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...