Jump to content

Archived

This topic is now archived and is closed to further replies.

Cannibal_Monkey

Newbie problems

Recommended Posts

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">
<?php
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`) ;
        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]

Share this post


Link to post
Share on other sites
Once you connect to the database in php:
[code]
<?php
mysql_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

Share this post


Link to post
Share on other sites
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 4

Warning: 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 4
Could 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)

Share this post


Link to post
Share on other sites
You need to put your database connection info in before the code you were given

ie

[code=php:0]<?php

// YOUR DATABASE CONNECTION DATA HERE

mysql_query("CREATE DATABASE `a`") OR DIE ('Could not execute query: ' . mysql_error());
?>[/code]

Share this post


Link to post
Share on other sites
[quote author=sanfly link=topic=109633.msg442164#msg442164 date=1159328178]
You need to put your database connection info in before the code you were given

ie

[code=php:0]<?php

// YOUR DATABASE CONNECTION DATA HERE

mysql_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 4

Warning: 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 4
Could 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]

Share this post


Link to post
Share on other sites
This is definitely a database connection error, I get the same error all the time when I forget to include my database file

Can 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]

Share this post


Link to post
Share on other sites
[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">
<?php
if (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)

Share this post


Link to post
Share on other sites
So what happened?

For your database connection info, you need to have that before you execute a mysql query

I keep mine in a seperate file, db.php, which I include() before its needed each time

Heres 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]<?php
if (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]

Share this post


Link to post
Share on other sites
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 8
Couldn't connect to server."

I shouldn't have a username or password I don't think.

Share this post


Link to post
Share on other sites
Whatever your database name is

[code=php:0]$dbhost = 'localhost';
$dbusername = '<DATABASE NAME>';
$dbpasswd = '<USER NAME>';
$database_name = '<PASSWORD>';[/code]

Share this post


Link to post
Share on other sites
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.");
?>

<?php
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`)
        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?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.