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);
Related
I have a problem switching from MYSQL to MYSQLi. The codes work fine with MYSQL but when i change the connection to MYSQLi, I received the error as stated above when I'm fetching my query. How can i fetch my queries using mysqli functions?
Code:
function __construct(){
$this->link = mysqli_connect('localhost', 'root', '', 'ajax_rating');
if (!$this->link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . mysqli_get_host_info($this->link) . "\n";
}
function getItems($id = null){
if(isset($_GET['id']))
{
$query = $this->link->query("SELECT * FROM items WHERE id = '$id'");
}
else
{
$query = $this->link->query("SELECT * FROM items");
}
$rowCount = $query->rowCount();
if($rowCount >= 1)
{
$result = $query->fetchAll();
}
else
{
$result = 0;
}
return $result;
Use num_rows
Read MySQLi row_count
So final answer would be
function getItems($id = null)
{
if(isset($_GET['id']))
{
$query = $this->link->query("SELECT * FROM items WHERE id = '$id'");
}
else
{
$query = $this->link->query("SELECT * FROM items");
}
$rowCount = $query->num_rows;//change here
if($rowCount >= 1)
{
$result = $query->fetchAll();
}
else
{
$result = 0;
}
return $result;
}
EDIT 01
use mysqli_fetch_all instead of fetchAll()
mysqli_fetch_all($query,MYSQLI_ASSOC);
so answer world be
if($rowCount >= 1)
{
$result = mysqli_fetch_all($query,MYSQLI_ASSOC);
}
you can use num_rows for counting rows in DB and you can direct call connection like this :-
//for connection
$con = mysqli_connect("localhost", "root", "", "ajax_rating");
if(!$con)
{
echo "connection error";
}
//query
function getItems($id = null)
{
if(isset($_GET['id']))
{
$query = mysqli_query($con,"SELECT * FROM items WHERE id = '$id'");
}
else
{
$query = mysqli_query($con,"SELECT * FROM items");
}
if (mysqli_num_rows($query) > 0)//change here
{
while($row = mysqli_fetch_assoc($query))
{
$result=$row;
}
}
else
{
$result = 0;
}
return $result;
}
I already know how could I UPDATE this values, but I am a litle confused about how can I get this current values.
So, here's my code:
<?php
//READ DB
$result = mysql_query("SELECT ativo FROM Spot", $db);
if(!$result){
die("ERRO" . mysql_error());
} else {
//RETURN RESULT
while ($result != '1') {
echo '<div>IT'S TRUE</div>';
}
echo '<div>IT'S FALSE</div>';
}
?>
The return value of mysql_query() is actually an resource identifier and not the data, you request. To get the actual data, you have to fetch it first, e.g., by using mysql_fetch_assoc():
$result = mysql_query("SELECT ativo FROM Spot", $db);
if(!$result){
die("ERRO" . mysql_error());
} else {
//RETURN RESULT
while ($row = mysql_fetch_assoc($result)) {
if( $row['ativo'] != 1 ) {
echo '<div>IT\'S TRUE</div>';
} else {
echo '<div>IT\'S FALSE</div>';
}
}
I also corrected the missing escaping of ' inside your string. See the highlighting in your question, what went wrong there.
You have to fetch the result from that query. Like this:
$query = mysql_query("SELECT ativo FROM Spot", $db);
$result = mysql_fetch_array($query);
if ($result['ativo'] == 1) {
// is 1
} else {
// not 1
}
//RETURN RESULT
while ($row=mysql_fetch_array($result)) {
echo $row['ativo'];
}
first you have to use other quotes
not
echo '<div>IT'S TRUE</div>';
should be like
echo "<div>IT'S TRUE</div>";
second
your query returns a resoure id, so it never will be 1 ^^
<?php
//READ DB
$result = mysql_query("SELECT ativo FROM Spot", $db);
if(!$result)
{
die("ERRO" . mysql_error());
}
else
{
//RETURN RESULT
while ($data = mysql_fetch_assoc($result))
{
echo $data['ativo'] . "<br/>";
}
}
?>
<?php
//READ DB
$result = mysql_query("SELECT ativo FROM Spot", $db);
if(!$result){
die("ERRO" . mysql_error());
} else {
//RETURN RESULT
while ($row = mysql_fetch_array($result)) {
$value = $row['ativo'];
if($value!=1){
echo '<div>IT\'S TRUE</div>'; // escape special chars
}
else{
echo '<div>IT\'S FALSE</div>'; // escape special chars
}
}
?>
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
So basically I have website that has names of cities that can be checked off. I save them into local storage under one key. And then I send them Via Ajax over to my php script below. Then in php I explode the city variable on "," and I filter the cities depending on which ones were given. But right now I only know how to do this manually, for example city[0], city[1] etc. Is there a way I can continually put those variables into mysql statement depending on how many there are? I am sorry if I am being confusing, I'm just totally lost. Any help would be appreciated.
Here is my code:
<?php
$con = mysql_connect("localhost", "test", "test") or die('could not connect to mysql');
mysql_select_db("test", $con) or die('could not find database');
$city2 = $_POST['city2'];
$cities = explode(",", $city2);
$rep2 = $_POST['rep2'];
$status2 = $_POST['status2'];
$size2 = $_POST['size2'];
$type2 = $_POST['type2'];
$lat2 = $_POST['lat2'];
$long2 = $_POST['long2'];
$radius2 = $_POST['radius2'];
if ($city2 == '') {
$city_stat = " city RLIKE '.*' ";
} else {
$city_stat = " (city='$cities[0]' OR city='$cities[1]') ";
}
if ($radius2 == '') {
$radius_stat = '10';
} else {
$radius_stat = $_POST['radius2'];
}
if ($size2 == '') {
$size_stat = '';
} else {
$size_stat = " AND size='$size2' ";
}
if ($rep2 == '') {
$rep_stat = '';
} else {
$rep_stat = " AND rep='$rep2' ";
}
if ($status2 == '') {
$status_stat = '';
} else {
$status_stat = " AND status='$status2' ";
}
$result = mysql_query("SELECT lat,lng,id,rep,rep_num,name,city,state,zip,address,status,category,size FROM test WHERE $city_stat $rep_stat $size_stat $status_stat LIMIT 0 , 50 ");
while ($array = mysql_fetch_assoc($result)) {
$array_json[] = $array;
}
echo json_encode($array_json);
?>
From what I think you are asking is that there might be a variable number of cities and you want to set a conditional that the city can be any of them.
So instead of:
$city_stat = " (city='$cities[0]' OR city='$cities[1]') ";
You can do something like this:
$city_stat = '(city IN ('.implode(',',$cities).'))';
Warning:mysql_fetch_array(): supplied argument is not a valid MySQL result resource in **/home/davzyco1/public_html/notes/functions.php** on line 43
was the error I got when I use the below class, even though the class works PERFECTLY with my old webhost. Here's my new hosts php info: http://davzy.com/notes/php.php
class mysqlDb
{
public $con;
public $debug;
function __construct($host,$username,$password,$database)
{
$this->con = mysql_connect($host,$username,$password);
if (!$this->con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($database, $this->con);
}
function kill()
{
mysql_close($this->con);
}
function debugOn()
{
$this->debug = true;
}
function debugOff()
{
$this->debug = false;
}
function select($query,&$array)
{
$c = 0;
$result = mysql_query("SELECT ".$query);
if($this->debug == true)
echo "SELECT ".$query;
while($row = mysql_fetch_array($result))
{
foreach($row as $id => $value)
{
$array[$c][$id] = $value;
}
$c++;
}
}
function update($update, $where,$array)
{
foreach($array as $id => $value)
{
mysql_query("UPDATE {$update} SET {$id} = '{$value}'
WHERE {$where}");
if($this->debug == true)
echo "UPDATE {$update} SET {$id} = '{$value}'
WHERE {$where}<br><br>";
}
}
function updateModern($update, $where,$array)
{
foreach($array as $id => $value)
{
mysql_query("UPDATE {$update} SET `{$id}` = '{$value}'
WHERE {$where}");
if($this->debug == true)
echo "UPDATE {$update} SET {$id} = '{$value}'
WHERE {$where}<br>";
}
}
function delete($t, $w)
{
mysql_query("DELETE FROM `{$t}` WHERE {$w}");
if($this->debug == true)
echo "DELETE FROM `{$t}` WHERE {$w}<br><br>";
}
function insert($where, $array)
{
$sql = "INSERT INTO `{$where}` (";
$sql2 = " VALUES (";
foreach($array as $id => $value){
$sql .= "`{$id}`, ";
$sql2 .= "'{$value}', ";
}
mysql_query(str_replace(', )',')',$sql.")") . str_replace(', )',')',$sql2.");"));
if($this->debug == true)
echo str_replace(', )',')',$sql.")") . str_replace(', )',')',$sql2.");")."<br><br>";
}
}
This is because mysql_query() will return FALSE if an error occured, instead of returning a result resource. You can check the error by calling mysql_error(), as shown here:
function select($query,&$array)
{
$c = 0;
$result = mysql_query("SELECT ".$query);
if($this->debug == true)
echo "SELECT ".$query;
if (!$result) {
// an error occured, let's see what it was
die(mysql_error());
}
while($row = mysql_fetch_array($result))
{
foreach($row as $id => $value)
{
$array[$c][$id] = $value;
}
$c++;
}
}
Based on the error message, you can find out what the real problem is.
You should really test to see if $result is not false before using it with mysql_fetch_array. The error you're receiving is indicative that the query itself failed.
Have you configured your database with your new host? (do all the tables exist?)
As Mark said above, you really should check your result before trying mysql_fetch_array on a result set, and verify that all tables actually exist.
Without knowing how your original server was set up, I can only guess, but it may also be that your old server was set up to not display warnings.