Crosswords0 min ago
SQL Query To PHP Variable
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
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
Answers
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 demonstratio n 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 =...
$table = "
$
$
$
23:02 Tue 25th 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.
$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.
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.