Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Writing in PHP to execute a SQL statement and the below is failing at
PHP
$test_query = "SELECT NAME,EMAIL,PASSWORD FROM AGENTS WHERE EMAIL='b'";



";
 
echo $database;
echo "<br>";
 
echo $username;
echo "<br>";
 
echo $password;
echo "<br>";
 
$link = mysqli_connect($servername, $username, $password) or die("Unable to connect to '$servername'");
 
echo "<br>vvbvbvbvb<br>";
 
if ($link -> connect_errno) {
  echo "Failed to connect to MySQL: " . $link -> connect_error;
  exit();
} else {
echo "Connected successfully";
}
 
$test_query = "SELECT NAME,EMAIL,PASSWORD FROM AGENTS WHERE EMAIL='b'";
 
echo "<br>";
 
echo $test_query;
 
echo "<br>";
echo "testdbye";
 
echo "<br>";
 
$result = mysqli_query($link, $test_query);
 
echo "goodbye";
 
echo "kasldkasldkasdlkasdlk";
echo "<br>";
 
 
mysqli_close($link);
 
 
echo "goodbye";
echo "<br>";
 
 
?>


What I have tried:

Removed User as it is a reserved word. Can't understand why it is failing. Any help would be appreciated.
Thank you!
Posted
Updated 25-Nov-23 13:21pm
v2
Comments
Dave Kreskowiak 25-Nov-23 15:51pm    
...and the EXACT error message is??? (really important in diagnosing the problem!)
Richard MacCutchan 26-Nov-23 3:18am    
What do you mean by "failing"?
Member 15627495 27-Nov-23 1:53am    
'db name' is required.

for 'mysqli_connect(1,2,3,4)' or die("") ; // 4 parameters needed.
do as you are in the 'production final website'.

----------------------------------------------
one thing about 'echo' function :

the 'echo' function is using 'intermediate buffer', before the page is sent to the client.
it's an 'output buffer' filled every time you use echo.
to have less call to the 'echo' function, you have to use a 'temp' var :
$temp_output = "<br>" ; // init of the var
$temp_output .= "message of your choice" ; // concatenation here, you add 'words' to the var.
$temp_output .= "<html tag> with text, or script" ;

// and , at end of your script, or before 'exit case', or end of scope :
echo $temp_output ;

1 solution

a Little late but the following to point you in the right direction -

Your code is very "loose" and mixed, there are many easier ways to echo out values to see if a correct item(character, array, variable etc) is returned from a line of code, the best is by using error trapping by using try-catch blocks as you did in your code -
PHP
if ($link -> connect_errno) {
  echo "Failed to connect to MySQL: " . $link -> connect_error;
  exit();
} else {
echo "Connected successfully";
}


If you do this with all of your variables, it will make life so much easier, as an example -
PHP
/ Example variables
$var1 = 42;
$var2 = "Hello, World!";
$var3 = true;

//Check each variable individually...
if (isset($var1) && isset($var2) && isset($var3)) {
    echo "All variables exist.";
} else {
    echo "At least one variable does not exist.";
}

//Use isset() with multiple arguments...
if (isset($var1, $var2, $var3)) {
    echo "All variables exist.";
} else {
    echo "At least one variable does not exist.";
}


The next will be to use PHP's error checking on each of your PHP pages, this will show you errors that you have not even realized exist in your code. This must be placed at the top of each page, keep in mind to always disable this when not developing -
PHP
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


Now to your error, using the above -
First check that each variable 'NAME', 'EMAIL', PASSWORD' actually has a value, else show an error message. If they do have values, the crash probably occurs at the 'b' value, PHP will never read a variable called 'b' as it need to be identified by the '$' operator. Fix this and you should be good to go - What are the naming conventions of variables in PHP?[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900