我有三个变量:$ Title,$ Pubdate和$ Link
在这些变量中,有几个这样的值:
$Title = aa, bb, cc, dd, ee, ff
$Pubdate = aa, bb, cc, dd, ee, ff
$Link = aa, bb, cc, dd, ee, ff
喜欢这个。 然后我有一个包含列的表(Title,Pubdate和Link)。 我尝试过:
$sql = "INSERT INTO ytable (Title, Pubdate, Link) VALUES ('$Title', '$Pubdate', '$Link')";
mysql_query($sql,$db_con);
虽然没有弹出错误,但它不会执行我想要的操作。
它将所有值(aa,bb,cc,dd,ee,ff)放入一列(Title)和一行(ID = 1),这意味着所有值都被压缩成一个盒子,而不是我想要的是 :(
我想将它放在一列中,但是在不同的行中(1,2,3,4,5,6)。
我已经在网上搜索了答案,但是如果他们想要一个盒子中的所有值与我想要的相反,那么一切似乎都在回答。
也许是一个接一个,因此INSERT INTO ytable (Title)VALUES($Title);
,它将所有值放在一列中的不同行中。
答案 0 :(得分:3)
您将不得不将字符串拆分为数组,以便您可以“循环”它们。
/**
* @brief Reads TIFF image
* @param path
* @return QImage
*/
QImage EdsImporter::readTiff(const QString &path) const {
// Loads tiff file
TIFF* tiff = TIFFOpen(path.toStdString().c_str(), "r");
if (!tiff) {
QString msg = "Failed to open TIFF: '" + path + "'";
throw new Exception(NULL, msg, this, __FUNCTION__, __LINE__);
}
// Temporary variables
uint32 width, height;
tsize_t scanlength;
// Read dimensions of image
if (TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width) != 1) {
QString msg = "Failed to read width of TIFF: '" + path + "'";
throw new Exception(NULL, msg, this, __FUNCTION__, __LINE__);
}
if (TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height) != 1) {
QString msg = "Failed to read height of TIFF: '" + path + "'";
throw new Exception(NULL, msg, this, __FUNCTION__, __LINE__);
}
// Number of bytes in a decoded scanline
scanlength = TIFFScanlineSize(tiff);
QImage image(width, height, QImage::Format_RGB16);
// TEMPORARY: Save to PNG for preview
image.save("tiff/" + StringUtils::random() + ".png", "PNG");
if (image.isNull() || scanlength != image.bytesPerLine()) {
TIFFClose(tiff);
QString msg = "Failed to create QImage from TIFF: '" + path + "'";
throw new Exception(NULL, msg, this, __FUNCTION__, __LINE__);
}
// Read image data
for (uint32 y = 0; y < height; y++) {
TIFFReadScanline(tiff, image.scanLine(y), y);
}
TIFFClose(tiff);
return image;
}
答案 1 :(得分:1)
您可以使用循环生成查询 -
$Title = "aa, bb, cc, dd, ee, ff";
$Pubdate = "aa, bb, cc, dd, ee, ff";
$Link = "aa, bb, cc, dd, ee, ff";
$TitleArray = explode(', ',$Title);
$PubdateArray = explode(', ',$Pubdate);
$LinkArray = explode(', ',$Link);
//Either: loop through the array and build individual INSERTS
for($i=0;$i<count($TitleArray);$i++){
// generate an INSERT for each row
$sql = "INSERT INTO ytable (Title, Pubdate, Link)
VALUES ('$TitleArray[$i]', '$PubdateArray[$i]', '$LinkArray[$i]')";
}
//Or you could build a single query string inside the loop, and run it at the end.
答案 2 :(得分:0)
您需要使用explode
,implode
和foreach
循环。
$Title = "aa, bb, cc, dd, ee, ff";
$Pubdate = "aa, bb, cc, dd, ee, ff";
$Link = "aa, bb, cc, dd, ee, ff";
$Title = explode(', ', $Title);
$Pubdate = explode(', ', $Pubdate);
$Link = explode(', ', $Link);
$sql = "INSERT INTO ytable (Title, Pubdate, Link) VALUES ";
$values= array();
foreach($Title as $key => $val)
{
$values[] = "('" . $val . "','" . $Pubdate[$key] . "','" . $link[$key] . "')";
}
$sql .= implode(',', $values);
警告强>: 从PHP 5.5.0开始,不推荐使用MYSQL扩展,自PHP 7.0.0起,该扩展已被删除。相反,应该使用MySQLi或PDO_MySQL扩展。
答案 3 :(得分:0)
你需要这样的东西,
$Title = "aa, bb, cc, dd, ee, ff";
$Pubdate = "aa, bb, cc, dd, ee, ff";
$Link = "aa, bb, cc, dd, ee, ff";
$titleArr = explode(",",$Title);
$pubdateeArr = explode(",",$Pubdate);
$linkArr = explode(",",$Link);
foreach($titleArr as $key => $title){
$insertSQL = "INSERT INTO ytable (Title, Pubdate, Link) VALUES ('$title', '$pubdateeArr[$key]', '$linkArr[$key]')";
}
或者您只能迭代值
$a = array();
$insertSQL = "INSERT INTO ytable (Title, Pubdate, Link) VALUES ";
foreach($titleArr as $key => $title){
$a[] = "('$title', '$pubdateeArr[$key]', '$linkArr[$key]')";
}
$insertSQL .= implode(",",$a);
答案 4 :(得分:0)
您可以尝试这样的事情:
$Title = "aa, bb, cc, dd, ee, ff";
$Pubdate = "aa, bb, cc, dd, ee, ff";
$Link = "aa, bb, cc, dd, ee, ff";
$titleArr = explode(",",$Title);
$pubdateeArr = explode(",",$Pubdate);
$linkArr = explode(",",$Link);
foreach($titleArr as $key => $title){
if(!empty($title) && !empty($pubdateeArr[$key]) && !empty($linkArr[$key]))
$insertSQL = "INSERT INTO ytable (Title, Pubdate, Link) VALUES ('$title', '$pubdateeArr[$key]', '$linkArr[$key]')";
}
我尝试过,如果不是空的,因为变量的值不能相同。