Reputation: 43
I am attempting to use XAMPP as a simulated web server so that I can write files locally from my laptop. I do school work at my job but I don't have an Internet connection there that I can use to upload my PHP files to my school's web server. The Apache sever that comes as part of XAMPP works great for debugging .php files but I can't seem to connect to the database that I created in phpMyAdmin to debug MySQL code. Below is an example of what I am trying to do:
<?php
$user = 'root';
$password = '';
$database = 'jesse';
$con = mysql_connect('localhost', $user, $password, $database) or die( "Unable to connect." );
$sql = "insert into CLIENT (First_Name) values ('Jesse')";
mysql_query($sql) or die("Insert failed. " . mysql_error());
?>
This returns: Insert failed. No database selected
Upvotes: 0
Views: 6737
Reputation: 1
It looks like the db created via either the xampp gui or script is locked within xamppfiles/var/sql but your admin script can be run from a php file, e.g. testdb.php in my xampp htdocs folder
$con = mysql_connect('www.myplace', 'root', '') or die ("unable to connect");
mysql_query('CREATE DATABASE test1', $con); //include if test1 doesn't already exist
mysql_select_db('test1', $con);
$sql = "INSERT INTO names (first_name) VALUES ('mini mouse')";
mysql_query($sql) or die("insert failed");
Turn on xampp mySQL and Apache beforehand (or it doesn't add to the db). Run by opening that php page in the browser at http://www.myplace/home/wk10db/testdb.php where tested.php resides.
NB - www.myplace is really localhost but this site was giving me trouble posting
Upvotes: 0
Reputation: 1542
You missed to select the database mysql_select_db($database,$con)
,Check this PHP mysql_select_db() Function
Upvotes: 1