How the SQL comes out of the collection is changed to the desired string 100 points!

Source: Internet
Author: User
Tags php mysql rtrim
Query (' SELECT infotags,id from hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags likes '% i love you% ')

Want to put the ID of the above query out of a string, do not know how to do it
For example, the output of five ID values, respectively, is 1,2,3,4,5
Now I want to convert these five values into such a string
(1,2,3,4,5)
I don't know how to do that.

I'm trying to do it in the following way, but I can't.
$result =query (' SELECT infotags,id from hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags likes '% i love you% ');
$foodsPic =array ();
while ($row =mysql_fetch_array ($result)) {
$foodsPic []= $row;
}

Print_r ($foodsPic);
The above method not only put the ID into the, the other useless also a drum brain into the, I just want to set the ID, and by processing, these several IDs into a string form, that is (1,2,3,4,5)
I don't know how to improve it,


Reply to discussion (solution)

$SQ = "Select Infotags,id from hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags likes '% i love you% '"; $result =mysqli_ Query ($link, $SQ), $idStr = "," if (Mysqli_num_rows ($result)) {    $data =mysqli_fetch_all ($result, MYSQL_ASSOC);    foreach ($data as $value) {        if (isset ($value [' ID ')])            $idStr. = $value [' id '];    }        $idStr =rtrim ($idStr, ', ');    echo $idStr;}

$SQ = "Select Infotags,id from hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags likes '% i love you% '"; $result =mysqli_ Query ($link, $SQ), $idStr = "," if (Mysqli_num_rows ($result)) {    $data =mysqli_fetch_all ($result, MYSQL_ASSOC);    foreach ($data as $value) {        if (isset ($value [' ID ')])            $idStr. = $value [' id '];    }        $idStr =rtrim ($idStr, ', ');    echo $idStr;}


Write a bit or not, but I feel you this idea is correct, but I will not benefit one of my database operation class
For example, one of the functions you used in it was mysqli_fetch_all.
I checked the information as if the function had been banned.
In addition, my system has a database function operation class, post up
 Sql=mysql_query ($query, Return_dblink ($query)) or Die ($ecms _config[' db '] [' ShowError ']==1?mysql_error (). '
'. Str_replace ($GLOBALS [' Dbtbpre '], ' ***_ ', $query): ' Dberror '); return $this->sql;} Execute mysql_query () statement 2function Query1 ($query) {$this->sql=mysql_query ($query, Return_dblink ($query)); return $ This->sql;} Executes the mysql_query () statement (select database use) function Usequery ($query) {global $ecms _config; $this->sql=mysql_query ($query, $ globals[' link ') or Die ($ecms _config[' db '] [' ShowError ']==1?mysql_error (). '
'. Str_replace ($GLOBALS [' Dbtbpre '], ' ***_ ', $query): ' Dberror '), if ($GLOBALS [' linkrd ']) {mysql_query ($query, $GLOBALS [' LINKRD ']);} return $this->sql;} Executes the mysql_query () statement (Operational database) function Updatesql ($query) {global $ecms _config; $this->sql=mysql_query ($query, return _dblink ($query)) or Die ($ecms _config[' db '] [' ShowError ']==1?mysql_error (). '
'. Str_replace ($GLOBALS [' Dbtbpre '], ' ***_ ', $query): ' Dberror '); return $this->sql;} Execute mysql_fetch_array () function Fetch ($SQL)//The parameter of this method is $sql is the SQL statement execution result {$this->r=mysql_fetch_array ($sql); return $ This->r;} Execution Fetchone (mysql_fetch_array ())//The difference between this method and fetch () is: 1, the parameter of this method is $query is the SQL statement//2, this method is used while (), the for () database pointer does not automatically move down, Fetch () can be moved down automatically. function Fetch1 ($query) {$this->sql= $this->query ($query); $this->r=mysql_fetch_array ($this->sql); return $this->r;} Execute mysql_num_rows () function num ($query)//The parameter of this class is $query is the SQL statement {$this->sql= $this->query ($query); $this Num=mysql_num_rows ($this->sql); return $this->num;} Execution Numone (mysql_num_rows ())//The difference between this method and Num () is: 1, the parameter of this method is $sql is the result of the SQL statement execution. function Num1 ($sql) {$this->num=mysql_num_rows ($sql); return $this->num;} Execute Numone (mysql_num_rows ())//statistic record number function gettotal ($query) {$this->r= $this->fetch1 ($query); return $this- >r[' total ';} Execute free (mysql_result_free ())//The parameter of this method is $sql is the result of the SQL statement execution. Use function free ($sql) {my) only in case of use of mysql_fetch_arraySql_free_result ($sql);} Execute Seek (Mysql_data_seek ())//The parameter of this method is $sql is the execution result of the SQL statement, $pit the offset number of the execution pointer function seek ($sql, $pit) {Mysql_data_seek ($sql , $pit);} Execution ID (mysql_insert_id ()) function LastID ()//Get last execution of MySQL database ID number {$this->id=mysql_insert_id ($GLOBALS [' link ']); if ($this->id<0) {$this->id= $this->gettotal (' SELECT last_insert_id () as Total ');} return $this->id;} Returns the number of affected (mysql_affected_rows ()) function affectnum ()//number of records affected after obtaining operation data table {return mysql_affected_rows ($GLOBALS [' Link '] );}}? >

Can you use the method here, for example, if
Instantiate this class first
$empire =news mysqlquery ();
And then how do you rewrite it with the $empire-> method?

$SQ = "Select Infotags,id from hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags likes '% i love you% '"; $result =mysqli_ Query ($link, $SQ), $idStr = "," if (Mysqli_num_rows ($result)) {    $data =mysqli_fetch_all ($result, MYSQL_ASSOC);    foreach ($data as $value) {        if (isset ($value [' ID ')])            $idStr. = $value [' id '];    }        $idStr =rtrim ($idStr, ', ');    echo $idStr;}


 
  Query (' SELECT infotags,id from hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags like '% spot promotion product% ');// Query the Infotags field under the Sub-table $idstr= '; if ($empire->num ($sqld)) {    $data = $empire->fetch ($sqld);    foreach ($data as $key = + $value) {        if (isset ($value [' ID ')])            $idStr. = $value [' id '];    }        $idStr =rtrim ($idStr, ', ');    echo $idStr;}? >

I changed it, but I didn't succeed, I don't know what's wrong

$SQ = "Select Infotags,id from hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags likes '% i love you% '"; $result =mysqli_ Query ($link, $SQ), $idStr = "," if (Mysqli_num_rows ($result)) {    $data =mysqli_fetch_all ($result, MYSQL_ASSOC);    foreach ($data as $value) {        if (isset ($value [' ID ')])            $idStr. = $value [' id '];    }        $idStr =rtrim ($idStr, ', ');    echo $idStr;}


I wrote it, and it came out, but it was all messed up. Some numbers are connected.
$sqld = $empire->query (' SELECT infotags,id from hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags like '% i love you% ' )//infotags field under Query sub-table
$idjh =array ();
while ($bqrd = $empire->fetch ($sqld)) {
if ($bqrd [id])
$idStr. = $bqrd [id];
$idStr =rtrim ($idStr, ', ');
Echo $idStr;

The code misled you. Modify the  $idStr. = $bqrd [id]. ', ';} $idStr =rtrim ($idStr, ', ')

In addition Mysqli_fetch_all is PHP MySQL database driver is not to be eliminated, only the server needs to open this support. Some server environments may not support such as sea ... You can easily get this support for your own compiled server

In addition Mysqli_fetch_all is PHP MySQL database driver is not to be eliminated, only the server needs to open this support. Some server environments may not support such as sea ... You can easily get this support for your own compiled server



Does this code change to a while without a foreach form?
$SQ = "Select Infotags,id from hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags likes '% i love you% '";
$result =mysqli_query ($link, $SQ);
$IDSTR = ";
if (Mysqli_num_rows ($result)) {
$data =mysqli_fetch_all ($result, MYSQL_ASSOC);
foreach ($data as $value) {
if (Isset ($value [' ID ']))
$idStr. = $value [' id '];
}

$idStr =rtrim ($idStr, ', ');
Echo $idStr;
}

$SQ = "Select Infotags,id from hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags likes '% i love you% '"; $result =mysqli_ Query ($link, $SQ), $idStr = "," if (Mysqli_num_rows ($result)) {    $data =mysqli_fetch_all ($result, MYSQL_ASSOC);    foreach ($data as $value) {        if (isset ($value [' ID ')])            $idStr. = $value [' id '];    }        $idStr =rtrim ($idStr, ', ');    echo $idStr;}



Can you change this to the while form, and then you can add points after the change?

Hava a try   SELECT group_concat (infotags SEPARATOR ",") as Infotags,id from Hd_ecms_shop_data_1 WHERE hd_ecms_shop_d Ata_1.infotags like '% i love you% ' GROUP by infotags

Can query out the array of IDs and then use implode to deal with Ah, why do you have to use SQL query to deal with?

Can query out the array of IDs and then use implode to deal with Ah, why do you have to use SQL query to deal with?


How to Write, Uncensored no truth

It's a question of basic skills. while ($row =mysql_fetch_array ($result)) {    $id []= $row [' id '];} echo Join (",", $id);

It's a question of basic skills. while ($row =mysql_fetch_array ($result)) {    $id []= $row [' id '];} echo Join (",", $id);


On the fairy, ask again
Why
echo $row [id];//Print no value, no error
echo Join (",", $id);//can print out values to

It's a question of basic skills. while ($row =mysql_fetch_array ($result)) {    $id []= $row [' id '];} echo Join (",", $id);


I got it wrong, I think it's completely confusing,
Array ([0] = 9 [1] = [2] [3] = [4] [+] = [5] =)

It's a question of basic skills. while ($row =mysql_fetch_array ($result)) {    $id []= $row [' id '];} echo Join (",", $id);


Shang Xian method is good, add points, hope in the future to meet again calendar, do not disappoint our expectations of you!

It's a question of basic skills. while ($row =mysql_fetch_array ($result)) {    $id []= $row [' id '];} echo Join (",", $id);


Well, the method of the immortal good, add points, Shang Xian!
  • Related Article

    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.