如何在PHP中转换数组中的字符串即
$str="this is string";
应该是这样的
arr[0]=this
arr[1]=is
arr[2]=string
该
str_split($str, 3);将字符串拆分为3个字符的单词,但我想在数组中的空格后转换字符串.
当我使用下面的代码并在本地解析xml时,它可以正常工作,但是当在服务器上传相同的脚本时,它会显示错误.
注:我检索到的$lng,并$lat从查询字符串,并将其在本地工作正常.
$lng=$_GET['lng'];
$lat=$_GET['lat'];
$conn=new LoginSystem();
$conn->connect();
$dom = new DOMDocument("1.0");
$query="select catch_id,catch_details,image from mycatch where longitude='$lng' AND latitude='$lat'";
$result = mysql_query($query);
if (!$result) {
die("Invalid query: " . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
$node = $dom->createElement("mycatch");
$node = $dom->appendChild($node);
foreach ($row as $fieldname => $fieldvalue) {
$child = $dom->createElement($fieldname);
$child = $node->appendChild($child);
$value = $dom->createTextNode($fieldvalue);
$value = $child->appendChild($value);
}
}
$conn->disconnect();
$xml_string …Run Code Online (Sandbox Code Playgroud) 我正在使用php/mysql.我知道mysql中的事务但不能在我的脚本中使用.下面是我的脚本我怎么能在我的code.ie BEGIN,ROLLBACK,COMMIT中使用php事务
foreach($json_a['shop'] as $jsondata=>$json)
{
if($json['category']==='product')
{
$name_product=$json['name'];
$query1="insert into product(id,name,user_id)values('','" . mysql_real_escape_string($name_product). "','1')";
$result1=mysql_query($query1) or die("error in query".mysql_errno());
//echo "success...!";
$product++;
}
else
if($json['category']==='order')
{
$name_order=$json['name'];
$query2="insert into order(id,name,user_id)values('','" . mysql_real_escape_string($name_order). "','1')";
$result2=mysql_query($query2) or die("error in query".mysql_errno());
$order++;
}
else
if($json['category']==='sale')
{
$name_sale=$json['name'];
$query3="insert into sale(id,name,user_id)values('','" . mysql_real_escape_string($name_sale). "','1')";
$result3=mysql_query($query3) or die("error in query".mysql_errno());
$sale++;
}
}Run Code Online (Sandbox Code Playgroud) 我有一个从数据库中检索到的json数据,即
$result=array();
$query="SELECT * FROM fish";
$result1 = mysql_query($query, $conn);
while ($table = mysql_fetch_array($result1, MYSQL_ASSOC)){
$result[]=$table;
}
echo json_encode($result);Run Code Online (Sandbox Code Playgroud)
这给了我结果 [{"fish_id":"1","name":"first fish update","info":"this is my first fish update","image":"http:\/\/www.localhost\/cafe\/pics\/logout (1).gif"}]
但是当我调用这个json数据时,从另一个页面,即
$input = file_get_contents("http://localhost/fish/fish-json.php");
$json=json_decode($input);
echo $json->fish_id;
它给了我错误
Notice: Trying to get property of non-object in /var/www/fish/json-to-php.php on line 13 Call Stack: 0.0005 318764 1. {main}() /var/www/fish/json-to-php.php:0 当我将记录插入mysql时,它给我错误
$sql = "insert into fish (fish_id,common_name,scientific_name,family,range,habitate,adult_size,identification,how_to_fish,image) values ('$com_name','$scientific_name','$family','$range','$habitate','$adult_size','$identification','$how_to_fish','$TARGET_PATH')";
Run Code Online (Sandbox Code Playgroud)
错误是
无法将数据插入数据库:您的SQL语法中有错误; 检查与MySQL服务器版本对应的手册,以便
range,habitate,adult_size,identification,how_to_fish,image) values ('Suwannee Ba在第1行的' ' 附近使用正确的语法
当我转储查询时,它显示所有字段都是正确的.
string(737) "insert into fish (catch_id,common_name,scientific_name,family,range,habitate,adult_size,identification,
how_to_fish,image) values ('','Suwannee Bass','Micropterus notius','Centrarchidae (Sunfish)','United States (Florida, Georgia)',
'Freshwater: found in Suwannee and Ochlockonee river drainages of Florida and Georgia.',
'Up to 12 oz (.34 kg).','The smallest of the Black Bass; brown with dark markings along back and sides. Adult male has blue cheeks, breast, and belly.',
'Natural or artificial bait such as spinners, …Run Code Online (Sandbox Code Playgroud) i have two tables
CREATE TABLE IF NOT EXISTS `user` ( `user_id` int(20) NOT NULL AUTO_INCREMENT, `ud_id` varchar(50) NOT NULL, `name` text NOT NULL, `password` text NOT NULL, `email` varchar(200) NOT NULL, `image` varchar(150) NOT NULL, PRIMARY KEY (`user_id`) ) ENGINE=InnoDB
and mycatch table is
CREATE TABLE IF NOT EXISTS `mycatch` ( `catch_id` int(11) NOT NULL AUTO_INCREMENT, `catch_name` text NOT NULL, `catch_details` text NOT NULL, `longitude` float(10,6) NOT NULL, `latitude` float(10,6) NOT NULL, `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON …