Can you use a query code in multiple places?

Source: Internet
Author: User
Tags dsn
$bliang = ' This is a variable ' $sql =select * from a table WHERE cart like '%. $bliang.% ' $query =mysql_query ($sql), while ($row =mysql_fetch_ Array ($query)) {   ...}..}


The top is the query code and then I use it just like this $row [$bliang = ' Category 2 '] so that you can classify the content under 2

$row [$bliang = ' Category 3 '] displays the content under Category 3

How does this need to be achieved?


Reply to discussion (solution)

function foo ($x) {global $con; $sql =select * from a table WHERE cart like '%. $x.% ' mysql_query ($sql, $con);//Some Codereturn $quer Y;}


Reference can be written as a class can be written as a function ...

function foo ($x) {global $con; $sql =select * from a table WHERE cart like '%. $x.% ' mysql_query ($sql, $con);//Some Codereturn $quer Y;}


Reference can be written as a class can be written as a function ...


Moderator adult can give a little more code. My little white is not very good with the amount. What do you do when you look at the output of a query method?

function foo ($x) {global $con; $sql =select * from a table WHERE cart like '%. $x.% ' mysql_query ($sql, $con);//Some Codereturn $quer Y;}


Reference can be written as a class can be written as a function ... I do not understand the same, is not to include the next file and then call the Foo method? return returns $query

Write a function Ah, I also just learned PHP, I give you a I do the class reference, all database operations are here

 DSN, $this->user, $this->pass);  $DBH->query ($this->names);  Return $DBH->query ($sql);  $DBH = null;  } catch (Exception $e) {echo ' ERROR: '. $e->getmessage (); }}//Manipulate a single data (update/delete/insert), no return result public function Sql_one ($sql) {try {$dbh = new PDO ($this->dsn, $this->user, $thi  S->pass);  $DBH->exec ($this->names);  $DBH->exec ($sql);  $DBH = null;  } catch (Exception $e) {echo ' ERROR: '. $e->getmessage (); }}//Operation multiple data (update/delete), no return result public function Sql_more ($sql, $str) {try {$dbh = new PDO ($this->dsn, $this->user,  $this->pass);  $DBH->exec ($this->names);  foreach ($str as $arrs) {$DBH->exec ($sql. $arrs);  } $DBH = null;  } catch (Exception $e) {echo ' ERROR: '. $e->getmessage (); }}//Parameterized Query database returns results (single) public function Cs_sql_select ($sql, $str) {try {$dbh = new PDO ($this->dsn, $this->use  R, $this->pass);  $DBH->setattribute (Pdo::attr_emulate_prepares, false);  $DBH->query ($this->names);$stmt = $dbh->prepare ($sql);  $stmt->bindvalue (": Key", $str, PDO::P aram_int);  $stmt->execute ();  return $stmt;  $DBH = null;  } catch (Exception $e) {echo ' ERROR: '. $e->getmessage (); }}//Parameterized query operation multiple data (delete/update), no return result public function Cs_sql_more ($sql, $str) {try {$dbh = new PDO ($this->dsn, $this-  >user, $this->pass);  $DBH->setattribute (Pdo::attr_emulate_prepares, false);  $DBH->exec ($this->names);  foreach ($str as $arrs) {$stmt = $dbh->prepare ($sql);  $stmt->bindvalue (": Key", $arrs, PDO::P aram_int);  $stmt->execute ();  } $DBH = null;  } catch (Exception $e) {echo ' ERROR: '. $e->getmessage (); }}//Parameterized query operation single data (delete/update), no return result public function Cs_sql_one ($sql, $str) {try {$dbh = new PDO ($this->dsn, $this-&gt  ; user, $this->pass);  $DBH->setattribute (Pdo::attr_emulate_prepares, false);  $DBH->exec ($this->names);  $stmt = $dbh->prepare ($sql);  $stmt->bindvalue (": Key", $str, PDO::P aram_int); $stmt->execute ();  $DBH = null;  } catch (Exception $e) {echo ' ERROR: '. $e->getmessage (); }}////////}?>

