vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Can't connect to database right (PHP/MySQL) (https://vborg.vbsupport.ru/showthread.php?t=82929)

TruthElixirX 06-12-2005 01:18 AM

Can't connect to database right (PHP/MySQL)
 
Code:

<?php

$username = "cms";
$password = "***INSERT PASS HERE****";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
        or die("Unable to connect to MySQL");
print "Connected to MySQL<br>";

// make CMS the current db
mysql_select_db('CMS');
    or die ("Unable to connect to CMS");
print "Connected to CMS";
{
$comments = mysql_real_escape_string($_GET['comments']);
$body = mysql_real_escape_string($_GET['body']);
$title = mysql_real_escape_string($_GET['title']);

mysql_query('INSERT INTO articles (articlename , body , comments) values ($title, $body, $comments)');
}
mysql_close('CMS');
?>

I keep getting errors. I keep trying so..can someone show me what I'm doing wrong..and yes I knowq the code is a mess..I don't need clean code as of now..jsut functional code..>.
<

Adrian Schneider 06-12-2005 01:30 AM

Try:
PHP Code:

<?php

$username 
"cms";
$password "CENSORED";
$hostname "localhost";
$dbh mysql_connect($hostname$username$password);
if (!
$dbh)
{
    echo 
'Unable to connect';
}
print 
"Connected to MySQL<br>";

// make CMS the current db
$db mysql_select_db('CMS');
if (!
$db)
{
    print 
"Unable to connect to CMS";
}
print 
"Connected to CMS";

$comments mysql_real_escape_string($_GET['comments']);
$body mysql_real_escape_string($_GET['body']);
$title mysql_real_escape_string($_GET['title']);

mysql_query("
    INSERT INTO articles (articlename , body , comments) values ('
$title', '$body', '$comments'
"
);

mysql_close("$dbh");
?>


TruthElixirX 06-12-2005 01:32 AM

Can you show me what you changed? Cause I'm really trying to learn how o do this on my own.

EDIT:: Oops..forgot to say thanks..so Thanks. :D


EDIT 2:: After upladoing that and trying to get this ot work I got this error:

Parse error: parse error, unexpected T_LOGICAL_OR in /home/httpd/vhosts/supportcore.org/httpdocs/mycms/articlesubmit.php on line 12

Also I just noticed I put my DB password on here...crap..Could you edit it out for me? In ur post I mean

Adrian Schneider 06-12-2005 01:36 AM

No problem, does it work? :)

I'm not sure why you put { after print and } after the the $title line. Wasn't supposed to be there, and I just cleaned up your query syntax a bit (added 's and put the whole thing in double quotes).

Try updated one...

TruthElixirX 06-12-2005 01:40 AM

Thanks. No it didn't work. See edits. :p.

But thanks for the explanation.


EDIT:: Didn't work again:

Parse error: parse error, unexpected T_IF in /home/httpd/vhosts/supportcore.org/httpdocs/mycms/articlesubmit.php on line 7

Adrian Schneider 06-12-2005 01:43 AM

Check it again, should work. Time to do dishes though, bye.

TruthElixirX 06-12-2005 01:53 AM

Hmm... I was geting some errors but fixed them and now only have one and it is this:

Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/httpd/vhosts/supportcore.org/httpdocs/mycms/articlesubmit.php on line 29


Also it doesn't seem to be adding the data to the table. It is just conencting to the database and then not closing. It doesn't seem to be doing anything with the user input.

http://www.supportcore.org/mycms/submitarticle.html

And yes..I'm working on a CMS...And yes..I know I'm a super long ways off.


EDIT:: Hmm..I haven't changed anything and I tried again out of sheer curiosity and got the following erros:

Warning: mysql_connect(): Access denied for user: 'cms@localhost' (Using password: YES) in /home/httpd/vhosts/supportcore.org/httpdocs/mycms/articlesubmit.php on line 6
Unable to connectConnected to MySQL

Warning: mysql_select_db(): Access denied for user: 'apache@localhost' (Using password: NO) in /home/httpd/vhosts/supportcore.org/httpdocs/mycms/articlesubmit.php on line 14

Warning: mysql_select_db(): A link to the server could not be established in /home/httpd/vhosts/supportcore.org/httpdocs/mycms/articlesubmit.php on line 14
Connected to CMS
Warning: mysql_real_escape_string(): Access denied for user: 'apache@localhost' (Using password: NO) in /home/httpd/vhosts/supportcore.org/httpdocs/mycms/articlesubmit.php on line 21

Warning: mysql_real_escape_string(): A link to the server could not be established in /home/httpd/vhosts/supportcore.org/httpdocs/mycms/articlesubmit.php on line 21

Warning: mysql_real_escape_string(): Access denied for user: 'apache@localhost' (Using password: NO) in /home/httpd/vhosts/supportcore.org/httpdocs/mycms/articlesubmit.php on line 22

Warning: mysql_real_escape_string(): A link to the server could not be established in /home/httpd/vhosts/supportcore.org/httpdocs/mycms/articlesubmit.php on line 22

Warning: mysql_real_escape_string(): Access denied for user: 'apache@localhost' (Using password: NO) in /home/httpd/vhosts/supportcore.org/httpdocs/mycms/articlesubmit.php on line 23

Warning: mysql_real_escape_string(): A link to the server could not be established in /home/httpd/vhosts/supportcore.org/httpdocs/mycms/articlesubmit.php on line 23

Warning: mysql_query(): Access denied for user: 'apache@localhost' (Using password: NO) in /home/httpd/vhosts/supportcore.org/httpdocs/mycms/articlesubmit.php on line 27

Warning: mysql_query(): A link to the server could not be established in /home/httpd/vhosts/supportcore.org/httpdocs/mycms/articlesubmit.php on line 27

Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/httpd/vhosts/supportcore.org/httpdocs/mycms/articlesubmit.php on line 29

But anyways..any reason why it isn't donig anything with the user input?

Adrian Schneider 06-12-2005 02:22 AM

Try the updated PHP code again.

Those errors are from insufficient permission to the db.

TruthElixirX 06-12-2005 02:29 AM

I figured out the problem with insuffiecnt privilages. I changed my db pass and forgot to update it. But it still doesn't seem to be sending anything to the database....I double checked to make sure I was lookngi in the right one and I was...hmm...>.<...

And for soem erason it still isn't closing the DB conenction.

Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/httpd/vhosts/supportcore.org/httpdocs/mycms/articlesubmit.php on line 29

Could the reason it isn't submitting to the database be an error in my HTML coding?

Adrian Schneider 06-12-2005 02:32 AM

Try removing quotes from that last line (around $dbh). Your HTML looked fine when I glanced at it. I'll look tomorrow when I'm not half asleep.


All times are GMT. The time now is 05:28 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01596 seconds
  • Memory Usage 1,755KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (1)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete