Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Connecting MySQL & PHP - problem

Options
  • 27-02-2012 9:11pm
    #1
    Registered Users Posts: 33


    I am having problems trying to connect my php script and mysql together. Every time I try and run the script, all I get is a blank white screen. I've tried so many different ways of php scripts and even all the simple and basic codes just to connect mysql and php. Anyone have an idea how to solve this problem please?

    One of the codes I tried is as the following:

    <?php

    $con = mysql_connect("localhost","root","password");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    ?>


Comments

  • Registered Users Posts: 5,246 ✭✭✭conor.hogan.2


    It is probably connecting and therefore not printing anything?


  • Registered Users Posts: 33 ciaramkm


    I even tried other codes to print something and it's still the exact same problem,

    One example code I used is:

    <?php

    $connect = mysql_connect("localhost", "root", "password") or die (mysql_error());

    mysql_select_db("localfyp", $connect) or die (mysql_error());

    $quey1="select * from idnumbers";

    echo $quey1;

    $result=mysql_query($quey1, $connect) or die(mysql_error());


    echo "Hello1";


    ?>


  • Closed Accounts Posts: 249 ✭✭OneIdea


    Do you have data stored in the database?

    Whats the name of the database and table/s?

    What information do you want to retrieve from the database?


  • Registered Users Posts: 894 ✭✭✭Dale Parish


    What you've posted shouldn't print anything on screen.
    Try:

    <?php

    $con = mysql_connect("localhost","root","password") or die(mysql_error());
    if ($con)
    {
    echo "Connected";
    }


    ?>


  • Registered Users Posts: 33 ciaramkm


    Just tried that and still no luck :(


  • Advertisement
  • Registered Users Posts: 33 ciaramkm


    OneIdea wrote: »
    Do you have data stored in the database?

    Whats the name of the database and table/s?

    What information do you want to retrieve from the database?

    Yes I have data stored in the database called localfyp and table called idnumbers. Information is id_number, room_no, module_code, and time.


  • Registered Users Posts: 5,246 ✭✭✭conor.hogan.2


    It is a simple if/else.

    It either connects or it does not, print out a "connected" in the if and a "not connected" in the else and that will tell you where the problem lies.


  • Closed Accounts Posts: 347 ✭✭quietriot


    Try the following:
    <?php
    	ob_start();
    	$conn = mysql_connect("localhost", "root", "password");
    	if(!$conn){
    		die("Database Connection Failed: " . mysql_error());
    	}
    	
    	$db = mysql_select_db("database_name", $conn);
    	if(!$db){
    		die("Database Selection Failed: " . mysql_error());
    	}
    
    	$query=mysql_query("SELECT * from table", $conn);
    		if(!$query){
    			die("Database Query Failed: " . mysql_error());
    		}
    		
    ?>
    

    That should throw up some specific errors. If nothing happens then everything executed fine.


  • Closed Accounts Posts: 249 ✭✭OneIdea


    In to a .php file

    [PHP]
    <?php
    $con = mysql_connect("localhost","Root","Password");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("localfyp", $con);

    $result = mysql_query("SELECT * FROM idnumbers");

    while($row = mysql_fetch_array($result))
    {
    echo $row . " " . $row;
    echo "<br />";
    }

    mysql_close($con);


    ?>
    [/PHP]


  • Registered Users Posts: 33 ciaramkm


    Thanks guys, I appreciate it. Unfortunately I'm still having no luck especially with all the codes ye gave me. It just keeps showing a white blank screen. I was thinking could it be something to do with priviledges in MySQL????


  • Advertisement
  • Registered Users Posts: 2,793 ✭✭✭oeb


    ciaramkm wrote: »
    Thanks guys, I appreciate it. Unfortunately I'm still having no luck especially with all the codes ye gave me. It just keeps showing a white blank screen. I was thinking could it be something to do with priviledges in MySQL????


    You are getting a blank white screen because errors are being thrown, and the messages are being suppressed. Error reporting is turned off be default in a lot of situations.

    Put this at the top of your code.

    [PHP]error_reporting(E_ALL); ini_set('display_errors', '1');[/PHP]


  • Registered Users Posts: 33 ciaramkm


    oeb wrote: »
    You are getting a blank white screen because errors are being thrown, and the messages are being suppressed. Error reporting is turned off be default in a lot of situations.

    Put this at the top of your code.

    [PHP]error_reporting(E_ALL); ini_set('display_errors', '1');[/PHP]

    This helped me to figure out what was wrong after adding the line above into the code and it turns out that php was not installed properly for when connecting with MySQL so I reinstalled php again and all the codes are finally working :D!! Thank you for your help and to everyone that commented as well. :)


Advertisement