How can i execute a program if the monster id doesnt exist? - php

This is my current code.
$monster_result = mysql_query("SELECT * FROM xx_monsters WHERE MonsterId = '$monster_id'") or die(mysql_error());
$char_result = mysql_query("SELECT * FROM xx_characters WHERE id = '" . $char_id . "'") or die(mysql_error());
$char = mysql_fetch_assoc($char_result);
if (mysql_num_rows($monster_result) > 0) {
$monster_result = mysql_query("SELECT * FROM xx_monsters WHERE MonsterId = " . $monster_id) or die(mysql_error());
//I tried adding this, but for some reason it messed up the entire thing
//exec("/usr/local/bin/lostID $monster_id");
} else {
$level = $char['level'] + 1;
$level2 = $char['level'] - 10;
$monster_result = mysql_query("SELECT * FROM xx_monsters WHERE Level < " . $level . " AND Level > " . $level2 . " AND ExtraData != 'trainer' AND ExtraData != 'starter' ORDER BY RAND() LIMIT 1") or die(mysql_error());
}
$monster = mysql_fetch_assoc($monster_result);
I tried to add it under else too, but it renders the page totally blank, there are echo statements after this. Whats wrong?

Try the function isset($variable_name)

I would suggest to use
if(!empty($monster_id)) {
// your code here
}
Empty function will also make sure that variable value should not be null or empty string.

Related

Error in SQL syntax at line 1 php/sql

The first problem was with mysql_query which returns FALSE and mysql_num_rows expects parameter 1 to be resource not a boolean. I ve made this to get the error
if($itemsres === FALSE)
die(mysql_error());
Now i have this error " You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
<?php
ob_start();
session_start();
require("header.php");
require("functions.php");
echo "<h1>Your shopping cart</h1>";
showcart();
if(isset($_SESSION['SESS_ORDERNUM']) == TRUE) {
$sql = "SELECT * FROM orderitems WHERE order_id = " .
$_SESSION['SESS_ORDERNUM'] . ";";
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
if($numrows >= 1) {
echo "<h2><a href='checkout-address.php'>Go to the checkout</a></h2>";
}
}
require("footer.php");
?>
The problem started from here.
if(isset($_SESSION['SESS_LOGGEDIN']))
{
$custsql = "SELECT id, status from orders WHERE customer_id = ".
$_SESSION['SESS_USERID']. " AND status < 2;";
$custres = mysql_query($custsql);
$custrow = mysql_fetch_assoc($custres);
$itemssql = "SELECT products.*, orderitems.*, orderitems.id AS itemid FROM
products, orderitems WHERE orderitems.product_id =products.id AND order_id
= " . $custrow['id'];
$itemsres = mysql_query($itemssql);
if($itemsres === FALSE)
die(mysql_error());
$itemnumrows = mysql_num_rows($itemsres);
}
mysql_num_rows expects parameter 1 instead of boolean which is given by mysql_query and after i made that check now i have this sql error.
<?php
ob_start();
session_start();
require("config.php");
if(isset($_SESSION['SESS_LOGGEDIN']) == TRUE) {
header("Location: " . $config_basedir);
}
if(isset($_POST['submit']))
{
$loginsql = "SELECT * FROM logins WHERE username = '" . $_POST['userBox'].
"' AND password = '" . sha1($_POST['passBox']) . "'";
$loginres = mysql_query($loginsql);
$numrows = mysql_num_rows($loginres);
if($numrows == 1)
{
$loginrow = mysql_fetch_assoc($loginres);
session_start("SESS_LOGGEDIN");
session_start("SESS_USERNAME");
session_start("SESS_USERID");
$_SESSION['SESS_LOGGEDIN'] = 1;
$_SESSION['SESS_USERNAME'] = $loginrow['username'];
$_SESSION['SESS_USERID'] = $loginrow['id'];
$ordersql = "SELECT id FROM orders WHERE customer_id = " .
$_SESSION['SESS_USERID'] . " AND status < 2"; $orderres =
mysql_query($ordersql); $orderrow = mysql_fetch_assoc($orderres);
session_start("SESS_ORDERNUM"); $_SESSION['SESS_ORDERNUM'] =
$orderrow['id']; header("Location: ".$config_basedir);
}
else {
header("Location: http://" .$_SERVER['HTTP_HOST']. $_SERVER['SCRIPT_NAME'] .
"?error=1");
}
}
else {
require("header.php");
?>
You have a syntax error
Use query like this
$sql = "SELECT * FROM orderitems WHERE order_id = $_SESSION['SESS_ORDERNUM']";
Or
$sql = "SELECT * FROM orderitems WHERE order_id = ".$_SESSION['SESS_ORDERNUM'];
Or
$sql = "SELECT * FROM orderitems WHERE order_id = '$_SESSION['SESS_ORDERNUM']'";
NOTE: SQL Injection.
PS: Use prepared statements
$loginrow = mysql_fetch_assoc($loginres);
session_start();
$_SESSION['SESS_LOGGEDIN'] = 1;
$_SESSION['SESS_USERNAME'] = $loginrow['username'];
$_SESSION['SESS_USERID'] = $loginrow['id'];
$ordersql = "SELECT id FROM orders WHERE customer_id = " .
$_SESSION['SESS_USERID'] . " AND status < 2";
$orderres =
mysql_query($ordersql); $orderrow = mysql_fetch_assoc($orderres);
$_SESSION['SESS_ORDERNUM'] =
$orderrow['id']; }
First of all, you should check the value of $_SESSION['SESS_ORDERNUM'].
The error means the created query($sql) is not valid, so you should debug $sql.

