Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
write to db

Posted by bschultz (bschultz), 24 June 2003
Is it possible to have a write to db script that has a variable in it for the db name?

Here's what I'm using:

if ($submit == "click"){
 // The submit button was clicked!
 // Get the input for the game then store it in the database.
if (! $db = mysql_connect("", "", "")) die ("Unable to connect");
 $query = "insert into $gamedate values
('$score','$T1', '$T2', '$T3', '$T4', '$T5', '$T6', '$T7', '$T8', '$T9', '$T10',
'$T11', '$T12', '$T13', '$T14', '$T15', '$T16', '$T17', '$T18', '$T19', '$T20',
'$T21', '$T22', '$T23', '$T24', '$T25', '$T26', '$T27', '$T28', '$T29', '$T30',
'$T31', '$T32', '$T33', '$T34', '$T35', '$T36', '$T37', '$T38', '$T39', '$T40',
'$T41', '$T42', '$T43', '$T44', '$T45', '$T46', '$T47', '$T48', '$T49', '$T50',
'$T51', '$T52', '$T53', '$T54', '$T55', '$T56', '$T57', '$T58', '$T59', '$T60',
'$T61', '$T62', '$T63', '$T64', '$T65', '$T66', '$T67', '$T68', '$T69', '$T70',
'$T71', '$T72', '$T73', '$T74', '$T75', '$T76', '$T77', '$T78', '$T79', '$T80',
'$T81', '$T82', '$T83', '$T84', '$T85', '$T86', '$T87', '$T88', '$T89', '$T90',
'$T91', '$T92', '$T93', '$T94', '$T95', '$T96', '$T97', '$T98', '$T99', '$T100',

'$T101', '$T102', '$T103', '$T104', '$T105', '$T106', '$T107', '$T108', '$T109', '$T110',
'$T111', '$T112', '$T113', '$T114', '$T115', '$overall_record', '$clc_record')";
 $result = mysql_db_query ("bschultz_fall", $query);
 if ($result){
   echo "Thank you for your time, and good luck this season!";
   echo mysql_errno().": ".mysql_error()."<BR>";
 mysql_close ();

It says there's an error on line one...which confuses my little brain.  Line 1 is the <?php...


Posted by admin (Graham Ellis), 24 June 2003
Firstly, nothing to stop you using a variable name to select your database.

Now - the code error.  I suspect that you might have some control character(s) in your file.  This sometimes happens when files are transferred in ASCII when they should be transferred in binary (or vice versa), and on certain editiors to can mis-key and insert control codes in your source.

Solution ... call up the file in an editor that will display control codes and fix any problems revealed OR delete and retype the offending line.

Posted by bschultz (bschultz), 24 June 2003
this file is a local's never been ftp'ed.  I use a program called phpcoder to code php, and it shows variables in red...the variable for the table name is still showing up in green (regular text).  I can't find any way to display control codes...can you suggest an editor that does?


Posted by John_Moylan (jfp), 24 June 2003
I can't really speak on the topic of a dedicated php editor. (I use emacs, which does me fine)

But this will help:

Select your platform & licence type, I suspect you'll want freeware and browse the varients.


Posted by admin (Graham Ellis), 24 June 2003
Personally, I use vi with ":set list" to display control codes in files, and there is a PC version available even though this is very much a Linux and Unix editor.  I'm also used to notepad putting up "blotches" where there are certain control codes ....

If using these editors doesn't help / they're not available, can I suggest you start a new PHP file with just the <?php and ?> in it, and cut and paste in sections of the code to narrow down the failure a bit.

By the way - I cut and pasted your code from the forum and ran it through PHP at the command line.   Did exactly what I expected - no MySQL server on the test machine, so it compiled and blew at the connection:

[graham@dhansak graham]$ php btest
Content-type: text/html

<b>Warning</b>:  MySQL Connection Failed: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)
in <b>bs</b> on line <b>21</b><br>
<b>Warning</b>:  MySQL:  A link to the server could not be established in <b>bs</b> on line <b>21</b><br>
: <BR><br>
<b>Warning</b>:  No MySQL-Link resource supplied in <b>bs</b> on line <b>28</b><br>

[graham@dhansak graham]$

I conclude that either it's something to do with the PHP version, or something that's in your file but not in your post (e.g. special characters!)

[Hey, jfp ... didn't mean to "overpost" you ... I think you and I were coming up with ideas at the same time!]

Posted by waygood (waygood), 1 July 2003
This may help you as it simplifies executing a query a little bit.

function doQuery($query)  

$location = "localhost";
$user = "user";
$password = "password";
$db_name = "database";

           // Create database connection
           $connection = mysql_connect($location ,$user ,$password ) or die ("Couldn't connect to database");

           // Select the database
           $db = mysql_select_db($db_name, $connection) or die ("Couldn't select database");

           // Execute SQL query and get result
           $fetch = mysql_query($query,$connection) or die ("Couldn't execute query " . $query);

       return $fetch;  
           return FALSE;

$sql="SELECT * FROM users";


$submit=="click" ---- You should be using $_POST['submit']=="click"

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho