New to Mysql connection

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
daregazi
Forum Newbie
Posts: 15
Joined: Mon Dec 08, 2008 1:49 pm

New to Mysql connection

Post by daregazi »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi,

I am new to PHP&Mysql.

I have just installed mysql 5 and php 5.2.6

phpinfo() works fine, and I have created a simple mysql-connection php file as:

Code: Select all

 
<?php
 
define('DB_USER', 'lottt');
define('DB_PASSWORD', 'lottt');
define('DB_HOST', 'localhost');
define('DB_NAME', 'lottt');
$dbc = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Could not connect to MYSQL: ' . mysql_error());
@mysql_select_db(DB_NAME) or die('Could not select the database: ' . mysql_error());
echo 'done!';
?>
the user, password and db name are correct.
I have some problems here.
first of all, it just runs to the connection command without showing any error messages. however it ignores anything appearing after the connection command, e.g. here showing the 'done!' message. so by browsing to this page, the result is a blank page!

Secondly in IE sometimes it gives me HTTP 500 internal error, while another time it runs without any change. This is not the case in Firefox.

Would anyone please help me to get started?
Very thankful!


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: New to Mysql connection

Post by pickle »

Take off the '@' symbols & maybe you'll see something useful. It's never a good idea to use '@' in development.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: New to Mysql connection

Post by califdon »

As pickle said, remove those "@"s in front of your commands while you are developing and debugging your code. That's for use when it has been completely tested, but you want to avoid sending error messages out to users of your production version.

I would use simple variables for your connection values, rather than define constants. Something like this:

Code: Select all

$dbuser='lottt';
$dbpwd='lottt';
$dbhost='localhost';
$dbname='lottt';
$dbc = mysql_connect('$dbhost','$dbuser','$dbpwd') or die('Could not connect to MYSQL: ' . mysql_error());
mysql_select_db('$dbname') or die('Could not select the database: ' . mysql_error());
daregazi
Forum Newbie
Posts: 15
Joined: Mon Dec 08, 2008 1:49 pm

Re: New to Mysql connection

Post by daregazi »

Thanks you both for your replies.
I listened to your advice. but no hope regarding my problems yet.

It seems a bit strange to me. because even I changed the user password to something invalid, still no error message in return!!

Code: Select all

<?php
//phpinfo();
echo 'hello';
$dbuser='l';
$dbpwd='l';
$dbhost='localhost';
$dbname='l';
$dbc = mysql_connect('$dbhost','$dbuser','$dbpwd') or die('Could not connect to MYSQL: ' . mysql_error());
mysql_select_db('$dbname') or die('Could not select the database: ' . mysql_error());
echo 'done!';
?>
 
 
I do not know why you califdon have put the variables $dbhost $dbuser and $dbpwd arguments inside ' ', however I checked both with and without ' ' and no difference!

Also I do not know what is wrong with this simple php code that sometimes IE gives http 500 internal error. but never Firefox!!
Therefore the result of the above script in Firefox is always just a hello message and in IE mostly http 500 internal error.

I appreciate your help!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: New to Mysql connection

Post by califdon »

Oops, my slip-up, those ' ' should not have enclosed the variables.

I don't see what would cause the problem you are describing, but first I would eliminate the possibility that there's a non-printing character in there that's causing the parser to die, by starting a new file and entering your code again, since it's a small file. You should be using a plain text editor; a word processor may introduce control characters that will screw up a script parser.

Ordinarily when a php script fails to send anything to the browser, it's because a syntax error has caused the parser to just quit, so it sends nothing to the web server. That is consistent with displaying just "hello" (in FF), suggesting that one of the following php lines has a problem, so nothing beyond that point was sent to the web server. But your script is so short and I don't see any syntax errors (apart from the one I mistakenly suggested, but you said you tried it without the quotes, too).

OK, leave the "hello" at the top and the "done" at the end, and leave the assignment of the variables, but comment out the 2 mysql_ command lines, and see if both the "hello" and "done" are echoed properly. If they do, then uncomment the first mysql_ command line and try again.
daregazi
Forum Newbie
Posts: 15
Joined: Mon Dec 08, 2008 1:49 pm

Re: New to Mysql connection

Post by daregazi »

without mysql commands everything is ok.

