Php function inside mysql query?

I have this request

$query = "INSERT INTO $new_db.wp_users (user_login, user_pass, user_nicename) select user_email, md5(user_password), user_name from $source_db.users"; 

and i have this function

 function wp_hash_password($password) { require_once('class-phpass.php'); // By default, use the portable hash from phpass $wp_hasher = new PasswordHash(8, TRUE); return $wp_hasher->HashPassword($password); } 

I need the request to be like this

 $query = "INSERT INTO $new_db.wp_users (user_login, user_pass, user_nicename) select user_email, ". wp_hash_password(user_password) .", user_name from $source_db.users"; 

but it doesn’t work ... any ideas

+4
source share
5 answers

You have 2 options:

  • rewrite the wp_hash_password function in SQL / PL and use it in the query
  • load the result in PHP - change and send them with the code as follows:
  $ results = mysql_query ("SELECT user_login, user_pass, user_nicename FROM $ source_db.users");

 while ($ row = mysql_fetch_assoc ($ results)) {
   $ sql = "$ query =" INSERT INTO $ new_db.wp_users (user_login, user_pass, user_nicename) VALUES (";
   $ sql. = $ row ['user_login'].  ',';
   $ sql. = wp_hash_password ($ row ['user_password']).  ',';
   $ sql. = $ row ['user_nicename;
   $ sql. = ')';
   mysql_query ($ sql);
 } 
+4
source

You need to split the query into two queries. First execute a SELECT query, run your function in the corresponding column in the data you received, and then run the last INSERT query.

Note that you will need to do this one line at a time (or maybe in pieces) in a loop; You do not want to load the entire table into memory.

+2
source

You can not. Thus, MySQL cannot interact with PHP. Only MySQL functions are available.

You need to write a PHP script that does this using iteration.

+2
source

Update . I think I misunderstood what you were trying to do (thanks to Peter Bailey for the tip).

You need to wrap the result of this function call in single quotes when it is inserted into your query so that it is interpreted as a string in your query, and not a column reference:

 $query = "INSERT INTO $new_db.wp_users (user_login, user_pass, user_nicename) select user_email, '" . wp_hash_password($user_password) . "', user_name from $source_db.users"; 
+1
source

I created a simple php function to use MySQL queries.

Any request can be made in 1 simple function.

If you select a query, we can get the selected arguments, because the variable name contains the selected parameter value.

For ex:

 <?php q("select user_name,email_id from users where user_id=48"); echo $user_name; echo "<br>"; echo $email_id; ?> 

or you can set your own variable name by setting "how"

 <?php q("select user_name as uname, email_id as email from users where user_id=48"); echo $uname; echo "<br>"; echo $email; ?> 

output result will be:

  someuser someemail 

If more rows were selected, the variable name will be created as an array for ex:

 <?php q("select user_name,user_id from users"); for($n=0;$n<count($user_name);$n++) { if(count($user_name)==1) // if single row is selected { $username_val=$user_name; $user_ids=$user_id; }else{ $username_val=$user_name[$n]; // for multiple rows selected $user_ids=$user_id[$n]; } echo $username; } ?> 

or you can set your own variable name by setting "how"

 <?php q("select user_name as un,user_id as uid from users"); for($n=0;$n<count($user_name);$n++) { if(count($user_name)==1) // if single row is selected { $username_val=$un; $user_ids=$uid; }else{ $username_val=$un[$n]; // for multiple rows selected $user_ids=$uid[$n]; } echo $username_val; echo " "; echo $user_ids; echo "<br>"; } ?> 

The output will be: (If the user table has three rows)

 User1 4043 User2 4048 User3 4056 

Create mysql Connection ex file: mysql_connect_file.php

 <?php $dbc=new mysqli('localhost', 'my_user', 'my_password', 'my_db'); ?> 

Php function below

 <?php require_once './mysql_connect_file.php'; function q($q) { $main_q=$q; $q= strtolower($q); global $dbc; $temp=$q; $temp=str_replace(" ", "", $temp); $temp= strtolower($temp); $temp=".$temp"; if(strpos($temp, "update")==1 || strpos($temp, "insert")==1 || strpos($temp, "delete")==1 || strpos($temp, "alter")==1 || strpos($temp, "create")==1) { $rd2= mysqli_query($dbc,$main_q); if($rd2) { return TRUE; } else{ $mysql_err= mysqli_error($dbc); $err= debug_backtrace(); $err_line=$err[0]['line']; $err_file=$err[0]['file']; echo "<font color='black'>Error at <b>$err_file on line $err_line </b>query --></font><font color='maroon'>$main_q</font> (<font color='red'> $mysql_err </font> )"; return FALSE; } }elseif(strpos($temp, "select")==1){ $qn= str_replace("select ", "", $q); $qn=substr($qn,0, strpos($qn, " from")); $qn="$qn,"; $selc= str_replace("`","", $qn); $qn= str_replace("`","", $qn); $my_var=array(); $my_nm=array(); for($m=1;$m<=substr_count($selc, ',');$m++) { $my_nm[$m]=substr($qn,0, strpos($qn, ",")); $qn=substr($qn,strpos($qn, ",")+1, strlen($qn)); if(strpos($my_nm[$m]," as ")>0) { $my_var[$m]= str_replace(" as ", "~", $my_nm[$m]); $my_var[$m]= str_replace(" ", "", $my_var[$m]); $my_var[$m]=substr($my_var[$m],strpos($my_var[$m],"~")+1,strlen($my_var[$m])); }else { $my_var[$m]=substr($my_nm[$m],0, strlen($my_nm[$m])); $my_var[$m]= str_replace(" ","", $my_var[$m]); } } $rn=mysqli_query($dbc, $main_q); if($rn) { if(mysqli_num_rows($rn)>0) { for($t=1;$t<=count($my_var);$t++) { $$my_var[$t]=array(); } while($row=mysqli_fetch_array($rn,MYSQLI_ASSOC)) { if(mysqli_num_rows($rn)>1) { for($t=1;$t<=count($my_var);$t++) { ${$my_var[$t]}[]=$row[$my_var[$t]]; } }else{ for($t=1;$t<=count($my_var);$t++) { $$my_var[$t]=$row[$my_var[$t]]; } } } if(mysqli_num_rows($rn)>1) { for($t=1;$t<=count($my_var);$t++) { $GLOBALS[$my_var[$t]]= sel_mr($my_var,$$my_var[$t]); } for($t=1;$t<=count($my_var);$t++) { return $$my_var[$t]; } } if(mysqli_num_rows($rn)==1) { for($t=1;$t<=count($my_var);$t++) { $GLOBALS[$my_var[$t]]=$$my_var[$t]; } for($t=1;$t<=count($my_var);$t++) { return $$my_var[$t]; } } }else { for($t=1;$t<=count($my_var);$t++) { $GLOBALS[$my_var[$t]]=NULL; } for($t=1;$t<=count($my_var);$t++) { return $my_var[$t]; } } }else { for($t=1;$t<=count($my_var);$t++) { $my= mysqli_error($dbc); if($t==1) { $err= debug_backtrace(); $err_line=$err[0]['line']; $err_file=$err[0]['file']; echo "<font color='#ef0000'>Error at <b>$err_file on line $err_line </b>query --></font><font color='maroon'>$q</font> (<font color='red'> $my </font> )"; } } for($t=1;$t<=count($my_var);$t++) { for($p=0;$p<count($$my_var[$t]);$p++) { $a=$$my_var[$t]; return $a; } } } } } function sel_mr($a,$ab) { for($t=1;$t<=count($a);$t++) { foreach ($ab as $my) { ${$a[$t]}[]=$my; } } for($t=1;$t<=count($a);$t++) { return $$a[$t]; } } ?> 

Notes:

You can save this code to a file, then you can call this function by specifying this file name

for ex: if your q.php file name (-> contains q function) then you can use the code for other files by including

 <?php include 'q.php'; q("select user_name from users where user_id=4048"); echo $user_name; ?> 
+1
source

Source: https://habr.com/ru/post/1336358/


All Articles