Cannibal_Monkey Posted September 27, 2006 Share Posted September 27, 2006 I searched around, but I havn't found the answer. Basically there is a problem with my code, and I've had help but all the help has done was switch things from 1 problem to another. With this script below I have "Unknown database 'a'" as my problem. Basically the script is supposed to add a random number from 1 to 10 into a database (I am new to coding). Here is the script below:[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><?phpif (isset($_POST['update'])){ mysql_connect('localhost', 'root', ''); mysql_select_db('a') or die(mysql_error()); mysql_query('CREATE TABLE IF NOT EXISTS `b` ( `d` INT NOT NULL AUTO_INCREMENT , `c` INT NOT NULL , PRIMARY KEY ( `d` ) ) TYPE = innodb;') or die(mysql_error()); mysql_query('INSERT INTO `b`(`c`, `d`) ; VALUES (' . mt_rand(1, 10) . ', 1) ON DUPLICATE KEY UPDATE `c` = `c` + ' . mt_rand(1,10)) or die(mysql_error()); $q = mysql_query('SELECT `c` FROM `b` WHERE `d` = 1 LIMIT 1') or die(mysql_error()); $r = mysql_fetch_assoc($q); print_r($r);}?><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>This is my basic page!</title>Hello, if you got to this, you are hacking my computer. Please exit in a timely fashion otherwise I will be forced to <b>eat</b> your family. And I'll do it too. Don't believe me? Stay here then, when you get up to go to get something to eat, you'll see me cooking up your sister.</head><body><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"><input name="update" type="submit" value="Test" /></form></body></html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22187-newbie-problems/ Share on other sites More sharing options...
High_-_Tek Posted September 27, 2006 Share Posted September 27, 2006 Are you sure that the database 'a' exists? Quote Link to comment https://forums.phpfreaks.com/topic/22187-newbie-problems/#findComment-99340 Share on other sites More sharing options...
Cannibal_Monkey Posted September 27, 2006 Author Share Posted September 27, 2006 I was under the impression that this would create database a, how do I create it then? Quote Link to comment https://forums.phpfreaks.com/topic/22187-newbie-problems/#findComment-99356 Share on other sites More sharing options...
High_-_Tek Posted September 27, 2006 Share Posted September 27, 2006 Once you connect to the database in php:[code]<?phpmysql_query("CREATE DATABASE `a`") OR DIE ('Could not execute query: ' . mysql_error());?>[/code]Or go into phpMyAdmin or the MYSQL Command Line and create it Quote Link to comment https://forums.phpfreaks.com/topic/22187-newbie-problems/#findComment-99358 Share on other sites More sharing options...
Cannibal_Monkey Posted September 27, 2006 Author Share Posted September 27, 2006 When I add that code I get errors "Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in H:\Program Files (x86)\xampp\htdocs\Test.php on line 4Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in H:\Program Files (x86)\xampp\htdocs\Test.php on line 4Could not execute query: Access denied for user 'ODBC'@'localhost' (using password: NO)"I don't know why it's logging me in as ODBC though, I say root in the code, not ODBC. And when I create the database manually, I get this error."You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; VALUES (2, 1) ON DUPLICATE KEY UPDATE `c` = `c` + 2' at line 1" Now I am assuming the 2 is the random number because when I did it before, I got 4 in place of both 2s there. Now I am assuming the problem is C can't equal C+2 (because we learned that in Pre-Algebra) But I'm not sure what exactly to do to fix that. What do I do? I want C to be replaced by the orginal value of C + a random number between 1 and 10 (in this case 2) Quote Link to comment https://forums.phpfreaks.com/topic/22187-newbie-problems/#findComment-99386 Share on other sites More sharing options...
sanfly Posted September 27, 2006 Share Posted September 27, 2006 You need to put your database connection info in before the code you were givenie[code=php:0]<?php// YOUR DATABASE CONNECTION DATA HEREmysql_query("CREATE DATABASE `a`") OR DIE ('Could not execute query: ' . mysql_error());?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22187-newbie-problems/#findComment-99388 Share on other sites More sharing options...
Cannibal_Monkey Posted September 27, 2006 Author Share Posted September 27, 2006 [quote author=sanfly link=topic=109633.msg442164#msg442164 date=1159328178]You need to put your database connection info in before the code you were givenie[code=php:0]<?php// YOUR DATABASE CONNECTION DATA HEREmysql_query("CREATE DATABASE `a`") OR DIE ('Could not execute query: ' . mysql_error());?>[/code][/quote]That leaves me with exactly the same problem as described below...[quote author=Cannibal_Monkey link=topic=109633.msg442162#msg442162 date=1159328071]When I add that code I get errors "Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in H:\Program Files (x86)\xampp\htdocs\Test.php on line 4Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in H:\Program Files (x86)\xampp\htdocs\Test.php on line 4Could not execute query: Access denied for user 'ODBC'@'localhost' (using password: NO)"I don't know why it's logging me in as ODBC though, I say root in the code, not ODBC. And when I create the database manually, I get this error."You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; VALUES (2, 1) ON DUPLICATE KEY UPDATE `c` = `c` + 2' at line 1" Now I am assuming the 2 is the random number because when I did it before, I got 4 in place of both 2s there. Now I am assuming the problem is C can't equal C+2 (because we learned that in Pre-Algebra) But I'm not sure what exactly to do to fix that. What do I do? I want C to be replaced by the orginal value of C + a random number between 1 and 10 (in this case 2)[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/22187-newbie-problems/#findComment-99921 Share on other sites More sharing options...
sanfly Posted September 27, 2006 Share Posted September 27, 2006 This is definitely a database connection error, I get the same error all the time when I forget to include my database fileCan you post the code you are using for your database connection (with your user name and password removed of course)For the second error, try removing the ; in the code, hightlighed below in red[quote]if (isset($_POST['update'])){ mysql_connect('localhost', 'root', ''); mysql_select_db('a') or die(mysql_error()); mysql_query('CREATE TABLE IF NOT EXISTS `b` ( `d` INT NOT NULL AUTO_INCREMENT , `c` INT NOT NULL , PRIMARY KEY ( `d` ) ) TYPE = innodb;') or die(mysql_error()); mysql_query('INSERT INTO `b`(`c`, `d`) [color=red];[/color] VALUES (' . mt_rand(1, 10) . ', 1) ON DUPLICATE KEY UPDATE `c` = `c` + ' . mt_rand(1,10)) or die(mysql_error()); $q = mysql_query('SELECT `c` FROM `b` WHERE `d` = 1 LIMIT 1') or die(mysql_error()); $r = mysql_fetch_assoc($q); print_r($r);}[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/22187-newbie-problems/#findComment-99943 Share on other sites More sharing options...
Cannibal_Monkey Posted September 27, 2006 Author Share Posted September 27, 2006 [code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><?phpif (isset($_POST['update'])){ mysql_query("CREATE DATABASE `a`") OR DIE ('Could not execute query: ' . mysql_error()); mysql_connect('localhost', 'root', ''); mysql_select_db('a') or die(mysql_error()); mysql_query('CREATE TABLE IF NOT EXISTS `b` ( `d` INT NOT NULL AUTO_INCREMENT , `c` INT NOT NULL , PRIMARY KEY ( `d` ) ) TYPE = innodb;') or die(mysql_error()); mysql_query('INSERT INTO `b`(`c`, `d`) VALUES (' . mt_rand(1, 10) . ', 1) ON DUPLICATE KEY UPDATE `c` = `c` + ' . mt_rand(1,10)) or die(mysql_error()); $q = mysql_query('SELECT `c` FROM `b` WHERE `d` = 1 LIMIT 1') or die(mysql_error()); $r = mysql_fetch_assoc($q); print_r($r);}?><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>This is my basic page!</title>Hello, if you got to this, you are hacking my computer. Please exit in a timely fashion otherwise I will be forced to <b>eat</b> your family. And I'll do it too. Don't believe me? Stay here then, when you get up to go to get something to eat, you'll see me cooking up your sister.</head><body><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"><input name="update" type="submit" value="Test" /></form></body></html>[/code](I just removed the semicolon) Quote Link to comment https://forums.phpfreaks.com/topic/22187-newbie-problems/#findComment-99958 Share on other sites More sharing options...
sanfly Posted September 28, 2006 Share Posted September 28, 2006 So what happened?For your database connection info, you need to have that before you execute a mysql queryI keep mine in a seperate file, db.php, which I include() before its needed each timeHeres what the file has:[code=php:0]<? /* Database Information - Required!! *//* -- Configure the Variables Below --*/$dbhost = 'localhost';$dbusername = 'intergal';$dbpasswd = 'root';$database_name = '';/* Database Stuff, do not modify below this line */$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd") or die ("Couldn't connect to server."); $db = mysql_select_db("$database_name", $connection) or die("Couldn't select database.");?>[/code]Then in your main file[code=php:0]<?phpif (isset($_POST['update'])){include "db.php"; mysql_query("CREATE DATABASE `a`") OR DIE ('Could not execute query: ' . mysql_error()); mysql_query('CREATE TABLE IF NOT EXISTS `b` ( `d` INT NOT NULL AUTO_INCREMENT , `c` INT NOT NULL , PRIMARY KEY ( `d` ) ) TYPE = innodb;') or die(mysql_error()); mysql_query('INSERT INTO `b`(`c`, `d`) VALUES (' . mt_rand(1, 10) . ', 1) ON DUPLICATE KEY UPDATE `c` = `c` + ' . mt_rand(1,10)) or die(mysql_error()); $q = mysql_query('SELECT `c` FROM `b` WHERE `d` = 1 LIMIT 1') or die(mysql_error()); $r = mysql_fetch_assoc($q); print_r($r);}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22187-newbie-problems/#findComment-99962 Share on other sites More sharing options...
Cannibal_Monkey Posted September 28, 2006 Author Share Posted September 28, 2006 What do I put in place of integeral? I got this error when I tried to run the code before clicking the button with test on it. "Warning: mysql_pconnect() [function.mysql-pconnect]: Access denied for user 'intergal'@'localhost' (using password: YES) in H:\Program Files (x86)\xampp\htdocs\Test.php on line 8Couldn't connect to server."I shouldn't have a username or password I don't think. Quote Link to comment https://forums.phpfreaks.com/topic/22187-newbie-problems/#findComment-99963 Share on other sites More sharing options...
sanfly Posted September 28, 2006 Share Posted September 28, 2006 Whatever your database name is[code=php:0]$dbhost = 'localhost';$dbusername = '<DATABASE NAME>';$dbpasswd = '<USER NAME>';$database_name = '<PASSWORD>';[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22187-newbie-problems/#findComment-99987 Share on other sites More sharing options...
Cannibal_Monkey Posted September 28, 2006 Author Share Posted September 28, 2006 I figured it out a few min ago (sorry for not saying). I think you got the last 3 parts switched around. Thanks for helping me get the script actually working (I spend a lot of time on it before I even came here. I know its some simple script but it's the first code I've made of any coding language)Anyway I have edited the code a little so now it's this:[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><? $dbhost = 'localhost';$dbusername = 'root';$dbpasswd = '';$database_name = 'a';$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd") or die ("Couldn't connect to server.");$db = mysql_select_db("$database_name", $connection)or die("Couldn't select database.");?><?phpif (isset($_POST['update'])){ mysql_connect('localhost', 'root', ''); mysql_select_db('a') or die(mysql_error()); mysql_query('CREATE TABLE IF NOT EXISTS `b` ( `d` INT NOT NULL AUTO_INCREMENT , `c` INT NOT NULL , PRIMARY KEY ( `d` ) ) TYPE = innodb;') or die(mysql_error()); mysql_query('INSERT INTO `b`(`c`, `d`) VALUES (' . mt_rand(1, 3) . ', 0) ON DUPLICATE KEY UPDATE `c` = `d` + ' . mt_rand(1,3)) or die(mysql_error()); $q = mysql_query('SELECT `c` FROM `b` WHERE `c` = 0 LIMIT 99999') or die(mysql_error()); $r = mysql_fetch_assoc($q); print_r($r);}?><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Berry Gatherer</title></head><body><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"><input name="update" type="submit" value="Gather Berries" /></form></body></html>[/code]In the database it creates new D and C files each time you hit the button. That's what I want but I want it to add up all the Cs and desplay it on the page as variable E, and the last variable C in a different part of the code. I'm not sure how to go about creating variable E or how to desplay it. I tried a few things involving $c=mysql_query($query); for desplaying c. Am I even close with that? Quote Link to comment https://forums.phpfreaks.com/topic/22187-newbie-problems/#findComment-99996 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.