<?php
$connect = mysql_connect("asdkjhaksjd", "username", "password");
if(!$connect) {
echo("Could Connect to MySQL.");
}
$select_db = mysql_select_db("database");
if(!$select_db) {
echo("Could not select the database.");
}
$test_query = "SELECT * FROM bus_timings WHERE slno = 1 ";
$q_result = mysql_query($test_query);
$count = mysql_num_rows($q_result);
if($count > 0){
while($row = mysql_fetch_assoc($q_result)) {
$slno = $row['slno'];
}
}
echo("<div style='margin-left:500px; z-index:950;'><center> Serial No = ". $slno . "<br /> </div></center>" );
?>
As expectations I’ve to get the error in the first line of my code i.e. while connecting to the server, because I’ve typed the server name incorrect and I’m getting the error but it is not showing up on the screen!
The Problem
When I comment the HTML/CSS of my form and execute the file I’m able to see the error. How I can solve this.
What I want?
Even though I get the error I want them to showed on the screen i.e. when the HTML/CSS aren’t commented.
On some servers you’ll get major errors as the mysql_* extension that you’ve used was REMOVED from PHP as of version 7.
On a development server you’ll want to turn on the display of errors and have all errors/notices reported. For a production/live server you’ll want to have the display of errors turned off and instead have all errors logged
I’m using PHP 5.
I copied the above code and executed the file and I’m getting this error Warning: Unsupported declare 'strict_types' in /Applications/XAMPP/xamppfiles/htdocs/Raavata R&D Retrieve all timings/search.php on line 104
Is it because I’m using PHP 5, shall I use PHP 7?
Now, I’m able to see the errors in the screen even when I’ve not commented by HTML but it is not the error of server connectivity. Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /Applications/XAMPP/xamppfiles/htdocs/Raavata R&D Retrieve all timings/search.php on line 108
<?php
define('HOST', 'asdkjhaksjd'); // Define the host
define('USERNAME', 'username'); // Define the username
define('PASSWORD', 'password'); // Define the password
define('DATABASE', 'database'); // Define the database
// Create a new mysqli connection
// It should always start like so
// {host}, {username}, {password}, {database}
$mysqli = new mysqli(HOST, USERNAME, PASSWORD, DATABASE);
$get_slno = 1; // Create the variable $slno and append the numeric 1 to it.
$sql = "SELECT slno FROM bus_timings WHERE slno = ?"; // Your SQL statement
$prepare = $mysqli->prepare($sql); // Prepare your SQL statement
$prepare->bind_param('i', $get_slno); // Bind your SQL statement with your variable you have for the WHERE clause
$prepare->execute(); // Execute the prepared statement
$prepare->store_result(); // Store the result for later checking
// Data types are as is
// s = string
// i = integer
// d = double (which is like an integer, but with a decimal)
// blob
// STOP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// IF YOU ARE THINKING OF COMPARING num_rows TO 0 OR 1 THEN YOU SHOULD STOP RIGHT THERE!!!!
// FOR PROPER CHECKING, IT DOES NOT NEED
// num_rows > 0 | num_rows > 1 | num_rows < 0 | num_rows < 1 | num_rows <= 0 | num_rows <= 1 | num_rows >= 0 | num_rows >= 1
// num_rows already returns a 0 or a 1. 0 meaning false and 1 meaning true.
// It's redundant to try and compare the values when you can have it in simplified form.
// If num_rows returns 1 (true), then it will by default always and forever go into the if statement.
// If num_rows returns 0 (false), then it will by default always and forever go into the else statement.
// DO YOU SEE WHERE IT CAN BE REDUDANT TO COMPARE IT WHEN IT AUTOMATICALLY DOES IT FOR YOU?
// Check whether num_rows returned true or false
if($prepare->num_rows) {
// If true, it will go here.
$prepare->bind_result($slno); // Bind the result from your SQL statement and append a variable to your results
// Create a while loop
while($prepare->fetch()) {
// Put your HTML stuff here.
// There are a few ways to add in HTML in PHP
// - You can use HEREDOCs.
// - You can use double quotes and escape other double quotes within PHP.
// - You can use single quotes and escape other single quotes within PHP.
// - And you can actually end the PHP tag, create your HTML elements. And then start another PHP tag.
?>
<div style='margin-left:500px; z-index:950;'><center> Serial No = <?php print($slno); ?><br /> </div></center>
<?php
}
} else {
// If false, it will go here.
// This part can be triggered by a lot of things.
// If the table is empty and has no rows, you will come to this conclusion.
// If the specified number does not exist, you will come to this conclusion.
// Print a custom error for your users to see.
// It is important to display proper errors like this so that the user does not get left in the dark.
// If you don't check for proper errors like this, your users will be displayed a blank page and that's not what you want.
// Anyone who does not support this only supports legacy logic/ code.
print('The data you requested for does not exist. Please try again with a different one.');
}
You should be using either 5.6 or 7 as these are the only currently supported versions.
You should not still be using mysql but use mysqli or pdo.
If you do move to php7, mysql won’t even be an option.
There is no sense in writing new scripts using old methods that won’t work in current versions.