php数组数据库怎么写
1.PHP数组,如何写入mysql数据库
<?php
$info=$_POST['info'] ;
$values = "";
for($i = 0; $i < count($info['title']); $i++){
$values .= "('{$info['title'][$i]}', '{$info['price'][$i]}', '{$info['amout'][$i]}'),";
}
$values = rtrim($values, ',');
mysql_query("insert into tablename values ".$values);
//这种情况适用于比较少的行插入 如果数据量比较大
将数据按行存入文本中
for($i = 0; $i < count($info['title']); $i++){
$values .= "{$info['title'][$i]}\t{$info['price'][$i]}\t{$info['amout'][$i]}\r\n";
}
file_put_content('data.txt', $values);
mysql_query("load data local infile 'data.txt' into table tablename");
2.php 怎么分割数组 写进数据库的
<?php
/*作者:泡啤网*/
$id=mysql_connect("localhost","root","123") or die('连接失败:' . mysql_error());//说明:root 是数据库用户名 123是密码
if(mysql_select_db("web",$id)) //说明:web 是数据库名称
echo "";
else
echo ('数据库选择失败:' . mysql_error());
mysql_query("set names gb2312"); //设置为简体中文
?>
<?php
$str="您好,您好吗,他是,我是";
$strArray = explode(",",$str);
foreach ($strArray as $list){
$query=mysql_query("insert into tag(txt) values('$list')");
}
?>
3.php怎么写入、存储数组数据
PHP有自带的高性能函数 var_export
conn.php
<?php
$dbconfig = array (
'host'=>'127.0.0.1',
'name'=>'root',
'password'=>'123456',
?>b.php
<?php
// 读取配置
include 'conn.php';
echo $dbconfig['host'];
// 修改配置
$dbconfig['host'] = 'xxx.xxx.xxx.xxx';
file_put_contents('conn.php', "<?php\n$dbconfig = " . var_export($dbconfig) . "\n?>");
// 再读取配置
include 'conn.php';
echo $dbconfig['host'];
?>
参考连接:/manual/zh/function.var-export.php
4.php 把数组插入数据库
lxydjx 正解,我来详细补充一下吧。未经测试、、、
//初始化
$sql = array();
// 从 a.php POST 过来的值
$_POST["xinxi"] = "20-2,19-1,18-1";
// 拆分为 array("20-2", "19-1", "18-1");
$post_data = explode(",", $_POST["xinxi"]);
// 循环数组
for($i = 0; $i
php怎么写代码
1. PHP这段代码怎么写
<html>
<head>
<meta ;charset=utf-8" />
<title>;测试</title>
</head>
<body>
<?php
if($_POST && $_POST['age']){
$age = intval($_POST['age']);
if($age>=1 && $age<=17){
echo "你正处于少年时期,好好努力";
}else if($age>=18 && $age<=29){
echo "你处于青年时期,努力奋斗";
}else if($age>=30 && $age<=50){
echo "你处于壮年时期,处于事业巅峰";
}else{
echo "你的年龄我无法判断";
}
}
?>
<form action="" method="post">
<p><span>;年龄</span><input name="age" type="text"/></p>
<p><input type="submit" value="提交" name="submit"/></p>
</form>
</body>
<html>
2. 这样的PHP代码怎么写
这样就行了。
也可以上楼上说的用反序列化函数unserialize()。这种形式一般是放在数组库的 $str ='a:7:{s:9:"frame_set";s:2:"lc";s:8:"allframe";a:2:{s:5:"dleft";a:8:{i:0;s:7:"profile";i:1;s:7:"applist";i:2;s:10:"friendlist";i:3;s:9:"footprint";i:4;s:5:"doing";i:5;s:4:"mtag";i:6;s:5:"share";i:7;s:6:"player";}s:8:"dcontent";a:7:{i:0;s:4:"feed";i:1;s:8:"albumone";i:2;s:4:"blog";i:3;s:8:"albumall";i:4;s:6:"thread";i:5;s:11:"forumstatus";i:6;s:4:"wall";}}s:9:"effectall";a:0:{}s:9:"blockname";a:0:{}s:5:"music";a:0:{}s:5:"block";a:1:{s:4:"body";a:1:{s:0:"";a:1:{s:16:"background-image";s:95:"background-image:url( http://127.0.0.1/pic/f7fc7f0ea9e5b753707ae20b87b8478e_b.gif)";}}}s:6:"cursor";N;}background-image:url( http://1ds.0.1/pic/f7fc7f0ea9e5b753707ae20b87b8478e_b.gif)";}}background-image:url( http://127.sdf.1/pic/f7fc7f0ea9e5b753707ae20b87b8478e_b.gif)";}}background-image:url( http://1jhgk0.1/pic/f7fc7f0ea9e5b753707ae20b87b8478e_b.gif)";}}'; if(preg_match_all('/url\((. ?)\)/', $str, $matches)) { foreach($matches[1] as $val) { echo $val; echo ""; } } 测试结果: http://127.0.0.1/pic/f7fc7f0ea9e5b753707ae20b87b8478e_b.gif http://1ds.0.1/pic/f7fc7f0ea9e5b753707ae20b87b8478e_b.gif http://127.sdf.1/pic/f7fc7f0ea9e5b753707ae20b87b8478e_b.gif http://1jhgk0.1/pic/f7fc7f0ea9e5b753707ae20b87b8478e_b.gif 不会再问:QQ:1350630107,接5000元以上大型项目,二次开发等。
3. 一段php代码该怎么写
根据你的描述,好像是显示产品的一个格式,$a, $id,$num,$money分别是对应各个产品的属性,$count则是计算所有产品的总价值,那么php代码可以这样写:$id = explode('|', '3171|2486');$num = explode('|', '8|5');$money = explode('|', '1|1');$count = 0;$totalNum = count($a);echo count($a) . "";$str = "{";for ($i = 0; $i /* $str .= '\"'; $str .= $a[$i]; $str .= '\":{\"id\":'; $str .= $id[$i]; $str .= ','; $str .= '\"num\":'; $str .= $num[$i]; $str .= ',\"money\":'; $str .= $money[$i]; $str .= '},'; */ $str .= sprintf('\"%d\":{\"id\":%d,\"num\":%d,\"money\":%d},', $a[$i], $id[$i], $num[$i], $money[$i]); $count += ($num[$i] * $money[$i]);}$str .= '\"Count\":';$str .= $count;$str .= '}';echo $str; 追问: json_encode json_decode 楼下说用这种方法,我也记得好想是有个命令可以把数组直接变成这种格式的 追答: 使用json_encode的话,以下是最简单的一个示例: 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);echo json_encode($arr);?>输出{"a":1,"b":2,"c":3,"d":4,"e":5}要使用的话,同样也是在for循环中构造,一样的结果,且只能构造{\"id\":3171,\"num\":8,\"money\":1}这个元素集合段,其中\符号也不会出现 追问: addslashes 这个函数可以添加 斜杠的 你QQ多少,我想再问一下 评论0 0 0。
4. PHP代码编写
<?php
function modelup($len){
$str="";
for($i=0;$i<$len;$i++){
for($n=$len;$n>$i;$n--){
$str.=" ";
}
for($j=0;$j<$i+1;$j++){
$str.=" * ";
}
$str.="<br>";
}
return $str;
}
function modeldown($len){
$str="";
for($i=0;$i<$len;$i++){
for($n=0;$n<$i+2;$n++){
$str.=" ";
}
for($j=5;$j>$i;$j--){
$str.=" * ";
}
$str.="<br>";
}
return $str;
}
function model($len){
return modelup($len).modeldown($len);
}
echo model(6);
?>
输出结果
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
5. PHP怎么写类,一般什么时候用,要代码哦
写类就用Class关键字来声明。
比如:
class Cart {
var $items; // 购物车中的物品
//构造函数,用来初始化一个类
function Cart() {
$this->items = array();
}
// 将 $num 个 $artnr 物品加入购物车
function add_item($artnr, $num) {
$this->items[$artnr] += $num;
}
// 将 $num 个 $artnr 物品从购物车中取出
function remove_item($artnr, $num) {
if ($this->items[$artnr] > $num) {
$this->items[$artnr] -= $num;
return true;
} elseif ($this->items[$artnr] == $num) {
unset($this->items[$artnr]);
return true;
} else {
return false;
}
}
}
类的用处很广,是面向对象编程的基础,一般在你要封装一个对象的时候使用。
6. 下面这段代码用PHP语言怎么写
$rows = explode(',', $str);
echo '';
foreach($rows as $row){
$cols = explode(' ',$row);
echo '';
foreach($cols as $col){
echo '' . $col . '';
}
echo '';
}
echo '';
转载请注明出处育才学习网 » php怎么写东西入数据库
育才学习网