Donate SIGN UP

SQL Query To PHP Variable

Avatar Image
jonny707 | 21:37 Tue 25th Jan 2011 | Technology
2 Answers
OK, this is taking me ages to do, and it is really frustrating me.

I want to look up a single cell, and put the information from the query into a variable so it can be printed.

I have searched online for quite a while and nothing is coming up.

Does anyone know how to do this?
Thanks
Gravatar

Answers

1 to 2 of 2rss feed

Avatar Image
Firstly, do you just mean to print a variable on screen? I will assume so. Here's an example I created in PHP/MySQL (simplified for demonstration purposes). You should be able to adapt to your needs as required.

$table = "test";
$query = "SELECT * FROM $table WHERE field1='test' ORDER BY field2";
$result = mysql_query($query);

$data_row =...
00:02 Wed 26th Jan 2011
Firstly, do you just mean to print a variable on screen? I will assume so. Here's an example I created in PHP/MySQL (simplified for demonstration purposes). You should be able to adapt to your needs as required.

$table = "test";
$query = "SELECT * FROM $table WHERE field1='test' ORDER BY field2";
$result = mysql_query($query);

$data_row = mysql_fetch_array($result, MYSQL_ASSOC);

The above will return the first record of data from your query. Then to extract a specific field of data from that record into the variable $test_variable, you can use the following:

$test_variable = $data_row['field1']; (field1 is the name of your field/column)

Then you can do whatever you want with $test_variable.


I hope that's what you're looking for.
Question Author
Yes, that is exactly what I was looking for. Thank You!

1 to 2 of 2rss feed

Do you know the answer?

SQL Query To PHP Variable

Answer Question >>

Related Questions

Sorry, we can't find any related questions. Try using the search bar at the top of the page to search for some keywords, or choose a topic and submit your own question.