Select query in PHP7

I copied and pasted your script and tried it on my computer and added my debug function named fred(…) because it is a lot easier to type than debug(…);

There was a strange syntax error:
Parse error : syntax error, unexpected ‘_query’ (T_STRING) in /var/www/aatests/joon1-001.php on line 17

So I remmed part of the script:

<?php
declare(strict_types=1);

error_reporting(-1);
ini_set('display_errors', '1');

$dbSevername  = 'localhost';
$dbUsername   = 'root';
$dbPassword   = 'la1asql';
$dbName       = 'mysql';

$dbConnect = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

$query  = "select authentication_string from user where user='root'";

/*
  $sql    =  mysqli _query($query);
  $row    =  mysqli_fetch_assoc($sql);

  $authentication_string = $row['authentication_string'];

  echo $authentication_string;
*/

//========================================================================================
function fred($val='', $title='')
{
  $style = 'width:88%; margin:2em auto; background-color: snow; color: #000; padding: 0.42em';
  echo "<pre style='$style' >";
    echo $title . ' ==> ';
      print_r($val);
  echo '</pre>';
}//

…and got this error:
Notice : Undefined variable: dbServername in /var/www/aatests/joon1-001.php on line 12

True but it is a debugging step in the right direction by using the correct “mysqli_” function names.

The problem is the space here…thus your “strange” error (Aside from it missing the connection parameter)

$sql=mysqli _query($query);

$dbSevername != $dbServername

These are really careless mistakes.

1 Like

Yes, those are really careless mistakes.
When i make this kind of mistakes, I usually find it and fix it by myself,
But this time
Because I have no confidence of correct db connecting, I failed in finding mistalke.
Anyway I am so sorry about the misakes.
And by the way, I fixed two mistakes in the code,
But the result still shows nothing.

.

Please supply your amendments and I will give it a try locally.

The following is the copy from nano editor.
It is all code in the page.
The password “******”’ is changing from the copy.

<?php

declare(strict_types=1);

error_reporting(-1);
ini_set('display_errors', '1');

$dVer='localhost';
$dUser='root';
$dassword='******';
$dbName='mysql';

$dbConnect=mysqli_connect($dVer, $dUser, $dassword, $dbName);

$query="select authentication_string from user where user='root'";
$sql=mysqli_query($query);
$row=mysqli_fetch_assoc($sql);
$authentication_string=$row['authentication_string'];

echo $authentication_string;

?>

Here is a modifed version using a “News” database and table:

Try changing your login parameters and see if it works OK

Source:

<?php
declare(strict_types=1);

error_reporting(-1);
ini_set('display_errors', '1');

if(0):
  $dbSevername  = 'localhost'; 
  $dbUsername   = 'root';
  $dbPassword   = 'la1asql';
  $dbName       = 'mysql';

  $dVer         = 'localhost';
  $dUser        = 'root';
  $dassword     = '******';
  $dbName       = 'mysql';

else:
  $dbServername = 'localhost';
  $dVer         = 'localhost';
  $dUser        = 'root';
  $dassword     = 'root';
  $dbName       = 'news';
  $dbName       = 'news';
endif;



$dbConnect=mysqli_connect($dVer, $dUser, $dassword, $dbName);
# fred($dbConnect);

$query="select authentication_string from user where user='root'";
$query="select `*` from `$dbName` where 1";

if(0) :
  echo '<p> 
    Fatal error: Uncaught ArgumentCountError: mysqli_query() expects at least 2 parameters, 1 given in /var/www/aatests/joon-002.php:31 Stack trace: #0 /var/www/aatests/joon-002.php(31): mysqli_query("select authenti...") #1 {main} thrown in /var/www/aatests/joon-002.php on line 31
  </p>';
endif;

$sql = mysqli_query($dbConnect, $query);
fred($sql, '$sql');
fred($MAX = $sql->num_rows, $MAX);

$sql->data_seek(0);
while ($row = $sql->fetch_assoc()) :
  # echo " id = " . $row['id'] . "\n";
  fred($row, '$row');
endwhile;

if(0) :
  echo '<p> Parse error: syntax error, unexpected "fetch_assoc" (T_STRING) in /var/www/aatests/joon-002.php on line 32 </p>';
endif;

$row = mysqli_fetch_assoc($sql);
fred($row, '$row');
# $authentication_string=$row['authentication_string'];

# echo $authentication_string;



//========================================================================================
function fred($val='', $title=NULL)
{
  $style = 'width:88%; margin:2em auto; 
            background-color: snow; color: #000; 
            padding: 0.42em; border: solid 1px #888;';
  if($title):
    $title . ' ==> <br>';
  else:  
    $title = '';
  endif;  

  echo "<pre style='$style'> <b>Debug ==> ";
    echo $title  .'<br>';
      print_r($val);
  echo '</pre>';
}//```

// ?> NOT RECOMMENDED - CAN CAUSE ERRORS

Output:

