Page 1 of 1

My first PHP script

Posted: Mon Nov 30, 2009 5:38 am
by thirstylocket
Hi,

I've recently written some really basic php to retrieve a few rows from a database but it doesn't seem to to work. I've gone through the thread 'Debugging MySQL code .. a few tips.' and included all of those debugging tips but I still cant work out why no data is returned.

I'm sure its something really simple Ive over looked and wondered if someone can look at my code, below, and spot any mistakes I may have made?

This is my code: -

Code: Select all

 
<?php 
    echo 'start<br>';
 
    error_reporting(E_ALL);
    
    if (($_SERVER['SERVER_NAME']!="www.blah,blah.com"))
    { 
            die(); 
    }
 
    $username = "username"; 
    $pwd          = "password"; 
    $host          = "IP Address"; 
    $dbname    = "database name"; 
        
    if (!($conn=mysql_connect($host, $username, $pwd)))  
    { 
        printf("error connecting to DB by user = $username and pwd=$pwd"); 
        exit; 
    }
         
    $db     = mysql_select_db($dbname,$conn) or die("Unable to connect to database1"); 
    
    $sql    = "SELECT id, subgame_id, name, value FROM scores LIMIT 0 , 30"; 
        
    $result = mysql_query($sql, $conn)or die("Unable to query local database <b>". mysql_error()."</b><br>$sql"); 
        
    if ($result)
    { 
        echo 'querying...<br>'.$sql.'<br>';
    
        while($row = mssql_fetch_array($result))
        {
            echo 'sd';
            echo "<li>" . $row["id"] . $row["subgame_id"] . $row["name"] . $row["value"] . "</li>";
        }
        echo 'queried<br>';
    }
    else
    { 
        echo "database query failed."; 
    }
    mysql_close();
?>
 
When I go to execute the above code this is the output on myy web page: -
start
querying...
SELECT id, subgame_id, name, value FROM scores LIMIT 0 , 30
What am I doing wrong, I am sooooo stuck!

Re: My first PHP script

Posted: Mon Nov 30, 2009 5:42 am
by jackpf
You're using a mixture of MySQL and MSSQL functions. Look closely at line 33.

I suspect error reporting isn't turned on in php.ini.

Try changing

Code: Select all

error_reporting(E_ALL);
to

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', E_ALL);
And chage mssql_* functions to mysql_*.

Re: My first PHP script

Posted: Mon Nov 30, 2009 6:18 am
by thirstylocket
doh! I've been stuck for 2 days and you spotted it in 2 seconds!

Thank you!

Here's my output now (what I wanted it to do):-
start
querying...
SELECT id, subgame_id, name, value FROM scores LIMIT 0 , 30
sd
283111
sd
284122
queried

Re: My first PHP script

Posted: Mon Nov 30, 2009 7:56 am
by jackpf
Np ;)