I have many php pages that are only combination of php and html codes (I mean without mysql commands) which are working perfectly.
Now I'd like to add some pages and queries by connecting to mysql. and this is my first step to mysql which fails.
before these problems I kept getting 'Fatal error: Call to undefined function mysql_connect()' which was problem with loading the right php.ini file. I reinstalled php and reconfigured the ini file and resolved it.

I guess there is still something wrong with the php.ini file in using extensions :?

here is part of my phpinfo() result:


Configuration
PHP Core
Directive Local Value Master Value
allow_call_time_pass_reference Off Off
allow_url_fopen On On
allow_url_include Off Off
always_populate_raw_post_data Off Off
arg_separator.input & &
arg_separator.output & &
asp_tags Off Off
auto_append_file no value no value
auto_globals_jit On On
auto_prepend_file no value no value
browscap no value no value
default_charset no value no value
default_mimetype text/html text/html
define_syslog_variables Off Off
disable_classes no value no value
disable_functions no value no value
display_errors Off Off
display_startup_errors Off Off
doc_root C:\apache\htdocs C:\apache\htdocs
docref_ext no value no value
docref_root no value no value
enable_dl On On
error_append_string no value no value
error_log no value no value
error_prepend_string no value no value
error_reporting 6143 6143
expose_php On On
extension_dir C:\php\ C:\php\
file_uploads On On
highlight.bg #FFFFFF #FFFFFF
highlight.comment #FF8000 #FF8000
highlight.default #0000BB #0000BB
highlight.html #000000 #000000
highlight.keyword #007700 #007700
highlight.string #DD0000 #DD0000
html_errors On On
ignore_repeated_errors Off Off
ignore_repeated_source Off Off
ignore_user_abort Off Off
implicit_flush Off Off
include_path .;c:\php\ .;c:\php\
log_errors On On
log_errors_max_len 1024 1024
magic_quotes_gpc Off Off
magic_quotes_runtime Off Off
magic_quotes_sybase Off Off
mail.force_extra_parameters no value no value
max_execution_time 30 30
max_input_nesting_level 64 64
max_input_time 60 60
memory_limit 128M 128M
open_basedir no value no value
output_buffering 4096 4096
output_handler no value no value
post_max_size 8M 8M
precision 14 14
realpath_cache_size 16K 16K
realpath_cache_ttl 120 120
register_argc_argv Off Off
register_globals On On
register_long_arrays Off Off
report_memleaks On On
report_zend_debug On On
safe_mode Off Off
safe_mode_exec_dir no value no value
safe_mode_gid Off Off
safe_mode_include_dir no value no value
sendmail_from no value no value
sendmail_path no value no value
serialize_precision 100 100
short_open_tag Off Off
SMTP localhost localhost
smtp_port 25 25
sql.safe_mode Off Off
track_errors Off Off
unserialize_callback_func no value no value
upload_max_filesize 2M 2M
upload_tmp_dir no value no value
user_dir no value no value
variables_order GPCS GPCS
xmlrpc_error_number 0 0
xmlrpc_errors Off Off
y2k_compliance On On
zend.ze1_compatibility_mode Off Off


please anyone reply me with a solution.
Thanks
daregazi
Forum Newbie
Posts: 15
Joined: Mon Dec 08, 2008 1:49 pm

Re: New to Mysql connection

Post by daregazi »

I want to know is there any diagnostic command to check the mysql connectivity from within a piece of php code other than mysql_connect function???

whatever mysql commands I am using between two echo commands, the result is only first echo without any error message, even with wrong parameters such as the following code:

Code: Select all

 
<?php
echo 'Hello';
mysql_connect() or die ('Error');
echo 'Done';
?>
if I comment the mysql line, then the result is 'HelloDone'.
shall I change anything in php.ini?

I fed up.Please somebody help...
daregazi
Forum Newbie
Posts: 15
Joined: Mon Dec 08, 2008 1:49 pm

Re: New to Mysql connection

Post by daregazi »

Hi,

I just managed to solve the above mysql problems.
I played a bit with the php.ini file.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: New to Mysql connection

Post by califdon »

daregazi wrote:Hi,

I just managed to solve the above mysql problems.
I played a bit with the php.ini file.
Congratulations! I'm glad you got over that obstacle. It can be very discouraging when nothing seems to work! I was just going to respond and tell you that the earlier process pointed to a configuration issue, either with PHP or MySQL.

Now, to help others who may be struggling with the same kind of problem, would you please tell us specifically what line or lines you needed to add or change to your php.ini file to relieve the problem? Thanks.
Post Reply