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();
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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();
?>

 

 

Link to comment
Share on other sites

<?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();
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.