5 Replies - 346 Views - Last Post: 02 July 2012 - 01:01 AM Rate Topic: -----

#1 juvef  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-June 12

write function to perform sql query

Posted 30 June 2012 - 04:40 PM

I trying to create function with input. Something like this...

function getvalue($column) {
return mysql_result(mysql_query("SELECT $column FROM table1 WHERE user_id = $user_id"), 0);
}



After that...

echo getvalue('column');

But this don't work. :notify:

This post has been edited by no2pencil: 30 June 2012 - 04:42 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: write function to perform sql query

#2 no2pencil  Icon User is online

  • Original Digital Gansta
  • member icon

Reputation: 4503
  • View blog
  • Posts: 24,971
  • Joined: 10-May 07

Re: write function to perform sql query

Posted 30 June 2012 - 04:45 PM

This assumes some values exist. Try the following with error checking :

function getvalue($column) {
  if(!$column) die("No column provided");
  if(!$user_id) die("User id is not set");
  $sql = SELECT $column FROM table1 WHERE user_id = $user_id";
  $result = mysql_query($sql):
  if(!$result) { 
    echo "The following sql had an error :<br>$sql";
  } else {
    return mysql_result($result);
  }
}
...
echo getvalue('column');


Was This Post Helpful? 1
  • +
  • -

#3 juvef  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-June 12

Re: write function to perform sql query

Posted 30 June 2012 - 09:16 PM

View Postno2pencil, on 30 June 2012 - 04:45 PM, said:

This assumes some values exist. Try the following with error checking :

function getvalue($column) {
  if(!$column) die("No column provided");
  if(!$user_id) die("User id is not set");
  $sql = SELECT $column FROM table1 WHERE user_id = $user_id";
  $result = mysql_query($sql):
  if(!$result) { 
    echo "The following sql had an error :<br>$sql";
  } else {
    return mysql_result($result);
  }
}
...
echo getvalue('column');



I check input values and it's good.

function get_value($user_id, $column) {
	return mysql_result(mysql_query("SELECT $column FROM table1 WHERE user_id = $user_id"));
}

Warning: mysql_result() expects at least 2 parameters... I tried to put ,0 but again get warning.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9161
  • View blog
  • Posts: 33,981
  • Joined: 27-December 08

Re: write function to perform sql query

Posted 30 June 2012 - 10:06 PM

The second parameter is the row being accessed. You should really try and model things after no2pencil's function. I would make a change though. Here:
if(!$result) {
07	    echo "The following sql had an error :<br>$sql";
08	  } else {



Instead of just printing that there was a SQL error, use the mysql_error() function to tell you what the actual error was.
Was This Post Helpful? 0
  • +
  • -

#5 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 590
  • Joined: 12-October 09

Re: write function to perform sql query

Posted 01 July 2012 - 07:38 AM

You need to make sure that your query is actually working BEFORE you try to return the result, as was explained in previous posts, about checking the result of mysql_query and displaying any mysql_error it may have come across.

Personally I wouldn't use mysql_result in this case.

If you do something like:

$sql = "SELECT $column FROM table1 WHERE user_id = $user_id";
$result = mysql_query($sql);
if(!$result) exit('ERROR: ' . mysql_error()); 

$row = mysql_fetch_object($result);

return $row->$column;



That would still do what you want, it would return the column that has been selected.

You can then do a simple print_r() or var_dump() on $row and see if it actually contains what you expect.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2937
  • View blog
  • Posts: 7,690
  • Joined: 08-June 10

Re: write function to perform sql query

Posted 02 July 2012 - 01:01 AM

alternately, a numeric array access (mysql_fetch_row()) would also work.

but given that the old, outdated mysql extension is exactly that, the more modern PDO or MySQLi extensions own methods for that (e.g. PDOStatement->fetchColumn())
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1