0

There are a couple questions I have regarding the following code:

    // Create a new MySQL connection, inserting a host, username, passord, and database you connecting to
    $conn = new mysqli('xxx', 'xxx', 'xxx',);
    if(!$conn) {
        die('Connection Failed: ' . $conn->error());
    }

    // Create and execute a MySQL query
    $sql = "SELECT artist_name FROM artists"; 
    $result = $conn->query($sql);

    // Loop through the returned data and output it
    while($row = $result->fetch_assoc()) {
        printf(
            "Artist: %s<br />", $row['artist_name'], "<br />",
            "Profile: %s<br />", $row['artist_dob'], "<br />",
            "Date of Birth: %s<br />", $row['artist_profile'], "<br />",
            "Password: %s<br />", $row['artist_password'], "<br />"
        );
    }

    // Free memory associated with the query
    $result->close();

    // Close connection
    $conn->close();

How can I assign artist_dob, artist_profile, and artist_password and return it to the browser?

What does the statement $conn->error() mean? I don't understand what the -> sign does.

2

3 Answers 3

1

The printf() function will return the information to the screen. The $conn->error() will echo out a database error if there was one trying to connect. the -> means it's calling a function from an object so $conn is an object and it has lots of functions and $conn->query() means to run the query function on the $conn object.

You may want to look into beginner php object-oriented tutorials: https://www.google.com/search?q=beginner+guide+to+php+oop

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Chris. I have checked out the tutorial and I think it's good. Let me check it out more...
1

This code is all wrong. What it actually have to be:

// Create a new PDO connection to MySQL
$dsn = "mysql:host=localhost;dbname=test;charset=utf8";
$opt = array(
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$conn = new PDO($dsn,'root','', $opt);

// Create and execute a MySQL query
$sql = "SELECT artist_name FROM artists"; 
$stm = $conn->prepare($sql);
$stm->execute();
$data = $stm->fetchAll();

// Loop through the returned data and output it
?>
<? foreach($data as $row): ?>
        Artist: <?=$row['artist_name']?><br />
        Profile: <?=$row['artist_dob']?><br />
        Date of Birth: <?=$row['artist_profile']?><br />
        Password: <?=$row['artist_password']?><br />
<? endforeach ?>

And it is in many (at least a dozen) ways better.

1 Comment

I think you used a different method of connecting to the database, the one I haven't gotten to learn about yet. I was using the MySQLi Method... I know less about PDO, but maybe I should look into it...
0

The -> in PHP is used to call methods and public parametres from objects like the . in many other languages like C# or Java.

PHP: $object->method();

Java: object.method();

Also, your concatenate character is wrong. If you use the comma , you are passing multiple parametres to the function. To concatenate strings in PHP you have to use the point . like in other languages you would use the +.

PHP: "Some "."string" = "Some string"

Java: "Some "+"string" = "Some string"

Of course, you can concatenate strings in the function calling as an argument.

$string = "Print".$string;
printf($string);

is the same than

printf("Print ".$string);

1 Comment

Incorrect, printf allows as many parameters as needed, check the manual

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.