Echo Nothing when Query is Empty - php

I am trying to check if the user input $entry is empty, and if it is, to echo nothing. I don't want it to do anything. There are no results, it shouldn't display anything. However, I keep getting an empty query error.
Here is my php:
$link = fConnectToDatabase();
//Retrieve parameters from querystring
$entry = fCleanString($link, $_GET['entry'], 15);
if (!empty($entry)) {
$sql = "select price, name, image
from geekproducts
where name like '%$entry%' or ' $entry '
order by name";
}
$result = mysqli_query($link, $sql)
or die('SQL syntax error: ' . mysqli_error($link));
if (! ($result)){
echo "";
}
How do I get my page to display nothing when the result is null?
EDIT The problem seems to be solely with
$result = mysqli_query($link, $sql)
or die('SQL syntax error: ' . mysqli_error($link));
Getting rid of the or die statement returns a big paragraph on syntax errors. Any ideas?

How about
if (isset($_GET['entry']) {
$entry = fCleanString($link, $_GET['entry'], 15);
$sql = "select price, name, image
from geekproducts
where name like '%$entry%' or ' $entry '
order by name";
$result = mysqli_query($link, $sql)
or die('SQL syntax error: ' . mysqli_error($link));
} else {
echo 'nothing ;)';
}

you can do it like this:
if(mysql_num_rows($result)==0)echo '';

I think what you're trying to do is suppressing the error
$link = fConnectToDatabase();
//Retrieve parameters from querystring
$entry = fCleanString($link, $_GET['entry'], 15);
if (!empty($entry)) {
$sql = "select price, name, image
from geekproducts
where name like '%$entry%' or ' $entry '
order by name";
}
$result = #mysqli_query($link, $sql)
Adding # before the function suppresses errors

Related

mysql insert/update always inserting never updating

I have the following code to insert if a row doesn't exist with the given part_code or update if the part_code already exists. The problem is that it always inserts and produces duplicates. Can anyone see why.
$query = 'SELECT * FROM qty_csv';
$result = mysqli_query($conn,$query);
if (!$result) {
die(mysqli_error($conn));
}
while($row = mysqli_fetch_array($result)){
$part_code = $row['code'];
$part_descr = $row['descr'];
$part_qty_in_stock = $row['qty_in_stock'];
$reorder_level = $row['reorder_level'];
$reorder_qty = $row['reorder_qty'];
$part_price = $row['price'];
// check to see if the value you are entering is already there
$result1 = $conn->query("SELECT * FROM part WHERE part_code == '$part_code'");
if (!$result1){
$sql =
"INSERT INTO part VALUES (
'',
'',
'$part_descr',
'$part_price',
'',
'$part_code',
'',
'',
'',
'',
'$reorder_level',
'$reorder_qty',
'',
'',
'$part_qty_in_stock',
'',
'',
''
)";
if (!$conn->query($sql)) {
echo "INSERT failed: (' . $conn->errno . ') " . $conn->error;
}
}else{
$sql = "UPDATE part SET "
. "part_qty_in_stock = '$part_qty_in_stock',"
. "reorder_level = '$reorder_level',"
. "reorder_qty = '$reorder_qty' "
. "WHERE part_code == '$part_code'";
// This code exists and will be updated
$conn->query($sql);
}
}
I have it under the debugger and see that even if part_code == '$part_code' the $result1 is bool()false. I would have expected it to be true.
The problem here is that your "$result1" object is False only when an error happens, otherwise, even if the query returns no results (because it's empty), it will be a mysqly object.
You should check the number of rows rather than just checking !$result.
Just change
if (!$result1){
with
if (mysqli_num_rows($result1) == 0){
Check out the docs for more info:
http://it1.php.net/mysqli_query
http://www.php.net/manual/en/mysqli-result.num-rows.php
please check your query,please use = for checking instead of ==
SELECT * FROM part WHERE part_code = '$part_code'
Use
$result1 = $conn->query("SELECT count(*) as count FROM part WHERE part_code = '$part_code'");
Then simply check $result1['count'] > 0

Query was empty PHP error

I am trying to build a cart using MySQL. I keep getting this error 'Query was empty' when I run this code. Please help I've tried several things such as putting the variables inside the string instead of concatenating it.
<?php ob_start(); ?><?php require_once("../include/membersite_config.php"); ?>
<?php
require('../products_reloaded/config.php');
session_start();
$user = $_REQUEST['user'];
$user = mysql_real_escape_string($user);
$itemNum = $_REQUEST['itemNum'];
$itemNum = mysql_real_escape_string($itemNum);
$quantity = $_POST['quantity'];
$quantity = intval($quantity);
$CheckForExistence = mysql_query("select * from cart where user = '$user' and p_number = '$itemNum'" );
$alreadyExistsChecker = mysql_num_rows($CheckForExistence);
if($alreadyExistsChecker >= 1)
{
$quantity +=1;
echo "this is equal to $alreadyExistsChecker";
}
if($alreadyExistsChecker == 0)
{
$getQuery = mysql_query("select * from product where p_number = '$itemNum'");
while($row = mysql_fetch_array($getQuery))
{
$name = $row['p_name'];
$image = $row['p_url'];
$price = $row['p_price'];
}
$name = mysql_real_escape_string($name);
$image = mysql_real_escape_string($image);
$price = intval($price);
$query = mysql_query('insert into cart values('.$user.','.$itemNum.','.$name.', '.$image.','.$quantity.', '.$price.')');
$result = mysql_query($query);
if (!$result) {
print "An error occured: " . mysql_error() . "\n";
}
}
header('http://www.definitionxjm.com/shopping/viewCart.php');
?>
What do you try to achieve with this line?
$result = mysql_query($query);
Just delete it and change the line above to
$result = mysql_query('insert into cart values('.$user.','.$itemNum.','.$name.', '.$image.','.$quantity.', '.$price.')');
[Edit]
Btw. you are forgetting the " (quotes) inside the query which causes an sql error to occur, leading to $query = false (see manual). $query (false) then gets converted to string, resulting in '' (an empty string) which you pass to mysql_query and that generates an "Query was empty"-Message, as it should because you tried to send an empty query.
It can also mean the table which is in consideration is empty.This condition arises when the "DELETE FROM table_name where ;" is used. Here if the table "table_name" has no data, it cannot delete anything and hence it shows an error stating that the query is empty.

Undefined connection variable in PHP

I've got a php script as follows:
function addPost(BlogPost $item, $tags) {
$connection = mysql_connect('localhost', '***', '***') or die(mysql_error());
mysql_select_db('jschaible1') or die(mysql_error());
$queryString = "insert into BlogPost values ( null, '" . $item->Title . "', '" . $item->Body . "', " . "now());";
$result = mysql_query($queryString) or die(mysql_error());
$dbResult = mysql_query('select * from blogpost where Title = "' . $item->Title . '";') or die(mysql_error());
while ($row = mysql_fetch_array($dbResult)) {
$tableID = $row['BlogPostID'];
}
foreach($tags as $t) {
$queryString = "insert ignore into Tag values('" . strtolower($t) . "');";
mysql_query($queryString) or die(mysql_error());
$queryString = "insert into blogposttag values (" . $tableID . ", '" . strtolower($t) . "');";
mysql_query($queryString) or die(mysql_error());
}
echo $connection;
mysql_close($connection) or die(mysql_error());
}
The function is being called like this:
<?php
session_start();
$errors = '';
if (!isset($_SESSION['dadfg6d5f6df54']))
header('Location:admin.php');
else {
include('Classes.php');
include('mySql.php');
include('utils.php');
if(isset($_POST['Submit'])) {
if ($_POST['Title'] == '') {
$errors = 'Post must have a title!';
}
else if ($_POST['PostBody'] == '') {
$errors = 'Post must be something!';
}
else if (strlen($_POST['PostBody']) < 10) {
$errors = "Write something substantial, c'mon!";
}
else if ($_POST['Tags'] == '') {
$errors = "At least one tag must be entered";
}
else {
$newPost = new BlogPost(NULL, sanitize($_POST['Title']), sanitize($_POST['PostBody']), NULL);
$newPost->Title = addEmoticons($newPost->Title);
$newPost->Body = addEmoticons($newPost->Body);
$tags = str_replace(',', '', $_POST['Tags']);
$tags = str_replace(';', '', $tags);
$tags = explode(' ', $tags);
error_reporting(E_ALL); ini_set('display_errors', 1);
addPost($newPost, $tags) or die();
$errors = 'Post added successfully';
}
}
}
?>
When it gets to mysql_close(), the page just stops executing and I get a blank page. This is really frustrating me, I don't understand at all why it's happening, especially seeing as how the echo on the PREVIOUS line puts out "resource id#6". I get no error message, just a blank page! Please help!
Your function has no return value. It therefore returns NULL which evaluates as a "falsy" value. Since you follow it with an or die() call, the false evaluation triggers the or die() and terminates your script.
// Don't do this:
addPost($newPost, $tags) or die();
// Do this:
addPost($newPost, $tags);
In the end of your function, you could return TRUE, but it is entirely unnecessary unless you wish to return a value based on the success or failure of your post addition. The way you have it, the die() is just causing undue harm. Since all the potential failing points in your function are already going to terminate the script on error, there is no great purpose to returning TRUE. Just remove the die() after the function call.

PHP Mysql script error

I send data too this script from a C# script inside of Unity 3d . What I want to do is get the page to display the result of whatever query i end up running from it , and inside of Unity I get the text of that page and display it .
This is a very jerryridged way of doing this , but i'm just starting with PHP and mysql
Here's the error that unity 3d gives me
"Warning: mysql_result(): supplied argument is not a valid MySQL result resource in WEBSITE.com/newget.php on line 30"
<?php
$db = mysql_connect('host, user, pass') or die('Could not connect: ' . mysql_error());
mysql_select_db('dbname') or die('Could not select database');
$gotString = mysql_real_escape_string($_GET['GetString'], $db);
$hash = $_GET['hash'];
//SELECT * FROM table_name
$real_hash = md5($gotString . $secretKey);
$secretKey = "KeyHERE";
$real_hash = md5($gotString . $secretKey);
$locString = "SELECT A FROM Quiz1 WHERE Question = 1";
if ($real_hash == $hash) {
Compare();
}
function Compare() {
if ($gotString == "1B") {
$result = mysql_query("SELECT B FROM Quiz1 WHERE Question = 1") or die(mysql_error());
} else {
$result = mysql_query("SELECT A FROM Quiz1 WHERE Question = 1") or die(mysql_error());
}
}
print( $result);
?>
Try use
if (!$result) {
die('error: ' . mysql_error());
} else {
$row = mysql_fetch_array($result);
var_dump($row);
}
instead of Print( $result) ; at the end

PHP error with mysql_num_rows

I have a simple form which when submitted should count the number of rows in a table which already have the same value as that submitted.
I can't work out why this is returning an error... any ideas?
The error is Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in .../register.php on line 31
$con = mysql_connect("table","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
function check_input($value, $quoteIt)
{
// Stripslashes
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
// Quote if not a number
if (is_null($value) || $value=="") {
$value = 'NULL';
} else if (!is_numeric($value) && $quoteIt == 1) {
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}
$useremail = check_input($_POST['useremail'], 1);
// Check to see if email address already exists in USERS table
$query="SELECT * FROM users WHERE email = $useremail";
$result = mysql_query($query);
echo $query;
echo mysql_num_rows($result); //THIS IS LINE 31
mysql_close();
You can see your query error using this, because of query error only this error showing
$result = mysql_query($query) or die(mysql_error());
<?php
$con = mysql_connect("table","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
function check_input($value, $quoteIt)
{
// Stripslashes
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
// Quote if not a number
if (is_null($value) || $value=="") {
$value = 'NULL';
} else if (!is_numeric($value) && $quoteIt == 1) {
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}
$useremail = check_input($_POST['useremail'], 1);
// Check to see if email address already exists in USERS table
$query="SELECT COUNT(1) FROM users WHERE email = $useremail";
$result = mysql_query($query);
$array = mysql_fetch_array($result);
$whatYouWant = $array[0][0];
mysql_close();
?>
But Leigh is right, prefer PDO instead of mysql_* functions...
Usually this means the query is wrong. Try backquotes for fields:
"SELECT * FROM `users` WHERE `email` = '$useremail'"
Replace
$result = mysql_query($query);
with
$result = mysql_query($query, $con);

Categories