There is a need to make a function to get data from the database before the main query, then to substitute values ​​from another table.

Inventing a bicycle, something like that

function useraccess() { mysql_connect($host, $user, $pass); mysql_select_db($base); q = mysql_query("SELECT `accesslevel` FROM accounts WHERE login = " . $res['commentautor'] . ""); res = mysql_fetch_assoc($q); $accesslevel = res['accesslevel']; } 

I tried to call, too, in different ways ....

Climbed half the guide, queries on the "php function" show the results on simple functions, I have not found any examples of working with the database. (

  • Please specify what exactly you want from this code and what exactly you can't do because I personally did not understand at all ... - Zowie
  • I want from the database in the right places to pull the value of accesslevel. if the login at output is equal to the already displayed name of the author - DemoriaN
  • Be so kind, please explain what the value of the <b> commentautor </ b> lies in? Or you do not know how to make the function return something? O_O - Zowie
  • commentautor is obtained from the request to the database which is executed before this. Yes, I can’t figure out how to make the function return the value I need, because I can’t figure out the functions. - DemoriaN
  • can be closed) - DemoriaN

3 answers 3

Generally concluded that you do not know about the return, it's terrible ... Rejoice :)

For example:

 function getAll() { $res = mysql_query('SELECT ........'); $RETURN = mysql_fetch_assoc($res); return $RETURN; } 

Example of use:

  $all = getAll(); // теперь в $all у нас лежит $RETURN функции getAll... 

Specifically for your case:

  // данные функции достаточно вызвать единожды .... mysql_connect($host,$user,$pass); mysql_select_db($base); function getAccess($user) { $q=mysql_query('SELECT "accesslevel" FROM accounts WHERE login=' . $user); $res=mysql_fetch_assoc($q); $accessLevel=res['accesslevel']; return $accessLevel; } 

And use accordingly:

 $userAccess = getAssecs($res['commentautor']); // теперь в $userAсcess у нас будет результат выборки .... 

PS: the worst thing is that when I taught php I also came across something like this, therefore there is no criticism from my side of how

  • I'll test it, I'm actually in shock ... I basically use PCP for 3 years somewhere .. and have never seen a more or less decent description of functions and their work ... even in books for big money, which I have at home there are a couple of pieces ... they do not describe such issues anywhere, or there is a blah blah blah, we have a function further, which is not important for you and how it is .... - September
  • Updated, if interestno tomorrow afternoon add skype <b> alexwindhope </ b> there I can tell you more;) - Zowie
  • some kind of nonsense ... returns Array if there is a SELECT accessLevel FROM account WHERE login = 'demorian', that is, then echo $ all; displays the Array can not be. in the database one such login. - DemoriaN
  • This can not be because. Only 1 row of the sample is processed, even if such logins would be a bit like this should not be - Zowie
  • everyone has their drawbacks, I write from time to time .. now I have to write the satik myself and not in Jumla in a couple of minutes that the thread should be twisted. what you just rewrote yourself and did, added $ return ['acesslevel'], thanks for the help) and you can never chew everything completely, people should think more, not ready to use) - DemoriaN
 function db_query($query){ if(!$link = mysql_connect("db_host", "db_user", "db_pass")){ echo "<br>Не могу соединиться с сервером базы данных<br>"; exit(); } if(!mysql_select_db($db_name, $link)){ echo "<br>Не могу выбрать базу данных<br>"; exit(); } mysql_query('SET NAMES utf8'); $set_query = mysql_query($query); if($set_query){ return $set_query; } else { return false; } } 

And here is how to retrieve from the database:

 $data = db_query("SELECT * FROM `table_name`"); 
  • Would you even read the question chtola -.- - Zowie
  • $link will always be false + E_WARNING - Sh4dow
  • Not understood ?? :)) How frightened would he be? :)))) That's curious :) Explain, please, oh Great! :) - Stanislav Komar
  • general entry! $ link = mysql_connect ("db_host", "db_user", "db_pass") is scary -.- - Zowie
  function useraccess($host, $user, $pass, $base) { mysql_connect($host, $user, $pass); mysql_select_db($base); q = mysql_query("SELECT `accesslevel` FROM `accounts` WHERE login = '" . $res['commentautor'] . "'"); res = mysql_fetch_assoc($q); $accesslevel = res['accesslevel']; return $accesslevel; } 

It is necessary now, when using the function, to constantly turn on the host, user, pass, and so on, because the function does not tackle this, it needs to be done separately. And do not forget about the visibility of the function, all that lies in the function is only visible in it, and therefore it is necessary that the function return something — something that will be visible outside the function.


I wrote a working version of your code, because it isn’t written at all (I’m talking about connecting to the database directly in the function and with the select). And by the way, I wrote that I had to throw it out so that it was correct and competent.

 function useraccess($login) { $sql = mysql_query("SELECT `accesslevel` FROM `accounts` WHERE login = '{$login}'"); $result = mysql_fetch_assoc($sql); $accesslevel = res['accesslevel']; return $accesslevel; } 

Minimal amendments and this crap does the same as above. And what additional data?

  • Well, if the answer is already accepted, what for write, especially, what for I will pull additional data if I need only an access level ?? This is a function for news, so that the commentator has a usergroupicon))) and nothing more)) do not read, then provide "disservice" well, why do so .. I do not understand .. - - DemoriaN