Single select sql

I have a page with the code below

declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1'); 

$dbConnect=mysqli_connect('localhost', 'root', '*******', 'test');

$sql="select * from test";
$sqlt=mysqli_query($dbConnect, $sql);
$sqlCount=mysqli_num_rows($sqlt);
if ($sqlCount ] 0){
while ($row =  mysqli_fetch_assoc($sqlt))  {
echo $row['id'] . ' ' . $row['name'].'[br]'; }
}

The code above works fine.
And the result of the code above is the following.

Now I like to call Jack with id=2.
In order to call Jack with id=2, I made the code below.

declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1'); 

$dbConnect=mysqli_connect('localhost', 'root', '*******', 'test');

$sql="select name from test where id=2";
$sqlt=mysqli_query($dbConnect, $sql);
$row =  mysqli_fetch_assoc($sqlt)
echo $row['name'];

The following is my target result.
But the code above seems not to work as I expected.

(main question) How I can get my target result above with the single sql “select name from test where id=2”?

  • single sql means not while sql.
    (sub question) How do you call the not-while SQL instead of single SQL?

are you absolutely sure? I’m just not sure what kind of logical operator a closing square bracket is meant to be. :wink:

What did it do instead? We cant diagnose without knowing the symptoms.

Doesn’t that mean that your id field actually contains (1) and (2), as you don’t add those brackets in your display code but still get them in the display?

1 Like

I don’t know what kind of HTML tag they are.

Maybe it’s a non-qwerty keyboard thing? special syntax? If it’s not comprehending the different uses for square brackets vs. angle brackets, please review until it becomes second nature.

Hey joon1, what is the data type of id in the database? It looks like your code should work, unless there’s no numeric id found that is equal to 2…

FWIW it looks like you are re-typing your code, because some of it doesn’t look like it would run (as others have mentioned). It’s better to copy/paste to avoid typos.

The code below has typos in it.

if ($sqlCount ] 0){
while ($row =  mysqli_fetch_assoc($sqlt))  {
echo $row['id'] . ' ' . $row['name'].'[br]'; }
}

I think this is the code that should produce your first result?

if ($sqlCount > 0) {
  while ($row =  mysqli_fetch_assoc($sqlt))  {
    echo '('.$row['id'] . ') ' . $row['name'].'<br>';
  }
}
<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1'); 
$dbConnect=mysqli_connect('localhost', 'root', '********', 'test');
$sql="select name from test where id=2";
$sqlt=mysqli_query($dbConnect, $sql);
$row =  mysqli_fetch_assoc($sqlt)
echo $row['name'];

The code above works fine. It produces “Jack”.
I am sorry I was careless.
Thank you.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.