1

First, I'm not asking how to display all records from a table, but a specific information. I've seen similar questions about how to display all data, but it doesn't help me when it's about choosing specific data and actually showing it on the site.

I'll explain better with an exemple :

I have a table named ressources with three columns: name, x, y.

I have this row: anonymous, 12, 14.

How do I get the x or y value by only knowing the name? I've seen this kind of SQL request:

mysqli_query($connexion,"SELECT * FROM ressources WHERE name = 'anonymous' LIMIT 1");

But my problem here is: how do I display it with echo?

Here's what I mean:

echo "You have " + $x + " cows and " + $y " horses."

while:

$x : x

$y : y

name : anonymous

3
  • Its really a simple thing, actually fundamentals of the topic, what about reading some wikis or watching some tutorials? Commented Nov 9, 2014 at 0:57
  • I've seen some, but I can't figure out this one. Really. I always get "How to display table" not "display simple data" Commented Nov 9, 2014 at 0:59
  • 3
    Sidenote: I doubt you want to do math with the + in echo "You have " + $x + " cows and " + $y " horses." - I think you come from a JS background. This is "PHP"; use a period. They're two different animals altogether ;) Commented Nov 9, 2014 at 1:00

2 Answers 2

3

Usually, when you query SELECT statements, you need to fetch them before making any echoes:

// assign it on a variable
$query = mysqli_query($connexion,"SELECT * FROM ressources WHERE name = 'anonymous' LIMIT 1");
$result = $query->fetch_assoc();
// fetch the results, since you're expecting a single row, no need to loop
// this returns an associative array

// then as usual just a normal variable assignment.    
$x = $result['x'];
$y = $result['y'];
$name = $result['anonymous'];

// then your echoes
echo "You have " . $x . " cows and " . $y . " horses.";
// (concatenation in PHP is `.`)
Sign up to request clarification or add additional context in comments.

Comments

1

Just fetch and then echo

     $result=mysqli_query($connexion,"SELECT * FROM ressources WHERE name = 'anonymous' LIMIT 1");

       // Associative array
       $row=mysqli_fetch_assoc($result);
       echo 'You have '.$row["x"].'cows and '.$row["y"].' horses';

Comments

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.