-3

I make a random quote app, in which by pressing a button, i can load one random phrase. I created for this a MySQL database, and two php code. I upload my two code in a web hosting, and the app is running!
But sometime it gives me "null" instead of the phrase. I don't know why. I'm not very good with this. What is probably the problem?
This is my index.php

    require_once 'db.php';

    $query = "SELECT * FROM quotes ORDER BY rand() LIMIT 1";

    $result = mysqli_query($con, $query);

    while($row = mysqli_fetch_array($result)) {
            print(json_encode($row['quote']));
    }

And this is my db.php

    // Create connection
    $con=mysqli_connect("host","user","pass","a6361246_phrases");

    // Check connection
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
Akiko93
  • 21
  • 3

1 Answers1

-1

You can use the first three lines in your db.php file like this...

<?php
$host = 'localhost'; $db = 'database-name'; $user = 'database-user'; $pw = 'database-password';
$conn = new PDO('mysql:host='.$host.';dbname='.$db.';charset=utf8', $user, $pw);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>

Don't forget to change database-name, database-user & database-password to your specific credentials.

Then using PDO get your phrase like this...

<?php
require_once 'db.php';

try {
    $sql = "SELECT * FROM Quotes ORDER BY rand() LIMIT 1";
    $query = $conn->prepare($sql);
    $query->execute();
    $row = $query->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    die("Could not get the data: " . $e->getMessage());
}
?>
Kuya
  • 7,280
  • 4
  • 19
  • 31
  • sorry for another question, but i don't know very much about this :( so my questions are stupid! Is there my index? what i mean, i have two code, but with this...? – Akiko93 Sep 03 '15 at 09:15
  • and another question, in 'mysql:host' i have to insert my credentials or not? sorry for my stupidity – Akiko93 Sep 03 '15 at 09:16
  • Yes you have to enter your database connection credentials in place of "database-name", "database-user" & "database-password" – Kuya Sep 03 '15 at 09:29
  • Please see my updated answer. It may explain a little better. – Kuya Sep 03 '15 at 09:37
  • "Fatal error: Call to a member function prepare() on a non-object in /.../index.php on line 4" – Akiko93 Sep 03 '15 at 09:47
  • Did you update your db.php file and put your database credentials in the file? – Kuya Sep 03 '15 at 09:52
  • If you are going to downvote an answer, be man/woman enough to leave a comment as to why. – Kuya Sep 03 '15 at 13:42
  • 1
    We're all supposed to be here on SO to learn from each other. No one learns anything from an uncommented downvote. – Kuya Sep 03 '15 at 13:51
  • @Akiko93 - Do you need more help? If this answer was helpful to you and answered your question, please don't forget to [accept that answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). Also see [What does it mean when an answer is "accepted"?](http://meta.stackexchange.com/help/accepted-answer) and [Why is voting important?](http://meta.stackexchange.com/help/why-vote). – Kuya Sep 24 '15 at 06:59