在 MySQL 中使用 INSERT 时未选择数据库?
No database selected when using INSERT in MySQL?
好吧,我对 php 和 mysql 很陌生,我似乎无法弄明白。所以我要做的是将一些变量发送到 MySQL table。我已经在一个函数内建立了连接(我相信这是可能的(?))
使用我当前的代码,它给了我以下错误。
Notice: No database selected in INSERT INTO retrieveddata (profileId, profileName, pageViews, profileRevenue, currentDate) VALUES(NULL,'www.mywebsite.nl[testview2]', '48702', '0.0',NULL) in C:\xampp\htdocs\analyticsoverview\HelloAnalyticsApi.php on line 281
我曾尝试将 select_db 添加到我的代码中,但它不起作用。
这是我的代码。
function printResults(&$results) {
if (count($results->getRows()) > 0) {
$profileName = $results->getProfileInfo()->getProfileName();
$rows = $results->getRows();
$sessions = $rows[0][0];
$pageviews = $rows[0][1];
$bounces = $rows [0][2];
$betaalgoal = $rows [0][3];
$transrevenue = $rows [0][4];
// set up database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "googleanalytics";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// select database that needs to be filled with the data from the variables
$conn->select_db('googleanalytics');
// the mysql query to insert the variables into table called retrieveddata
<
$query = "INSERT INTO retrieveddata (profileId, profileName, pageViews, profileRevenue, currentDate)
VALUES(NULL,'$profileName', '$pageviews', '$transrevenue',NULL)";
mysql_query($query) or trigger_error(mysql_error()." in ".$query);
您可以删除
$conn->select_db('googleanalytics');
因为您在创建连接时已经选择了数据库,它是 mysqli() 的最后一个参数。
尝试改变:
mysql_query($query) or trigger_error(mysql_error()." in ".$query);
这是:
$conn->query($query) or trigger_error(mysql_error()." in ".$query);
构建查询时,您必须连接更多字符串,否则您的变量将无法正确使用
$query="INSERT INTO retrieveddata (profileId, profileName, pageViews, profileRevenue, currentDate) VALUES(NULL,'".$profileName."', '".$pageviews."', '".$transrevenue."',NULL)";
首先,你不需要$conn->select_db('googleanalytics');
您已经选择了您的数据库:
$conn = new mysqli($servername, $username, $password, $dbname);
然后,您将 MySQL API 与 mysql_
不与任何其他函数混合的函数混合。
$query = "INSERT INTO retrieveddata
(profileId, profileName, pageViews, profileRevenue, currentDate)
VALUES (NULL,'$profileName', '$pageviews', '$transrevenue',NULL)";
$result = mysqli_query($conn, $query);
if ( !$result ) {
trigger_error('query failed', E_USER_ERROR);
}
将数据库连接传递给查询和我更改的错误触发器。
或者简单地说,or die(mysqli_error($conn))
到 mysqli_query()
。
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
旁注:
现在,如果您的数据包含撇号或 MySQL 可能会抱怨的其他数据,您将需要使用 mysqli_real_escape_string()
清理数据。否则,您可能会遇到语法错误。
即举个例子:
$profileName = mysqli_real_escape_string($conn, $profileName);
并对其他变量执行相同的操作。
- 您当前的代码对 SQL injection. Use
mysqli_
with prepared statements, or PDO with prepared statements 开放,它们更安全。
将 error reporting 添加到您的文件的顶部,这将有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:错误报告只能在试运行中进行,绝不能在生产中进行。
好吧,我对 php 和 mysql 很陌生,我似乎无法弄明白。所以我要做的是将一些变量发送到 MySQL table。我已经在一个函数内建立了连接(我相信这是可能的(?))
使用我当前的代码,它给了我以下错误。
Notice: No database selected in INSERT INTO retrieveddata (profileId, profileName, pageViews, profileRevenue, currentDate) VALUES(NULL,'www.mywebsite.nl[testview2]', '48702', '0.0',NULL) in C:\xampp\htdocs\analyticsoverview\HelloAnalyticsApi.php on line 281
我曾尝试将 select_db 添加到我的代码中,但它不起作用。 这是我的代码。
function printResults(&$results) {
if (count($results->getRows()) > 0) {
$profileName = $results->getProfileInfo()->getProfileName();
$rows = $results->getRows();
$sessions = $rows[0][0];
$pageviews = $rows[0][1];
$bounces = $rows [0][2];
$betaalgoal = $rows [0][3];
$transrevenue = $rows [0][4];
// set up database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "googleanalytics";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// select database that needs to be filled with the data from the variables
$conn->select_db('googleanalytics');
// the mysql query to insert the variables into table called retrieveddata
<
$query = "INSERT INTO retrieveddata (profileId, profileName, pageViews, profileRevenue, currentDate)
VALUES(NULL,'$profileName', '$pageviews', '$transrevenue',NULL)";
mysql_query($query) or trigger_error(mysql_error()." in ".$query);
您可以删除
$conn->select_db('googleanalytics');
因为您在创建连接时已经选择了数据库,它是 mysqli() 的最后一个参数。
尝试改变:
mysql_query($query) or trigger_error(mysql_error()." in ".$query);
这是:
$conn->query($query) or trigger_error(mysql_error()." in ".$query);
构建查询时,您必须连接更多字符串,否则您的变量将无法正确使用
$query="INSERT INTO retrieveddata (profileId, profileName, pageViews, profileRevenue, currentDate) VALUES(NULL,'".$profileName."', '".$pageviews."', '".$transrevenue."',NULL)";
首先,你不需要$conn->select_db('googleanalytics');
您已经选择了您的数据库:
$conn = new mysqli($servername, $username, $password, $dbname);
然后,您将 MySQL API 与 mysql_
不与任何其他函数混合的函数混合。
$query = "INSERT INTO retrieveddata
(profileId, profileName, pageViews, profileRevenue, currentDate)
VALUES (NULL,'$profileName', '$pageviews', '$transrevenue',NULL)";
$result = mysqli_query($conn, $query);
if ( !$result ) {
trigger_error('query failed', E_USER_ERROR);
}
将数据库连接传递给查询和我更改的错误触发器。
或者简单地说,or die(mysqli_error($conn))
到 mysqli_query()
。
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
旁注:
现在,如果您的数据包含撇号或 MySQL 可能会抱怨的其他数据,您将需要使用 mysqli_real_escape_string()
清理数据。否则,您可能会遇到语法错误。
即举个例子:
$profileName = mysqli_real_escape_string($conn, $profileName);
并对其他变量执行相同的操作。
- 您当前的代码对 SQL injection. Use
mysqli_
with prepared statements, or PDO with prepared statements 开放,它们更安全。
将 error reporting 添加到您的文件的顶部,这将有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:错误报告只能在试运行中进行,绝不能在生产中进行。