Debug ==> $sql
mysqli_result Object ( 
[current_field] => 0 
[field_count]   => 4 
[lengths]       => [num_rows] => 3 
[type]          => 0 )</b>

Debug ==> 3
3

Debug ==> $row
Array ( [id] => 1 
[title] => Elvis sighted 
[slug] => elvis-sighted 
[body] => Elvis was sighted at the Podunk internet cafe. It looked like he was writing a CodeIgniter app. )

Debug ==> $row
Array ( [id] => 2 
[title] => Say it isn't so! 
[slug] => say-it-isnt-so 
[body] => Scientists conclude that some programmers have a sense of humor. )

Debug ==> $row
Array ( [id] => 3 
[title] => Caffeination, Yes! 
[slug] => caffeination-yes 
[body] => World's largest coffee shop open onsite nested coffee shop for staff only. )

Off for a very long late lunch :slight_smile:

PHP Data Objects, I think, something like that.

That’s fine, it is interesting to see why your code doesn’t seem to work even once the typos are fixed. But at some point, it might be a good thing to drop the thing you can’t get working, and who knows, maybe PDO will just work straight off for you.

If your code is showing nothing at all, not even any error messages, even if you right-click and “view source”, how are you executing it? Are you opening it with a “http://localhost/whatever” in your browser, or browsing to it with “file://c:\whatever”? The former is the correct way and should / will execute the PHP code on the server - the latter is just opening the file and relies on the browser to run the code, which it will not.

The below is the result of “view-source:http://localhost/

1

1 is the 1st line number. It means that view source also produces nothing.

look at the developer console (F12 in the browser) and there in the “network” tab. If you et a 500 error, you have to look in the webservers error.log to see the message.

I think you are trying to get the whole script to work and should be testing very small chunks starting at the top because PHP starts at the first line then proceeds to process the script one line at a time.

The modified script I supplied in a previous post was based on your revised script. Along with the script is the output from a very simple table. Did you try running the script? If it does not run and produce a blank screen then start remming near the top until an error is showing, once the error is cleared then move the rem further down,

If you just add a quick html tag before your PHP open tag, does that get displayed? Did you really call your database “mysql”?

As well as remming out code (commenting it out, if you’re not familiar with the term) you can add echo statements through it, another way to see how far it’s getting. For example

<?php

declare(strict_types=1);

error_reporting(-1);
ini_set('display_errors', '1');
echo "here 1";

$dVer='localhost';
$dUser='root';
$dassword='******';
$dbName='mysql';

echo "here 2";

$dbConnect=mysqli_connect($dVer, $dUser, $dassword, $dbName);

echo "here 3";

$query="select authentication_string from user where user='root'";

echo "here 4";

$sql=mysqli_query($query);

echo "here 5";

$row=mysqli_fetch_assoc($sql);

The code is above.
The result is below.

The following is from the shell.

As already said, you can’t just replace mysql with mysqli, you have to refactor this, and the error message is pretty clear, so here is the manual:

https://www.php.net/manual/en/mysqli.query.php

<?php

declare(strict_types=1);

error_reporting(-1);
ini_set('display_errors', '1');

$dVer='localhost';
$dUser='root';
$dassword='la1asql';
$dbName='mysql';

$dbConnect=mysqli_connect($dVer, $dUser, $dassword, $dbName);

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

The above is the copy from nano editor.
The below is its result.

How can I defined variable mysqli?

How can I add 1 more parameter?

Read the documentation that chorn linked to, and have a look at the code that John_Betong wrote for you.

You can either change your connection to be called $mysqli, or you can change the second bit of code to use your connection name.

It seems that you’re randomly copying and pasting code here without sitting down and reading up on what each bit does. Slow down a bit and read through the relevant doc and sample code that you have been given.

You should also be consistent between using procedural calls (like your database connection call) or object-oriented (like your check for errors). Use one or the other, don’t keep swapping around between the two. I believe it will work, but it’s not really the right way to do it.

2 Likes

<?php

declare(strict_types=1);

error_reporting(-1);
ini_set('display_errors', '1');

$dbServer='localhost';
$dbUser='root';
$dbPassword='******';
$dbName='*user';

$dbConn=mysqli_connect($dbServer, $dbUser, $dbPassword, $dbName);

$sql="select * from user;";
$result=mysqli_query($dbConn, $sql);
$resultCheck=mysqli_num_rows($result);

if ($resultCheck > 0){
   while ($row =  mysqli_fetch_assoc($result))  {
      echo $row['authentication_string'];
   }
}

?>

The code above gets the result what I want.

Thank you to all.

1 Like

Congratulations :slight_smile:

I noticed you are still using the closing ?> which I mentioned in the script I supplied that it is best not to use because it can cause problems. I forget the exact reason, something to do with white-space.

It was long way.
And I have long way to go.

Thank you.
I’ll keep that in my mind.

1 Like

The reason for not having a ?> in every file is because if there is blank line after it you’ve started with the HTML output and can’t send headers anymore using the header() function.

2 Likes