Php results that excluded null database entries have now stopped working.
I produced the following test code and it delivers nulls in the result. I am baffled and haven't a clue how to solve. Please help a 75 year old and my first time on an internet question site
<body>
<?PHP
include "dbcfg.php";
$link = mysqli_connect($mysqlserver, $mysqlusername, $mysqlpassword, $dbname) or
die("Error connecting to mysqli server: " . \mysqli_error());
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}
$attractquery = "select * from eatingout where town = 'Newcastle'";
$result = \mysqli_query($link, $attractquery) or die
("Query to get data from table failed: " . \mysqli_error());
while ($db_row = mysqli_fetch_array($result)) { // iterate through all selected
/*if($db_row['image'] != null){*/
/*if (!empty($db_row['image']));{*/
if(isset($db_row['image']));{
echo $db_row['image'] ." ". $db_row['EOID']."<br/>" ;
}
}
?>
</body>
Results from the above include numerous Nulls followed by EOID numbers from any of the three options. What have I done?
You can add NULL test in query:
$attractquery = "select * from eatingout where town = 'Newcastle' AND image is NOT NULL";
Might be a string conversion problem?
while ($db_row = mysqli_fetch_array($result)) {
if(isset($db_row['image']) && $db_row['image'] != NULL && strtolower($db_row['image']) != 'null' ){
echo $db_row['image'] ." ". $db_row['EOID']."<br/>" ;
}
}
Please check that your columns containt a NULL value and not a string that says "NULL". You should enable for column image to have NULL value by ticking the checkbox for NULL
<body>
<?PHP
include "dbcfg.php";
$link = mysqli_connect($mysqlserver, $mysqlusername, $mysqlpassword, $dbname) or
die("Error connecting to mysqli server: " . mysqli_error());
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}
$attractquery = "select * from eatingout where town = 'Newcastle' and ifnull(image,'')=''";
$result = mysqli_query($link, $attractquery) or die
("Query to get data from table failed: " . mysqli_error());
while ($db_row = mysqli_fetch_array($result)) { // iterate through all selected
/*if($db_row['image'] != null){*/
/*if (!empty($db_row['image']));{*/
if(!is_null($db_row['image'])){
echo $db_row['image'] ." ". $db_row['EOID']."<br/>" ;
}
}
?>
</body>
Kindly remove '\' before mysqli_query and mysqli_error.
You should use !is_null($db_row['image']) instead of isset bcoz $db_row['image'] will always be set.
Secondly you can check the null value in query it self, as mentioned in other answer.
third there is a colan after if clause ie
if(isset($db_row['image']));{
echo $db_row['image'] ." ". $db_row['EOID']."<br/>" ;
}
Related
Title pretty much explains it here is what I have tried this is my little test script to check if the mysql query works
$servername = "HIDDEN";
$username = "HIDDEN";
$password = "HIDDEN";
$dbname = "fbaFees";
/////////////////////////////////////
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected to database successfully<br><br>";
$sqlcheck = "SELECT * FROM fbafee_table WHERE ASIN = 'B07GC6DXFQ' ORDER BY ID DESC LIMIT 1 OFFSET 1;";
if ($conn->query($sqlcheck) === TRUE) {
echo "New record created successfully<br>";
} else {
echo "Error: " . $sqlcheck . "<br>" . $conn->error;
}
This script gives me this error:
Error: SELECT * FROM fbafee_table WHERE ASIN = 'B07GC6DXFQ' ORDER BY ID DESC
LIMIT 1;
This is the mysql query that works on phpmyadmin
Error: SELECT * FROM fbafee_table WHERE ASIN = 'B07GC6DXFQ' ORDER BY ID DESC
LIMIT 1;
Any help is greatly appreciated!
edit:
Try changing the code with the following:
$query = "SELECT * FROM fbafee_table WHERE ASIN = 'B07GC6DXFQ' ORDER BY ID DESC LIMIT 1 OFFSET 1;";
$result = $conn->query($query);
if ($result && $result->num_rows>0) {
while($row = $result->fetch_assoc()) {
// do stuff with your row data
echo $row['COLUMN_NAME'];
}
}
else
{
echo "Error: " . mysqli_error($conn) . "<br>" . $query;
}
The SELECT query selects data from the database, so if you check if the result is strictly equal to TRUE, the result is not. You need to cycle the results. If there's no rows returned, then you display the error using mysqli_error($conn).
Well the $conn->query method will return a result set when it finds are record not the boolean TRUE. It returns the boolean false if there is no result found. So you have to slightly change your code first to check for unavailability and then the availability as follows
if (!$conn->query($sqlcheck)) {
echo "Error: " . $sqlcheck . "<br>" . $conn->error;
} else {
echo "New record created successfully<br>";
}
So I am trying to display an array from a database that I have. When I run the script the script, I get an internal server error. Now I am not sure if this has to do with my config script or if I am not cycling through my array properly.
include 'config.php';
$conn = name2;
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Feild FROM Season 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Feild " . $row["Feild"]. " "<br>";
}
} else {
echo "0 results";
}
The syntax you have used to display the results was incorrect, replace:
echo "Feild " . $row["Feild"]. " "<br>";
With:
echo 'Feild '.$row["Feild"].'<br>';
You have not terminated your string properly.
Replace
echo "Feild " . $row["Feild"]. " "<br>";
With
echo "Feild " . $row["Feild"]. "<br>";
If you had errors turned on you would be getting an error stating a line number.
We need to know which column you're selecting from, as "SELECT Feild FROM Season 1" isn't valid. It should be something like "SELECT Feild FROM Season WHERE column = '1'"
With that in mind, this gets you closer to a solution:
include 'config.php';
$conn = name2;
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Feild FROM Season 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Feild ". $row["Feild"] ."<br>";
}
} else {
echo "0 results";
}
Use correct syntax
echo "Feild ".$row['Feild']."<br>";
Hi I can connect to my database but it's says that I have 0 rows!
I've tried different ways to connect to my database, but all of them always return 0 rows so it will not show any data from the database.
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM keys";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Key: " . $row["key"]. " " . $row["used"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
SELECT * FROM keys
^
KEYS is a reserved term, hence your query fails to execute and no data is returned, at the very least escape it.
Best option would be to change your field name to something that's not reserved.
Something funny to note
You're saying if there are some rows in my result show me those but if there are no rows in my result then show me the number of rows. In short, displaying the number of rows when your query fails is utterly pointless! That will be a good place to display an error message returned by the database driver
keys is reserved keyword in mysql it must be in backtick as
$sql = "SELECT * FROM `keys`";
Your query fails and your got 0 result always
To check error in query use
if (!$conn->query("Your_QUERY")) {
printf("Errormessage: %s\n", $conn->error);
}
Read http://php.net/manual/en/mysqli.error.php
I think you have to use this code for get result :-
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM `keys`";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Key: " . $row["key"]. " " . $row["used"]. "<br>";
}
} else {
echo "0 results";
echo "Rows:". mysqli_num_rows($result);
}
$conn->close();
I know the basics of HTML, and nothing about PHP. I've found a ton of tutorials, but I couldn't get a single one to work. I have a mySQL database working and apache running. How can I display an entire table on my page? I do not know the column names ahead of time.
Edit: Added and adjusted the code. My index page shows this: connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } //$sql = "SHOW TABLES"; $sql = "select * from cpu"; //edit your table name here $res = $mysqli->query($sql); while ($row = $res->fetch_assoc()) { print_r($row); } ?>
This is my entire index.html source:
<?php
$mysqli = new mysqli("192.168.1.2", "webuser", "*****", "OCN");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//$sql = "SHOW TABLES";
$sql = "select * from cpu"; //edit your table name here
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
print_r($row);
}
?>
Once I changed my file to .php, the error message was more clear. It reminded me that I didn't have webuser at 192.168.1.2. I created the user again and gave the correct permissions. The function appears to work, but I'll have more time in a few hours to look at it.
The real problem here is that you see connect_errno) { echo "Failed to... on your page, instead of a line beginning with Failed to... only.
Because your browser thinks that the code from <?php to $mysqli-> is an HTML tag (because of the opening and closing <>, I think your file is a .htm(l) file rather than a .php file.
Just change the file extension and if PHP is installed correctly it should work.
<?php
$mysqli = new mysqli("localhost", "root", "", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//$sql = "SHOW TABLES";
$sql = "select * from tbl_comment"; //edit your table name here
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
print_r($row);
}
?>
From what I understood. You want to display values but you don't know column names.
If that's so, try this.
$mysqli = new mysqli("192.168.1.2", "webuser", "*****", "OCN");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//$sql = "SHOW TABLES";
$sql = "select * from cpu"; //edit your table name here
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
foreach($row as $ind => $val)
{
echo "Column name: $ind, Column Value: $val<br />";
}
echo "<hr />";
}
?>
How can I disable the round up in my PHP script. If I sum up something it only displays the rounded Number.
<?php
$con=mysqli_connect("localhost","XXXX","XXXX","XXXX");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM energrid WHERE ID = 1 ");
while($row = mysqli_fetch_array($result)) {
$wert = $row['Wert'];
}
$result = mysqli_query($con,"SELECT * FROM energrid WHERE ID = 3 ");
while($row = mysqli_fetch_array($result)) {
$wert1 = $row['Wert'];
}
mysqli_close($con);
echo "here we go.:";
echo $wert + $wert1;
?>
Ok, I guess I've found my mistake. The values are like 3,5 with a , instead of a .
How can I change that if these are values directly from the MySQL DB.?
1.You can use number_format function
2.optimize your code -there is no need of while loop to do this
<?php
$db = mysql_connect("localhost","username","password"); ///connect to mysql
if (!$db) {
// throw error if not connect
die("Database connection failed miserably: " . mysql_error());
}
$db_select = mysql_select_db("databasename",$db); ///select database
if (!$db_select) {
//database connection error
die("Database selection also failed miserably: " . mysql_error());
}
$sum=0;
$result = mysqli_query($db,"SELECT sum(wert) as sum FROM energrid WHERE ID in ('1','3') ");
$row = mysqli_fetch_array($result)
$sum =$row['sum'];
`enter code here`
echo number_format($sum,'number','.','');
//where number-no. of place you want to round off
?>
use str_replace() like this to replace the comma , in number by decimal point .
$a = "3,5";
str_replace(",",".",$a);
Try using a cast like this :
echo (int)$wert + (int)$wert1;
echo (float)$wert + (float)$wert1;