Select query in PHP7

<?php

declare(strict_types=1);

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

$dbServername='localhost';
$dbUsername='root';
$dbPassword='correct';
$dbName='mysql';

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

$query=select authentication_string from user where user='root';
$sql=mysql_query($query);
$row=mysql_fetch_assoc($sql);
$authentication_string=$row[authentication_string];
echo authentication_string;

?>

The browsing result is nothing.although I expect the below.
Is there someting wrong in the code above?

Take a close look at this line:

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

I fix it like the below. but it still shows blank.

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

Ah, this one is incorrect as well:

$authentication_string=$row[authentication_string];

I fix it like the below but it also still shows nothing.

$authentication_string=$row['authentication_string'];

Your problem is that you are using dangerous obsolete code that has been completely removed from Php. You need to use PDO with Prepared Statements.

That code will never ever work in Php 7.

1 Like

Try replacing the following functions:

OLD

mysql_connect (…)
mysql_query (…)

NEW - with an i

mysqli_connect (…)
mysqli_query (…)

You cannot just add an i and expect it to work. Syntax is not the same.

1 Like

The below is all code in a page.
Sadly, the browsing result of it still produces nothing.

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

?>

If database connecting is possible without PDO,
I like to make it to succeed in database connecting without PDB first.

if I succeed in db connecting without PDO, I may go to PDO
because benanamen and some other members recommend it strongly., but after succeeding it without PDO.

By the way, the word “PDO” seems an abbreviation.
What is the full word of PDO?

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,