-2

Edit 2: Removed incorrect and irrelevant code

FYI: I'm expecting it to be, I am not linking to the correct file location/database.

I'm very new to PHP and MySQL

So I've collected Data from an arduino and stored the data in a CSV file, I've uploaded the file to WAMP server / phpmyadmin.

I'm trying to link up to the database using PHP but get an error: "Connected to MySQL Could not select"

So it cannot seem to find 'sensordata' Here is the code

        <?php

$username = "root";
$password = "";
$hostname = "localhost";

$dbhandle = mysqli_connect($hostname, $username, $password)
    or die("Unable to connect to mysqli");
echo "Connected to mysqli<br>";

$dbname = "sensordata";

$selected = mysqli_select_db($dbhandle,"sensordata")
//$selected = mysqli_select_db($dbname,$dbhandle)

    or die("Could not select");
echo "Connected to sensordata<br>", "<br>";

?>

Any ideas anyone? The file structure can be seen below

enter image description here

  • 3
    you're doing what we call *Mixing APIs*; you can't do that. This besides that syntax error you're getting. So, it's back to the 'ol drawing board for you. – Funk Forty Niner Jun 09 '16 at 18:39
  • 3
    oh and `$dbname = "sensordata"` <<< notice what's missing here? plus, you have both `sensordata` as db and table. If one of those is different, well then......... there you go. – Funk Forty Niner Jun 09 '16 at 18:42
  • 2
    then you have `$submit` in `if($submit)` well that is undefined. Do check for errors and read the manuals over carefully. Your code is riddled with errors. – Funk Forty Niner Jun 09 '16 at 18:45
  • 1
    You now have enough information to go on in order to rebuild your code. I could have easily used my comments to be posted as an answer, but that would probably lead to other things which I am not up to doing any more than I already have. If someone cares to pop one in; go ahead (if you have time before the grace period ends), but good luck with this. However, I have closed the question because of the most obvious. – Funk Forty Niner Jun 09 '16 at 18:53
  • 1
    To @Fred-ii-'s point. I would trim this code down to just connect and spit the table out onto a page. If you can get that successfully, then add in that extra `INSERT` and whatnot. You don't have to connect to your db again in there, as you are already connected, for instance. The data in each field of your result set isn't going to be held in `$_POST`. The API's you are mixing are `mysqli_*` and `mysql_`. Switch everything to use `mysqli_` as the other is depricated. Take it one step at a time and you'll get it running. Theres a lot of problems here, but you are on the right track. – JNevill Jun 09 '16 at 18:54
  • @fred-ii can you reopen this please, my question is not answered. It's not about mixing API's. It's about $selected = mysqli_select_db($dbhandle,"sensordata") //$selected = mysqli_select_db($dbname,$dbhandle) or die("Could not select"); echo "Connected to sensordata
    ", "
    "; Not finding sensordata. I have gotten rid of all code below 'echo' connected to sensordata
    – Brian Geoghegan Jun 09 '16 at 19:06
  • oh really?? I don't see any change in your question – Funk Forty Niner Jun 09 '16 at 19:08
  • I have now, cheers – Brian Geoghegan Jun 09 '16 at 19:11
  • you need to get the *real* error here http://php.net/manual/en/mysqli.error.php rather than your echos (die) if/when failing occurs. You originally posted that the query table's is called `sensordata` yet so is the database you're calling. Are both your table AND database both called the same thing? A database and a table are two different animals. – Funk Forty Niner Jun 09 '16 at 19:23

1 Answers1

1

Quite simply you are trying to connect to a TABLE and not a DATABASE

The Mysqli API also allows you connect to a database as part of the mysqli_connect()

$username = "root";
$password = "";
$hostname = "localhost";
$database = 'edisonsensordata';     //<-- amended

$dbhandle = mysqli_connect($hostname, $username, $password, $database)
             or die("Unable to connect to mysqli");
echo "Connected to mysqli<br>";

The mysqli_select_db() API is basically for switching databases on that same connection if you ever find yourself using more than one database

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149