Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help moving result of query into variable? 2

Status
Not open for further replies.

Apollo6

Technical User
Jan 27, 2000
418
US
The following query will return a single social security number. I then want to put that into a variable that is used further down in the script. I am pulling from MySQL database using PHP4.3.1. I have been trying to use mysql_fetch_row but without success. The snippet is below:

$my_query1 = "SELECT security.ssn
FROM
security
WHERE
(((security.user_id)='$user_id'))";

$my_result1 = mysql_query ($my_query1)
or die ("Query Failed");

Thanks in advance for any suggestions...
 
I think it should look like this...


$my_query1 = "SELECT security.ssn
FROM
security
WHERE
(((security.user_id)='.$user_id.'))";

$my_result1 = mysql_query ($my_query1)
or die ("Query Failed");


... $my_query1 stays as an array.
 
fetch row is proper
Code:
$row = mysql_fetch_row($my_result1);

$the_variable = $row[0];

in your particular case.

-Rob
 
Sorry for the confiusion, but I hadn't understod your doubt very well, it won't happen again....

skyflyer's tip is the rigth one...

[neutral]
 
There are always four steps in getting something out of a MySQL database:

For connecting
1. connect -> mysql_connect() or mysql_pconnect()
2. select database -> mysql_select_db()

For querying
3. send query -> mysql_query()
4. retreive data -> mysql_fetch_row() or mysql_fetch_array() or mysql_fetch_assoc()

The query (step 3) yields a resource identifier that can be used to retreive the number of rows returned (mysql_num_rows($result)) or the actual data (one record at a time) if rows were returned.

The best way to retreive the data (in my opinion) is mysql_fetch_assoc() which returns an associative array that is keyed by the column name of the table or the aliases created in the SQL statement:
Code:
SELECT COUNT(id) AS num FROM mytable WHERE 1
results in an array where $row[num] equals the count result.

Using the associative array with column name reference keeps IMO the code clean and understandable. Just one short example to demonstrate which is clearer:
Code:
if ($row[0] >= $row[8]+$myVar) .....
...
if ($row[endTime] >= $row[startTime]+$myVar...
 
Thanks for steps... I thought I was on the right track, just took a little refreshing. I do have another question though.

Now that $ss_num contains "#########" (social security number), I need to insert "-" into it.

For example: "###-##-####"

Is there a function to insert characters into a variable or will I need to pull it apart and concat them back together with the "-"?

Thanks again for the help.
 
I suggest to use a regular expression:
Code:
$SSN = "123456789";
$pattern = "/(\d{3})(\d{2})(\d{4})/";
$formatted = preg_replace($pattern,"$1-$2-$3",$SSN);
 
DRJ478-

Thanks for the suggestion. I've managed to break something else so I'll try your suggestion when I fix my current issues.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top