Jump to content

[SOLVED] Can't get PHP to print a result of an sql query.


heldenbrau

Recommended Posts

What is wrong with this, I just want it to print what's in the rating field, but nothing happens.

<?php

   $mysqli = new mysqli("localhost", "user", "password", "user");
    if ($mysqli === false) {
      die("ERROR: Could not connect to database. " . mysqli_connect_error());
    }

$sql = "SELECT rating from users where username = \"Chris\"";

if ($result = $mysqli->query($sql)){
echo "$result";
} else {
echo "no results";
$result->close();
$mysqli->close();
?>

SQL requires single quotes for text fields (i.e. around 'Chris'), double quotes wont work from my understanding.

 

your bigger problem though is you do not close the bracket for your else statement which is probably causing the page to not show.

 

You need to turn on error reporting to see the errors for stuff like this

Thanks that has got the page showing, but now it says no results.  I have changed the script to

 

<?php

   $mysqli = new mysqli("localhost", "user", "password", "user");
    if ($mysqli === false) {
      die("ERROR: Could not connect to database. " . mysqli_connect_error());
    }

$sql = "SELECT rating from users where username = \'Chris\'";

if ($result = $mysqli->query($sql)){
echo "$result";
} else {
echo "no results";
}

$result->close();
$mysqli->close();
?>

take out the slashes

<?php
$sql = "SELECT rating from users where username = 'Chris'";

 

I am not entirely sure if this will work with your class structure, but try it

 

<?php
$result = $mysqli->query($sql) or die(mysql_error());

if it doesn't, then you should just take exactly what you have for your $sql statement and throw it into your sql query engine and try to run it manually to see what the errors are

It works fine when I type it directly into mysql.  I have taken out the slashes and it goes back to nothing happening. 

 

<?php

   $mysqli = new mysqli("localhost", "user", "password", "user");
    if ($mysqli === false) {
      die("ERROR: Could not connect to database. " . mysqli_connect_error());
    }

$sql = "SELECT rating from users where username = 'Chris'";

if ($result = $mysqli->query($sql)){
echo "$result";
} else {
echo "no results";
}

$result->close();
$mysqli->close();
?>

 

 

<?php

   $mysqli = new mysqli("localhost", "user", "password", "user");
    if ($mysqli === false) {
      die("ERROR: Could not connect to database. " . mysqli_connect_error());
    }

$sql = "SELECT rating from users where username = 'Chris'";

if ($result = $mysqli->query($sql)){
echo "$result";
} else {
echo "no results, but here is the error: ",$mysqli->error;
}

$result->close();
$mysqli->close();
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.