WordPress.org

Ready to get started?Download WordPress

Forums

MySQL query returns null when ran from template file (2 posts)

  1. Welton122
    Member
    Posted 1 year ago #

    Hello all,

    Okay i have a template file which im using for some customer PHP code but for some reason my mysql query keeps returning null where it shouldn't. Can anyone see why??

    Heres the code?

    // Database variables
    DEFINE ('DB_USER', 'test');
    DEFINE ('DB_PASSWORD', 'test');
    DEFINE ('DB_HOST', 'localhost');
    DEFINE ('DB_NAME', 'testdatabase');
    // Make the connection
    $dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error());
    $q = "SELECT * FROM <code>test</code>";
    $result = mysqli_query($dbc, $q);
    if(mysqli_num_rows($result)==0){
     echo"No results<br>";
    }
    echo"Start<br>";
    if($result)
    {
     while ($row = $result->fetch_assoc())
     {
     $test = $row['email'];
     echo"result: $test";
     }
    }
    echo"<br>End<br>";
    mysqli_close($dbc);

    Are you even able to make mysql querys from templates?

    Thanks

  2. bcworkz
    Member
    Posted 1 year ago #

    I can't imagine why you couldn't do this from a template, but I haven't done this, so I could be wrong.

    Do you really have a table named "test"? Seems rather unlikely, which would explain why you are not getting anything returned.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.