PHP help?

Status
Not open for further replies.
I have about two days' worth of PHP experience (more like one), and this is stumping me. I want to create a table with output from a MySqueal query. I also create a header with column names.

The header is created, the data rows are not.

Also, you might wonder why I didn't use require_once. I tried. This page is supposed to be included in another page of a larger program (which I largely don't get yet), and when I tried to use require_once, I got no output and a rather strange error. It didn't happen when I did it this way.

Code:
Code:
<?php

if (!function_exists("db_conn"))
{
    require '../include/functions.php';
}

$db = db_conn('project'); //creates a DB connection.
$link = mysql_query("select 1, 2, 3;", $db);
if ($link)
{
    echo "<table border='1'><tr style='background-color:FFFFCC'>";
    for ($i = 0; $i < mysql_num_fields($link); $i++)
    {
        echo sprintf("<th>%s</th>", mysql_field_name($link, $i));
    }
    echo "</tr>";
    while (true)
    {
        $row = mysql_fetch_row($res);
        if ($row)
        {
            echo "<tr>";
            for ($i = 0; $i < count($row); $i++)
            {
                echo sprintf("<td>%s</td>", $row[$i]);
            }
            echo "</tr>";
        }
        else
        {
            break;
        }
    }
    echo "</table>";
    mysql_close($link);
    mysql_close($db);
}
else
{
    echo "Your SQL blows!";
}
?>

Output:
Code:
<table border='1'><tr style='background-color:FFFFCC'><th>1</th><th>2</th><th>3</th></tr></table>

Also, dollar signs should be followed by NUMBERS. Big ones.
 

hanser

Ars Legatus Legionis
43,847
Subscriptor++
npHardProblem":2f67pbh2 said:
So you are new to interpreted languages is what you are saying? :D I guess most of them will at least blow up on you and complain about nil or undefined or whatever. It's quite possible PHP is actually throwing an error and you're just not seeing it, depending on what kind of environment you're running in.
Probably.

Tiiba, create a page that just has:

Code:
<?php
phpinfo();

and nothing else in it. Look for where your error log is. You can take a look there for a log of errors that you've created over the course of writing your script.

You can also turn on error reporting such that it's echo'd to the screen, if you don't feel like refreshing a log file. (display_errors)

Obligatory "Use PDO for PHP database access" comment.
 
I'm about two days into PHP also. I'm hating it, perhaps I should be using Ruby. But anyway I found this log class and it's helping a lot especially dumping $_POST when sent by things that don't echo();

Code:
<?php
class Logging{
    // define default log file
    private $log_file = '/tmp/phpdev.log';
    // define file pointer
    private $fp = null;
    // set log file (path and name)
    public function lfile($path) {
        $this->log_file = $path;
    }
    // write message to the log file
    public function lwrite($message) {
        // if file pointer doesn't exist, then open log file
        if (!$this->fp) {
            $this->lopen();
        }
        // define script name
        $script_name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
        // define current time
        $time = date('H:i:s');
        // write current time, script name and message to the log file
        // in case of using on Windows, instead of "\n" use "\r\n"
        fwrite($this->fp, "$time ($script_name) $message\n");
    }
    // close log file (it's always a good idea to close a file when you're done with it)
    public function lclose() {
        fclose($this->fp);
    }
    // open log file
    private function lopen() {
        // define log file path and name
        $lfile = $this->log_file;
        // define the current date (it will be appended to the log file name)
        $today = date('Y-m-d');
        // open log file for writing only; place the file pointer at the end of the file
        // if the file does not exist, attempt to create it
        $this->fp = fopen($lfile . '_' . $today, 'a') or exit("Can't open $lfile!");
    }
}

$log = new Logging();

$log->lwrite($_POST['NewName']);

$log->lclose();
?>
 
topham":3vg4guy9 said:
sryan2k1":3vg4guy9 said:


Nitpick, but while this is how some people say it, that isn't the name. It's MySQL.

He obviously knows how to spell it as it was correct in the code. If you check his prior posts working in other programming languages you'll find a similar poor attitude.

Poor attitude? Oh, that's rich.
 
Status
Not open for further replies.