how to send object/database connection to function in php

i open a sql connection and try to send it to another function and it doesnt work .
its probbably something with sintax ,
i search all over the internet and couldnt find it .
my code :
$mainDataBase = mysqli_connect(/...../);
if ($mainDataBase->connect_errno) {
printf("Connect failed: %s\n", $mainDataBase->connect_error);
die;
}
/**
* 1 -> logIn && getInformation
*
* */
switch ($whichOperation){
case 1:
$gettingPlayerNumber = checkIfPlayerExist($mainDataBase, $playerName, $playerPass);
if($gettingPlayerNumber == 0){
echo "F";
die;
}
... keep going ...
function ::
function checkIfPlayerExist($dataBase ,$playerName ,$pass ){
$result = $dataBase->query("select playerNumber from PlayerInformation where (playerAccount = '" . $playerName ."') && (playerPass = " . $playerPass .")");
$row = $result->fetch_assoc();
if($row != null)
return $row['playerNumber'];
return 0;
}
its says:
Call to a member function fetch_assoc() on a non-object.
thank you for your time.
after some of the answer i checked and $result is null , but i checked the query before and now and it works!.
pass is int so it doesnt need any '' around it in the query.*
its connected succsesfully to the database - i checked this too
there seems to be a bug in your query,near password,missing quotes:
change,
$result = $dataBase->query("select playerNumber from PlayerInformation where (playerAccount = '" . $playerName ."') && (playerPass = " . $playerPass .")");
to
$result = $dataBase->query("select playerNumber from PlayerInformation where (playerAccount = '" . $playerName ."') && (playerPass = '" . $playerPass ."')");

how to access fetch value from database using mysql and this value use in same form any were

use query for access the $current_rank this value want to access in different query but this value can not access any where in different query so how to access $current_rank......
$query = "select * from menu_master where menu_id =
$row_id and hotel_id='" . $_REQUEST['hotel_id'] . "'";
$result = mysql_query($query)."<br/>";
while($row=mysql_fetch_array($result))
{
$rank = $row['set_rank'];
}
$current_rank = $rank;
//echo $current_id = $row_id."<br/>";
//echo $new_rank =$_REQUEST['set_rank']."<br/>";
$sql = "select * from menu_master where set_rank = '$new_rank ' and hotel_id='".$_REQUEST['hotel_id']."'" ;
// echo $sql."<br/>";
$rs = mysql_query($sql)."<br/>";
while($row = mysql_fetch_array($rs))
{
$menu_id = $row['menu_id'];
$sql="update menu_master
set set_rank=$current_rank where menu_id= $menu_id and hotel_id='".$_REQUEST['hotel_id']."'";
//echo $sql."<br/>";
mysql_query($sql)."<br/>";
}
$sql="update menu_master set menu_name = '" . mysql_real_escape_string($_REQUEST['menu_name']) . "',
menu_name_ar = '" . mysql_real_escape_string($_REQUEST['menu_name_ar']) . "',
is_active = '" . $is_active . "',
set_rank = $new_rank where menu_id = '$current_id' and hotel_id='".$_REQUEST['hotel_id']."'";
//echo $sql."<br/>";
//exit;
mysql_query($sql);
Your current_rank seems to be an array. If you have single value in current_rank, then do not use while loop for it.
Just use $row=mysql_fetch_array($result);
$current_rank = $row['set_rank'];
Also you have commented out this line.
//echo $new_rank =$_REQUEST['set_rank']."";
So you have no value for $new_rank

While Loop in SQL query

I'm not sure why this SQL query is not working.
I'm new to SQL/PHP so please forgive.
mysql_query("
SELECT * FROM table WHERE name = " . "'Bob'" .
while($i < $size)
{
$i++;
echo "OR name = '";
echo $array[$i] . "'";
} .
" ORDER BY id DESC "
);
Dreamweaver gives me an error saying it is not correct but does not tell me what is wrong.
Is it possible to put a while loop into an sql command?
you can not use a while in a string
$where = "";
if ($size > 0)
{
$where .= " WHERE ";
}
while($i < $size)
{
$i++;
$where .= "OR name = '".$array[$i]."' ";
}
$query = "SELECT * FROM table WHERE name = '".Bob."'".$where." ORDER BY id DESC";
mysql_query($query);
(this code is not tested)
Woot !
You just can't write this :D
Build your OR condition before writing the query and it will be just fine:
$myCondition = " ";
while($i < $size) {
$i++;
$myCondition .= "OR name = '" . $array[$i] . "'";
}
mysql_query(
"SELECT * FROM table WHERE name = " . "'Bob'" . $myCondition . " ORDER BY id DESC ");
echo is to output the string, and it won't return the string.
Something like $str = "aaa" . echo "bbb"; won't work.
For you case, use IN will be better.
foreach ($array as &$name) {
$name = "'".mysql_real_escape_string($name)."'";
}
mysql_query("SELECT * FROM table WHERE name IN (".implode(',', $array).")");
Or use
"SELECT * FROM table WHERE name IN(".implode( ',', $array).")";

Out of memory, Mysql_query() unable to save resultset

I try to load about 30.000 record to mysql select. than i get this warning.
and than i try to break them into smaller limit. Every 1000. but why i still get this warning?
this is my code :
function processDataDb()
{
$rowIncrement = 1000;
$query = "SELECT count( * ) as totalfield FROM `isc_xml_data` ";
$result = mysql_query($query) or die ("$query" . mysql_error());
if ($result)
{
$row = mysql_fetch_assoc($result);
$total_row = $row['totalfield'];
for($i = 0; $i < $total_row ; $i+=$rowIncrement)
{
$sql_c = "SELECT * FROM isc_xml_data order by id LIMIT " . $i . " , " . ($i + $rowIncrement - 1);
$res_c = mysql_query($sql_c) or die (mysql_error());
while($row = mysql_fetch_object($res_c))
{
echo $row->id."\n";
$this->insertAndUpdateProduct($row->xmldata);
}
unset($res_c);
}
} else {
echo "result is unavailable";
}
}
Thanks for any help.
$sql_c = "SELECT * FROM isc_xml_data order by id LIMIT " .
$i . " , " . ($i + $rowIncrement - 1);
http://dev.mysql.com/doc/refman/5.0/en/select.html#id714605
The second argument to LIMIT is how many you want, not the upper bound; try just $rowIncrement.
unset($res_c); It's not valid, there is mysql_free_result($res_c) function for this.
Also, you can do it without first query with count(*) - just make your cycle queries until at least one row returned.
Because of this line:
$this->insertAndUpdateProduct($row->xmldata);
it's your XML processing being very inefficient memory-wise,
while mysql consumes no more than only one row contents.

Categories