My first PHP script

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
thirstylocket
Forum Newbie
Posts: 2
Joined: Sun Nov 29, 2009 2:42 pm

My first PHP script

Post 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!
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: My first PHP script

Post 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_*.
thirstylocket
Forum Newbie
Posts: 2
Joined: Sun Nov 29, 2009 2:42 pm

Re: My first PHP script

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: My first PHP script

Post by jackpf »

Np ;)
Post Reply