Here's how to call it:
 
  Sql_one ("DELETE from ' user_type ' WHERE ' user_id ' =". $_request[' id ']. "); /no Parameterized query operation $mysql->cs_sql_one ("DELETE from ' user_type ' WHERE ' user_id ' =: Key ', $_request[' id ']);//Parameterized Query operation}else{echo ' Illegal operation ';}} Multi-Select Delete else if ($_request["action"] = = ' Delcheck ') {$id =$_post[' Delall '];if (isset ($id)) {//$mysql->sql_more (" Delete from ' User_type ' where ' user_id ' = ", $id);//parameterless query Operation $mysql->cs_sql_more (" DELETE from ' user_type ' where ' user_ ID ' =: key ', $id);//Parameterized query operation}}//Read Database?>          
 
  


function foo ($x) {global $con; $sql =select * from a table WHERE cart like '%. $x.% ' mysql_query ($sql, $con);//Some Codereturn $quer Y;}


Reference can be written as a class can be written as a function ...


Moderator adult can give a little more code. My little white is not very good with the amount. You seem to be the method to query the output of the results of what to do when the return is the output.

Write a function Ah, I also just learned PHP, I give you a I do the class reference, all database operations are here

That's what I mean.

$b = ' This is a variable ' $sql =select * from a table WHERE cart like '%. $b.% ' $query =mysql_query ($sql), while ($row =mysql_fetch_array ($query) {    $new = "$row [' title ']."-". $row [' con ']."-". $row [' num '].";}


It's code, and then I just need to use it.
Category 1-Content 1-5

Category 2-Content 2-7

That's what I'm talking about. The code above must not be written that way.


Write a function Ah, I also just learned PHP, I give you a I do the class reference, all database operations are here

That's what I mean.

$b = ' This is a variable ' $sql =select * from a table WHERE cart like '%. $b.% ' $query =mysql_query ($sql), while ($row =mysql_fetch_array ($query) {    $new = "$row [' title ']."-". $row [' con ']."-". $row [' num '].";}


It's code, and then I just need to use it.
Category 1-Content 1-5

Category 2-Content 2-7

That's what I'm talking about. The code above must not be written that way.

is to write the query database code into a function ah, directly into the variable B call soon line

$new = "$row [' title ']."-". $row [' con ']."-". $row [' num '].";
Rewritten as
$new [$row [' title ']] = $row [' title ']. "-". $row [' con ']. "-". $row [' num '];

function foo ($x) {global $con; $sql =select * from a table WHERE cart like '%. $x.% ' mysql_query ($sql, $con);//Some Codereturn $quer Y;}

function foo ($x) {global $con; $sql =select * from a table WHERE cart like '%. $x.% ' mysql_query ($sql, $con);//Some Codereturn $quer Y;}


Reference can be written as a class can be written as a function ...

$tagxs _lx= "SELECT * from". Db_musicdata. "LEFT join". Db_singer. "On". Db_musicdata. ". Singer_id= ". Db_singer. ". Singer_id left join ". Db_tag. "On". Db_musicdata. ". Tag_id= ". Db_tag. ". Tag_id and ". Db_tag. ". Tag like '% label 1% ' ORDER by '. Db_musicdata. ". music_id desc LIMIT "; $query _lx=mysql_query ($tagxs _lx); $num =0; $tag 1=" "; while ($row =mysql_fetch_array ($query _lx)) {$num ++;if ($row [' singer_id ']== "") {$singer = "";} else{$singer = '. $row [' Singer_name ']. ';} $tag 1. = '
  • '. sprintf ("%02d", $num). ' '. $row [' Music_name ']. ' '. $singer. ' Join Playlist
  • ';}


    That's the code.

    And then I'm going to be able to call the page directly.

    And then I'm going to be able to call the page directly.

    Retrieve is an array of your this demand against humanity ... Do you want to write all the formatting in the function, which is anti-human design.
    So I think you have to write it down in the same way as your original forehead.

    function can you read the rest of the data is the same as you want to do? If this place is unfamiliar, I suggest you figure out the data structure to be returned ...
    I don't know what to say.

    Find another problem accidentally solved this, come back to make a mark by the way the solution is affixed to keep

    function index ($title, $limit) {$text = "select * from Test wheretag like '% $title% ' ORDER BY test.id desc limit $limit";    return $text;} echo index (' condition ', ' can be multiple ');


    Oh, that's it.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.