A table in MySQL names users has 3 String fields including f

A table in MySQL names users has 3 String fields including firstname, lastname and username. Write a complete PHP class that would allow for constructing a users object and getting and setting values for each of the fields

Solution

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource valid, or FALSE on error.

<?php

$firstname = \'fred\';

$lastname  = \'fox\';

$query = sprintf(\"SELECT firstname, lastname, address, age FROM friends
    WHERE firstname=\'%s\' AND lastname=\'%s\'\",
    mysql_real_escape_string($firstname),
    mysql_real_escape_string($lastname));

$result = mysql_query($query);

if (!$result) {
    $message  = \'Invalid query: \' . mysql_error() . \"\ \";
    $message .= \'Whole query: \' . $query;
    die($message);
}

while ($row = mysql_fetch_assoc($result)) {
    echo $row[\'firstname\'];
    echo $row[\'lastname\'];
    echo $row[\'address\'];
    echo $row[\'age\'];
}
mysql_free_result($result);
?>

PHP will internally create a new object from scratch ,execute __construct() in the Foo class, and assign this object to the variable $object.

class Foo {

    public function __construct() {

    }

}

class Bar extends Foo {

    public function __construct(ArrayObject $arrayObj, $number = 0) {

}

}

class Baz extends Bar {

    public function __construct(Bar $bar) {

}

}

A table in MySQL names users has 3 String fields including firstname, lastname and username. Write a complete PHP class that would allow for constructing a user

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site