Assign mysql record values to gloabl variabel

This is a discussion on Assign mysql record values to gloabl variabel within the PHP Language forums, part of the PHP Programming Forums category; Hi, I've been struggle with the code below. I've tried mysql_fetch_assoc and various other functions, but can't ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-11-2006
Julian Moorhouse
 
Posts: n/a
Default Assign mysql record values to gloabl variabel

Hi,

I've been struggle with the code below.
I've tried mysql_fetch_assoc and various other functions, but can't
seem to get it to work.
The code works as far as that point though.

Any suggestions?

Jules.

global $first_name;
global $last_name;
global $password;

function get_acct_rec($email){

$conn = db_connect(HOST, USER, PASS, DB, PORT);

$sql = "SELECT * FROM `clients` WHERE `email` = '" . $email . "';";

$c_result = mysql_query($sql, $conn) or die("Failed to execute query
at line " . __LINE__ . ": " . db_error($conn));


while ($row = mysql_fetch_assoc($c_result)) {
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$password = $row['password'];
echo $row['first_name'];
}

mysql_free_result($result);

db_close($conn);

}

Reply With Quote
  #2 (permalink)  
Old 07-11-2006
Mel
 
Posts: n/a
Default Re: Assign mysql record values to gloabl variabel

On 2006-07-11 23:47:26 +1000, "Julian Moorhouse" <mindwarpltd@gmail.com> said:

> Hi,
>
> I've been struggle with the code below.
> I've tried mysql_fetch_assoc and various other functions, but can't
> seem to get it to work.
> The code works as far as that point though.
>
> Any suggestions?
>
> Jules.
>
> global $first_name;
> global $last_name;
> global $password;
>
> function get_acct_rec($email){
>
> $conn = db_connect(HOST, USER, PASS, DB, PORT);
>
> $sql = "SELECT * FROM `clients` WHERE `email` = '" . $email . "';";
>
> $c_result = mysql_query($sql, $conn) or die("Failed to execute query
> at line " . __LINE__ . ": " . db_error($conn));
>
>
> while ($row = mysql_fetch_assoc($c_result)) {
> $first_name = $row['first_name'];
> $last_name = $row['last_name'];
> $password = $row['password'];
> echo $row['first_name'];
> }
>
> mysql_free_result($result);
>
> db_close($conn);
>
> }


Have you tried dumping out the SQL statement and running the SQL
directly on the database server? If that's okay, var_dump the $conn
variable to make sure it's a valid connection object. If it is, try
var_dump'ing the $row variable from inside the loop to see what it's
being populated with. Sorry if these suggestions seem a little obvious.

Reply With Quote
  #3 (permalink)  
Old 07-11-2006
Rik
 
Posts: n/a
Default Re: Assign mysql record values to gloabl variabel

Julian Moorhouse wrote:
> Hi,
>
> I've been struggle with the code below.
> I've tried mysql_fetch_assoc and various other functions, but can't
> seem to get it to work.
> The code works as far as that point though.
>
> global $first_name;
> global $last_name;
> global $password;


If you want to make them of use inside the function, you have to declare
themn global inside the function...

> function get_acct_rec($email){
>
> $conn = db_connect(HOST, USER, PASS, DB, PORT);
>
> $sql = "SELECT * FROM `clients` WHERE `email` = '" . $email . "';";
>
> $c_result = mysql_query($sql, $conn) or die("Failed to execute query
> at line " . __LINE__ . ": " . db_error($conn));


What's the output if you return mysql_num_rows() here? It might be a problem
in the database (such as fieldnames, no matching records etc.).

> while ($row = mysql_fetch_assoc($c_result)) {
> $first_name = $row['first_name'];
> $last_name = $row['last_name'];
> $password = $row['password'];
> echo $row['first_name'];
> }


Seems OK, but won't do a thing besides echoing first_name, see the previous
remark about globals.

Grtz,
--
Rik Wasmus


Reply With Quote
  #4 (permalink)  
Old 07-11-2006
David Haynes
 
Posts: n/a
Default Re: Assign mysql record values to gloabl variabel

Julian Moorhouse wrote:
> Hi,
>
> I've been struggle with the code below.
> I've tried mysql_fetch_assoc and various other functions, but can't
> seem to get it to work.
> The code works as far as that point though.
>
> Any suggestions?
>
> Jules.
>
> global $first_name;
> global $last_name;
> global $password;
>
> function get_acct_rec($email){
>
> $conn = db_connect(HOST, USER, PASS, DB, PORT);
>
> $sql = "SELECT * FROM `clients` WHERE `email` = '" . $email . "';";
>
> $c_result = mysql_query($sql, $conn) or die("Failed to execute query
> at line " . __LINE__ . ": " . db_error($conn));
>
>
> while ($row = mysql_fetch_assoc($c_result)) {
> $first_name = $row['first_name'];
> $last_name = $row['last_name'];
> $password = $row['password'];
> echo $row['first_name'];
> }
>
> mysql_free_result($result);
>
> db_close($conn);
>
> }
>


Does the query work from the mysql command prompt?
Try putting echo "$sql\n"; after your $sql = line and cut and paste that
into a mysql prompt.

Also, it should probably be mysql_free_result($c_result);

Adding print_r($row) after the while() would show if you ever enter
the loop.

-david-

Reply With Quote
  #5 (permalink)  
Old 07-11-2006
Julian Moorhouse
 
Posts: n/a
Default Re: Assign mysql record values to gloabl variabel

Hi,

Thanks, it was a combinatrion of those things mentioned.

Jules.

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 03:21 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0