Jump to content

[SOLVED] Problems executing queries / returns blank page


Recommended Posts

Hi there, I'm new to PHP - normally an ASP guy but trying to get something setup in php for a change.

 

We're running a Windows 2003 Server with PHP & MySql installed. I know PHP works (phpinfo()) and I know MySql works (use it for all the .asp apps) and phpmyadmin works, however whenever trying to display query results from our own pages we are greeted with blank pages. Please see below, hope you can help!

 

 

When running this code we get a blank page, no errors or anything

// Connect to the DB
$link = mysqli_connect("IPAddress", "User", "Password", "dbname"); 
// Note the fields above are populated correctly usually, but filtered for this forum post.

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* Create table doesn't return a resultset */
if (mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
    printf("Table myCity successfully created.\n");
}

/* Select queries return a resultset */
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) {
    printf("Select returned %d rows.\n", mysqli_num_rows($result));

    /* free result set */
    mysqli_free_result($result);
}

/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = mysqli_query($link, "SELECT * FROM City", MYSQLI_USE_RESULT)) {

    /* Note, that we can't execute any functions which interact with the
       server until result set was closed. All calls will return an
       'out of sync' error */
    if (!mysqli_query($link, "SET @a:='this will not work'")) {
        printf("Error: %s\n", mysqli_error($link));
    }
    mysqli_free_result($result);
}

mysqli_close($link);

 

 

However when running

 

<?php
// Connect to the DB

if(!$db=mysqli_connect("ipaddress", "user", "password", "dbname")){
    throw new Exception('Error connecting to host.'.mysqli_connect_error());
}
// display host information
echo 'Host information: '.mysqli_get_host_info($db);
echo '<br/>hello';
// close connection
mysqli_close($db);
?>

 

I get shown the Host information just fine. If I add to that code, at the bottom, then I don't see this second "Hello".

 

$result=$mysqli->query('SELECT * FROM customer');
echo '<br/>hello';

 

Any idea what could be causing this? and how to solve.

Without examining your code I can tell you the most of the time a blank page usually refers to a fatal syntactical error that's being suppressed.

 

After your first opening <?php tag add these lines:

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

Same situation even after adding that for the first code block I posted (which it should be noted I got from http://ie2.php.net/manual/en/mysqli.query.php - simply to test things were working!)

 

The second block did return an error but then that was as you said an error on my part.

 

PS - You can see the results of the phpinfo page here http://78.129.162.2/phptest.php - does it look ok?

You should be learning php, developing php code, and debugging php code on a system where error_reporting is set to E_ALL and display_errors is set to ON in your php.ini (stop and start your web server to get changes made to php.ini to take effect.) Fatal parse errors are not displayed when those two settings are made in a script because the script never executes.

 

Your mysql extension is probably not enabled, giving a fatal runtime error. But find out for sure by turning on full php error reporting/display errors in php.ini (or a .htaccess file - Apache, or in a local php.ini - php running as a CGI application.)

Certainly can... please see below

<?php
$mysqli = new mysqli("IP", "root", "PWD", "DBNAME");


/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* Create table doesn't return a resultset */
if ($mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
    printf("Table myCity successfully created.\n");
}

/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) {
    printf("Select returned %d rows.\n", $result->num_rows);

    /* free result set */
    $result->close();
}

/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = $mysqli->query("SELECT * FROM City", MYSQLI_USE_RESULT)) {

    /* Note, that we can't execute any functions which interact with the
       server until result set was closed. All calls will return an
       'out of sync' error */
    if (!$mysqli->query("SET @a:='this will not work'")) {
        printf("Error: %s\n", $mysqli->error);
    }
    $result->close();
}

$mysqli->close();
?>

thanks for help so far guys, have turned on those settings in php.ini as requested, same situation for me though - blank pages.

 

So, did you stop and start your web server to get any change made to php.ini to take effect, is the php.ini that you are changing the one that php is using, and did you use a phpinfo() statement to confirm that the settings are being used?

 

Also, how did you obtain php, the .msi installer or the .zip package?

I did stop/start yes and the phpinfo showed the changes made. (you can see the phpinfo page here http://78.129.162.2/phptest.php)

 

Am not sure what was used to install, someone did it for me when I brought the server. I've just had a look in the Administrator users documents and there is both a 5.2.5 zip and an .msi file there though! Is there anyway I can find out short of asking them (they wont be in again til Tuesday)

I looked at your phpinfo() link. Php 5.2.4 or 5.2.5 (I don't remember which) introduced a serious bug when output buffering is on in php.ini. Fatal errors are not reported because the stupid programmer that added a feature forgot to call the code to flush the output buffer when a fatal error stops execution.

 

You should upgrade to the latest php version or at a minimum turn off output buffering in php.ini.

 

Edit: And your page might be blank because the code is taking an execution path that is not outputting anything.

 

Edit2: I just checked and the bug I mentioned was in php 5.2.4 and was corrected in 5.2.5.

As to the .msi installer, I have no proof (and I'm not about to screw up my system by using the .msi installer as a test), but it seems like it puts settings into the Windows registry that override php.ini settings and the phpinfo() shows what the php.ini settings are and not the actual settings.

 

Edit: If you go into the Windows control panel and there is an add/remove item for php, it means that php was installed using the .msi installer. If that is the case, you must use the add/remove item to enable extensions, like mysqli. Otherwise they won't actually get enabled.

cheers for all the help, have gone into CP but nothing in there, so presume was installed via the .zip package.

 

We're getting somewhere though, looked at what I'd got off that site and yes there were no else statements so added some in and it posts the error for everything except the initial connection.

 

So, for some reason then I can connect but beyond that can't do anything... is there a common reason for this? Is the code i've got off that site simply wrong for calling from a table?

 

I'm now using the below... I know I have read/write permission on the DB as I'm logging in as root

 

 

<?php
$mysqli = new mysqli("ip", "root", "pwd", "dbname");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* Create table doesn't return a resultset */
if ($mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
    printf("Table myCity successfully created.\n");
}
else
{
print "Error 1";
}
/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) {
    printf("Select returned %d rows.\n", $result->num_rows);

    /* free result set */
    $result->close();
}
else
{
print "Error 2";
}
/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = $mysqli->query("SELECT * FROM City", MYSQLI_USE_RESULT)) {

    /* Note, that we can't execute any functions which interact with the
       server until result set was closed. All calls will return an
       'out of sync' error */
    if (!$mysqli->query("SET @a:='this will not work'")) {
        printf("Error: %s\n", $mysqli->error);
    }
    $result->close();
}
else
{
print "Error 3";
}
$mysqli->close();
?>

 

 

 

ok to update got it working - thank you to PFMaBiSmAd for pointing out the lack of Else statements, got me on the right path.

 

After that I could see the mysql errors that the insert statement was wrong, so I changed it to one which worked, as a result the select then worked etc etc

Just a side note, most if() conditional tests need an else statement to do something when the test fails, even if it is just to output a meaningful user message that the code could not complete its designed task.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.