tags:

views:

66

answers:

3

Hello,

I am using a page where a variable $submissionid is being posted to it. I would like to use this variable and pull the field subcheck from a MySQL table called submission. I would like the value of the field subcheck to simply be a new variable $subcheck. I'm not sure how to do this. I have a query below, but how to I convert the result of the query below into a variable called $subcheck? (For any given submissionid, there will only be one $subcheck.)

Thanks in advance,

John

$querysub = mysql_query("SELECT subcheck FROM submission WHERE submissionid = '$submissionid' ");
mysql_query($querysub) or die(mysql_error());
A: 

You can try:

$querysub = mysql_query("SELECT subcheck FROM submission WHERE submissionid = ".
                                   mysql_real_escape_string($submissionid));
$result = mysql_query($querysub);
if (!$result) {
    die 'Could not run query: ' . mysql_error();
}

$subcheck = mysql_result($result, 0);
codaddict
The last two lines could be replaced by $subcheck = mysql_result($result, 0);
Victor Nicollet
@Victor: Thanks for pointing.
codaddict
@Victor it could be but it shouldn't be. if you want to make it shorter - make a helper function out of this code and make it just one line call. But do not use odd functions for shortening. it will make your code not nice but ugly
Col. Shrapnel
@Col. Shrapnel: I'd rather use a function that, albeit obscure, is well-documented in the PHP manual, than define my own version of it for other developers to puzzle upon. Besides, mysql_result conveys the intent (read one element) better than using mysql_fetch_assoc.
Victor Nicollet
@Victor both you and developers you mention, have to learn programming. user-defined functions ARE for this. Not to replace this only one mysql_result but to replace whole snippet of extremely repetitive lines of code. This could be a real enhancement. think of it.
Col. Shrapnel
btw, -1 for using die. In hope that someday SO will become a real source of knowledge, not ancient practices mindlessly repeated again and again for ages.
Col. Shrapnel
A: 

This is more of a 'php' question, than it is for mysql.

Look up the 'extract' keyword for PHP Link. Effectively 'extract' takes the contents of an associative array and creates php variables (symbol table entries) using the names of keys. Each php variable will then contain the associated value.

You should be able to just:

   $result = mysql_query("SELECT * FROM table");
   $row    = mysql_fetch_array( $result, MYSQL_ASSOC );
   extract( $row );  // Create php variables, named after each column in the table.

   $row["field"] == $field;  // Will be a true statement after 'extract()'

Enjoy, you now have the ability to have your code dynamic adjust to a DB schema that could be changed.

-- J Jorgenson --

J Jorgenson
This will definitely work, but I would suggest only doing something like this in those parts of your code where you're prepared to deal with the new variables, such as in a database setup class. Sprinkling this throughout your code will almost certainly make the code harder to read and maintain, and may make it easier to introduce security vulnerabilities. It will also make it impossible for the code-completion functionality in editors such as NetBeans or PhpEd to auto-complete your variables for you.
mr. w
A: 

This should work:

$querysub = mysql_query("SELECT subcheck FROM submission WHERE submissionid = '" . $submissionid ."' ");
$result = mysql_query($querysub) or die(mysql_error());
$row = mysql_fetch_assoc( $result );
if ($row ) {
   $subcheck = $row['subcheck'];
} else {
    echo "Subcheck not found"; 
}

Be careful with the escape characters around $submissionid in your query string. In your sample, they are probably letting the name of the variable go into the string you send to the mysql server.

CarlesAndres