Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL和PHP复制打印_Php_Mysql - Fatal编程技术网

MySQL和PHP复制打印

MySQL和PHP复制打印,php,mysql,Php,Mysql,我对PHP和MySQL非常陌生,正在尝试从MySQL表中获取数据并将其打印出来。我给数据库打了个电话,效果很好。我能阅读信息。出来但数据中存在重复项。 到目前为止,我已经: <?php /// Make a MySQL Connection mysql_connect("localhost", "loop", "XXX") or die(mysql_error()); mysql_select_db("loop") or die(mysql_error()); // Retrieve a

我对PHP和MySQL非常陌生,正在尝试从MySQL表中获取数据并将其打印出来。我给数据库打了个电话,效果很好。我能阅读信息。出来但数据中存在重复项。 到目前为止,我已经:

<?php
/// Make a MySQL Connection
mysql_connect("localhost", "loop", "XXX") or die(mysql_error());
mysql_select_db("loop") or die(mysql_error());

// Retrieve all the data from the "profile" table
$result = mysql_query("SELECT * FROM profile")
or die(mysql_error());  

//print out info.
while ($row = mysql_fetch_array($result)) {
    echo("<pre>");
  var_dump($row);
    echo("</pre>");
}
?>

为什么它有重复的元素?我检查了数据库,一切正常。有人能解释一下我遗漏了什么吗?

您可以将第二个参数传递给mysql_fetch_数组函数,告诉它是返回关联数组(hashmap-从列到行的值)还是返回行元素的常规数组。默认情况下,它将同时返回这两个值

还有一些专用函数可以将行值作为数组和hashmap获取: mysql_fetch_row()
mysql_fetch_assoc()

这是因为默认情况下,
mysql_fetch_数组
会返回数字数组和关联数组检查php手册

请尝试使用
mysql\u-fetch\u-assoc
而不是
mysql\u-fetch\u-array
,因为
mysql\u-fetch\u-array
在PHP5.5.0中被弃用,在PHP7.0.0中被删除

但是如果你还想用这个,这个会有帮助的


尝试传递第二个参数
mysql\u-fetch\u-array($result,mysql\u-ASSOC)
mysql\u-fetch\u-array($result,mysql\u-NUM)

考虑使用诸如mysqli之类的更新方法。。php文档中的答案如下:要获取的数组类型。它是一个常量,可以接受以下值:MYSQL\u ASSOC、MYSQL\u NUM和MYSQL\u两者。MYSQL\u都是默认值。
array(1) {
  [0]=>
  array(14) {
    [0]=>
    string(1) "1"
    ["id"]=>
    string(1) "1"
    [1]=>
    string(13) "test@test.com"
    ["email"]=>
    string(13) "test@test.com"
    [2]=>
    string(8) "passcode"
    ["pass"]=>
    string(8) "passcode"
    [3]=>
    string(4) "John"
    ["nameFirst"]=>
    string(4) "John"
    [4]=>
    string(5) "Smith"
    ["nameLast"]=>
    string(5) "Smith"
    [5]=>
    string(8) "face.jpg"
    ["pic"]=>
    string(8) "face.jpg"
    [6]=>
    string(16) "Some dummy text."
    ["bio"]=>
    string(16) "Some dummy text."
  }
}