Getting just one INT from the database with PHP - php

How to fetch an integer from a mysql database using PHP and phpmyadmin.
This is my code right now:
session_start();
require_once("connect.php");
$query = "SELECT klantID FROM klant ORDER BY klantID DESC LIMIT 1;";
$result = mysqli_query($con, $query);
//echo $query."<br>";
while( $row=mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
echo $row['klantID'];
mysqli_close($conn);
}
I'm trying to get the highest 'klantID' in the table 'klant'. When I run the PHP file, it shows nothing. It's just blank.
How do I debug this to find out what is wrong?

The PHP script returns nothing because the SQL successfully returns no rows. You skip over the while loop and the program exits.
One way to help find out what is wrong is to read the result from mysqli_num_rows like this:
require_once("connect.php");
$sql = "SELECT `klantID` FROM `klant` ORDER BY `klantID` DESC LIMIT 1; ";
$query = mysqli_query($con, $sql);
if ( mysqli_num_rows($query) > 0 )
{
//we got a result
$result = mysqli_fetch_object($query);
echo "ID Found : ".$result->klantID."<br />";
} else {
echo "Nothing found!"
}
After that fetch, the result of mysqli_num_rows($query) > 0 evaluates to false and the program prints "Nothing found!"

Related

mysqli_num_rows returns a value of 0 entries

I'm trying to connect to my database to show a row of data from mySQL. I believe I have my data connection set up correctly, as I am able to post data into my database. So I don't believe the error lies in my connection variable $conn. So I am a bit confused in what I could do to try to debug this situation. Any help would be amazing! Thank you!!
Below is my code to retrieve my data from my database:
$sql = "SELECT * FROM `scream` ORDER BY `id` DESC LIMIT 0,1;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row['volt'];
}
}
else{
echo "no data";
}

php mysql affected_rows showing 2

I am having a bit of trouble. This was working until I added a second database class to run some test methods. After removing this I am now getting this error and can't understand why.
Warning: extract() expects parameter 1 to be array, null given in /home/bitandpi/public_html/temp/build/build.php on line 49
Here is my code:
$urltag = urldecode($contentPageVar);
$sql = "SELECT * FROM shopproducts
WHERE urltag = '$urltag' AND urltag != ''
AND pd_active > 0 AND pd_visible > 0";
$result = $database->fetch_array($sql);
echo $database->affected_row()."<BR>";
print_r($result);
exit;
if($database->affected_row() > 0) {
// run code
}
I have printed the $sql var and ran it straight into phpmyadmin query and it returns 0 results.
However if I run the above code it prints the following on my screen:
2
Array ( )
Why is it telling me it is affecting rows when it is not?
Thanks
You have use num_rows.
$urltag = urldecode($contentPageVar);
$sql = "SELECT * FROM shopproducts WHERE urltag = '$urltag' AND urltag != '' AND pd_active > 0 AND pd_visible > 0";
$result = $database->query($sql);
echo $row_cnt = $result->num_rows;
echo "<br/>";
if($row_cnt>0){
// run code
$result1 = $database->fetch_array($sql);
}

PHP If variable equals

I am trying to show some text depending upon what a status is set to in a db table.
See my code below:
$result=mysql_query("SELECT * FROM hr_recruitment_stages where vacancy_ref='$vacancyref' order by added_on DESC limit 0,1")or die('ERROR 315' );
$row = mysql_fetch_array($result);
$stage_name = $row ['stage_name'];
if($stage_name['stage_name'] == 'Shortlisting') { echo"Shortlisting"; } else { echo"Not Shortlisting"; } ?>
However this doesnt seem to be working properly as it is showing as Not Shortlisting even when stage_name equals Shortlisting.
Any ideas why?
Its variable type mistake. Check your assigned variable, you assigned the Array Element not the entire array. so try like below.
<?php
$result = mysql_query("SELECT * FROM hr_recruitment_stages where vacancy_ref='$vacancyref' order by added_on DESC limit 0,1") or die('ERROR 315' );
$row = mysql_fetch_array($result);
$stage_name = $row['stage_name'];
if($stage_name == 'Shortlisting') {
echo"Shortlisting";
} else {
echo"Not Shortlisting";
}
?>
Refer this Article for PHP Array understanding.
http://php.net/manual/en/language.types.array.php

PHP - Check table to see if entry exists

I need to check if a record exists in a table before adding it.
I've done some digging and this is what people keep coming back too:
$result= mysql_query("SELECT id FROM mytable WHERE city = 'c7'");
if(mysql_num_rows($result) == 0) {
// row not found, do stuff...
} else {
//row found, do other stuff...
}
or some variation there of.
This logic is exactly what I need except for the fact that $result is never returning a positive result.
The record does exist and should return a positive result.
I also tried
$sql="SELECT COUNT(email) FROM table WHERE email=$mail;";
$yesorno = mysqli_query($sql);
echo $yesorno ;
as a test and the echo returns no value.
you need to check first if the query succeed running maybe there is a problem with it
$Query = "Select id from mytable where city = 'c7' ";
if($result = mysql_query($Query)) {
if ( mysql_num_rows($result) == 0 ) {
// no rows found ;
} else {
// row exist ;
}
} else {
echo "Query failed ". $Query;
}

$result equals nothing if mysql column not found?

How could i get $result too equal nothing if the column doesn't exist in PHP?
I was thinking something like this:
$result=mysql_query("SELECT * FROM users WHERE username=$key DESC LIMIT 1")or die ($result = '');
But i was told thats wrong.
It's wrong because you're killing the script with die when a DB error occurs, rather than doing stuff when you find no row.
What you presumably need is more like:
$result = mysql_query($query);
if ($result) {
if ($row = mysql_fetch_assoc($result)) {
// do stuff with row
} else {
// do stuff without row
}
} else { // not needed but left here for illustration purposes
// this is the part that would occur, had you called mysql_query(...) or die;
die(mysql_error());
}
$result=mysql_query("SELECT * FROM users WHERE username= '$key' LIMIT 1")or die (mysql_error());
then check the result of mysql_num_rows()
If you mean that the result returns 0 rows, you can check with mysql_num_rows, like this:
$result=mysql_query("SELECT * FROM users WHERE username=$key DESC LIMIT 1");
if (mysql_num_rows($result) == 0)
$result = '';
Your code will set $result to '' if there's an error, in which case mysql_query returns false. It will also halt the code, since you're calling die(). An empty result set is not an error, however. In that case mysql_query returns a valid resource identifier with no rows. If I understand your question, this is what you want to do:
$result=mysql_query("SELECT * FROM users WHERE username=$key DESC LIMIT 1");
if (mysql_num_rows($result) == 0){
$result = '';
}
<?php
// Here I assume you're using PHP PDO
$pdo = new PDO("mysql:server=localhost;dbname=mydatabase", "root", "");
$result = $pdo->query("SELECT * FROM users WHERE username=$key DESC LIMIT 1");
$errorcode = $pdo->errorCode();
$errorinfo = $pdo->errorInfo();
// Columns doesn't exist
if($errorcode == "43072") $result = "";
// Other error...
else if($errorcode != "00000") die("MySQL Error: " . $errorinfo[2]);
?>

Categories