473,411 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

How to connect to MySQL in PHP

I'm a newbie at this so if someone could please help me out:
I have : PHP Version 5.2.3, Apache 2.2.4, and mySQL Version 4.1.22 on Windows XP.

I have PHP working independantly. I have mySQL client working perfectly by itself. When trying to connect to the mySQL database, using PHP, i don't get anything: only a blank page. I need help! I've been struggling for 22hrs now trying to get it to work.

This is what i've done:
I created a user in mySQL client that has basic privalages on "sitename" database that i've created using the following:
Expand|Select|Wrap|Line Numbers
  1. GRANT SELECT, INSERT,UPDATE, DELETE ON sitename. * TO 'mathurinjerry'@'localhost' IDENTIFIED BY 'yellow83';
  2.  
  3. FLUSH PRIVILEGES; //to apply changes
  4.  
Then when i tried to create a php script, and uploaded it in my server, all I get is a blank page.

This is the script i used:
[PHP]<?php
echo mysql_connect ('localhost','mathurinjerry','yellow83') or die('Cannot connect to database because:'.mysql_error());
?>[/PHP]

Can someone please help me PLEASE!
Aug 25 '07 #1
8 2224
gregerly
192 Expert 100+
I'm a newbie at this so if someone could please help me out:
I have : PHP Version 5.2.3, Apache 2.2.4, and mySQL Version 4.1.22 on Windows XP.

I have PHP working independantly. I have mySQL client working perfectly by itself. When trying to connect to the mySQL database, using PHP, i don't get anything: only a blank page. I need help! I've been struggling for 22hrs now trying to get it to work.

This is what i've done:
I created a user in mySQL client that has basic privalages on "sitename" database that i've created using the following:

GRANT SELECT, INSERT,UPDATE, DELETE ON sitename. * TO 'mathurinjerry'@'localhost' IDENTIFIED BY 'yellow83';

FLUSH PRIVILEGES; //to apply changes

Then when i tried to create a php script, and uploaded it in my server, all I get is a blank page.

This is the script i used:

[PHP]<?php
echo mysql_connect ('localhost','mathurinjerry','yellow83') or die('Cannot connect to database because:'.mysql_error());
?>[/PHP]

Can someone please help me PLEASE!
By the looks of your script, I'de say you are probably connecting. The mysql_connect should be returning true or false depending on if it can connect or not, if not, it would echo "Cannot connect to database because...", so I would venture a guess that it is connecting.

Try something more useful, like:

[PHP]<?

$con=mysql_connect('localhost','mathurinjerry','ye llow83');

if($con){
echo "connected succesfully";
}else{
echo "could not connect to the database";
}

?>[/PHP]
This is essentially what you are doing with the die statement, but you are testing the value of $con which should be true if it connects.

Also: Work on your post titles. Pbmods will be following promptly to tell you the same thing.

Greg
Aug 25 '07 #2
Atli
5,058 Expert 4TB
I have edited the thread's title to better describe it's contents.
Please avoid using words like 'help me' when you name your threads, as they are
less likely to attract the attention of our Experts, as well as our other members. Not to mention that other people facing similar problems will not be able to find it.

Please take a look at the Posting Gudelines, specifically the part about How to use a good thread title, before posting in these forums.

Moderator
Aug 25 '07 #3
pbmods
5,821 Expert 4TB
Heya, Jerry. Welcome to TSDN!

If you're getting a blank page, your script is probably generating an error. Check out this article to find out what is going on.
Aug 26 '07 #4
ak1dnar
1,584 Expert 1GB
I think your Php is working fine in your host. is it?

First try this to add these two lines to the top of the Php Pages. Some times error reporting might be disabled on the php.ini.
Expand|Select|Wrap|Line Numbers
  1. error_reporting(E_ALL);
  2. ini_set('display_errors', True);
  3. // Remaining Codes....
  4.  
[EDIT: Sorry Pbmods, this post is from a mirror thread, So the answer is same as your.]
Aug 26 '07 #5
Heya, Jerry. Welcome to TSDN!

If you're getting a blank page, your script is probably generating an error. Check out this article to find out what is going on.
i tried this and i still get nothing but a blank page:
[PHP]
<? error_reporting (E_ALL);
ini_set('display_errors', TRUE);
$con=mysql_connect('localhost','__MUNGED__','__MUN GED__');
if($con){
echo"connected successfully";
}else{
echo"could not connect to the database";
}
?>

[/PHP]
Aug 27 '07 #6
pbmods
5,821 Expert 4TB
Heya, Jerry.

Not a fan of whitespace, I see. If you're curious, developers have tested this, and whitespace does not affect script execution time to any significant degree.

Out of curiosity, does this output anything:
Expand|Select|Wrap|Line Numbers
  1. <?php // Short tags are not universally supported.
  2. error_reporting (E_ALL);
  3. ini_set('display_errors', TRUE);
  4. /*
  5. $con=mysql_connect('localhost','__MUNGED__','__MUN  GED__');
  6. if($con){
  7.    echo"connected successfully";
  8.   }else{
  9.    echo"could not connect to the database";
  10.   }
  11. */
  12.  
  13. echo 'Well, at least this works....';
  14. ?>
  15.  
Aug 27 '07 #7
Did anyone ever get this figured out? I am having the same trouble on all my sites that connect to a mysql database ver 4.1 . I am with Server Beach and everything was running fine untill I had them upgrade my database to 4.1. I know my code should work but It's driving me mad and of course they are blaiming it on my code!

Thanks All ~

Craig
Oct 24 '07 #8
Atli
5,058 Expert 4TB
Hi Craig. Welcome to TSDN!

If everything ran fine on an earlier version of MySQL your PHP code should be fine. It is possible, however, that your SQL queries are incompatible with your MySQL version.

Are you still using the same PHP version and configuration?
Have you checked out this article?

Could you post an example of the code that is not working?
Oct 24 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Mr Dygi | last post by:
Hi, PHP 4.3.4 installed manually from package *.zip and Apache 2. I have a problem with this simple code: <?php $link = mysql_connect("127.0.0.1","","") or die("Could not connect: " ....
0
by: Ryan Stewart | last post by:
I'm running MySQL server on a WinXP box at home. I have a cable modem/router. I've set up port forwarding on port 3306 to the machine running MySQL. I can connect locally just fine, but when I try...
3
by: Pugi! | last post by:
On a freshly installed Fedora C3 (incl. webserver apache php mysql) i get the following problem when connecting to mysql through a browser (phpMyAdmin): : #2002 Can't connect to local MySQL...
1
by: jiing | last post by:
Now let me describe what I have done and my purpose: Originally, I want to user ports to install phpBB But I found that phpBB doesn't support mysql 5.x (but the ports installed mySQL 5.0.0...
2
by: xiuchuanli | last post by:
I install Fedora 4 with web server, after that I installed mysql 5.0.15. Now I can connect to mysql from command line and mysql-administrator and mysql-query-browser. But when I try to test to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.