1

Okay I'm quite new to php and mysql and I can't seem to figure this out. So what I'm trying to do is send some variables to a MySQL table. I have set up the connection inside a function (I believe this is possible(?) )
And with my current code it gives me the following error.

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

I have tried adding the select_db to my code but it doesn't work. This is my code.

    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);
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
ampyfibe
  • 13
  • 3

2 Answers2

2

Firstly, you don't need $conn->select_db('googleanalytics');

You already chose your database in:

$conn = new mysqli($servername, $username, $password, $dbname);

Then, you're mixing MySQL APIs with mysql_ functions that do not intermix with any other functions.

$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);
}

Pass the database connection to the query and for the error trigger which I changed.

Or simply, or die(mysqli_error($conn)) to mysqli_query().

$result = mysqli_query($conn, $query) or die(mysqli_error($conn));

Sidenote:

Now, if your data contains apostrophes or other data the MySQL may complain about, you will need to sanitize your data using mysqli_real_escape_string(). Otherwise, you may get syntax errors.

I.e. and as an example:

$profileName = mysqli_real_escape_string($conn, $profileName);

and doing the same for the other variables.



Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Currently gives me Fatal error: query failed in C:\xampp\htdocs\analyticsoverview\HelloAnalyticsApi.php on line 289. But I'll still have some other options to try. I'll let you know how it goes ;). – ampyfibe May 21 '15 at 18:29
  • @ampyfibe You'll need to find out exactly what the real error is. Use `$result = mysqli_query($conn, $query) or die(mysqli_error($conn));` if you are not doing that already and check your logs. Just a `query failed` isn't enough to find out why the query failed. – Funk Forty Niner May 21 '15 at 18:33
  • @ampyfibe also add error reporting, as stated near the bottom of my answer. See if that also helps. – Funk Forty Niner May 21 '15 at 18:41
0

You could delete

$conn->select_db('googleanalytics');

because when you create the connection you've already selected the database, it's the last parameter of mysqli().

Try to change:

mysql_query($query) or trigger_error(mysql_error()." in ".$query);

whith this:

$conn->query($query) or trigger_error(mysql_error()." in ".$query);

When you build your query you have to concatenate more strings, elsewhere your variables won't be correctly used

$query="INSERT INTO retrieveddata (profileId, profileName, pageViews, profileRevenue, currentDate) VALUES(NULL,'".$profileName."', '".$pageviews."', '".$transrevenue."',NULL)";
Marco
  • 705
  • 8
  • 28
  • thanks for your reply, I have done what you said now it gives me Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) in C:\xampp\htdocs\analyticsoverview\HelloAnalyticsApi.php on line 284 – ampyfibe May 21 '15 at 17:40
  • `or trigger_error(mysql_error()` are you sure about that? – Sean May 21 '15 at 17:40
  • @zanna line 284: mysqli->query($query) or trigger_error(mysql_error()." in ".$query); – ampyfibe May 21 '15 at 17:41
  • Sorry I've forgotten a thing, the correct line is $conn->query($query) or trigger_error(mysql_error()." in ".$query); – Marco May 21 '15 at 17:41
  • Replaced it with the correct line and currently gives me: Notice: in INSERT INTO retrieveddata (profileId, profileName, pageViews, profileOmzet, currentDate) VALUES(NULL,'www.mywebsite.nl[testview2]', '48702', '0.0',NULL) in C:\xampp\htdocs\analyticsoverview\HelloAnalyticsApi.php on line 284 – ampyfibe May 21 '15 at 17:47
  • I've appended the correction of the query building, times ago I've got the same error and I solved it in that way – Marco May 21 '15 at 18:02