-1

I have tried several different scripts listed on StackOverflow and they all give the same error. I know the record exists in the database. Any help or answers please.

Latest script I tried :-

<?php
$team='West Ham';
$query = "SELECT * from analysed where team = '$team'";
$result = mysql_query($query);

if(mysql_num_rows($result) > 0)
{
    echo " exists";
} else {
    echo " Dont exists";
}
?>

I even tried

$query = "SELECT * from analysed where 1d = '1'";

and

$query = "SELECT * from analysed where 1d = 1";

and got same error

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\football\test-update.php on line 14

RiggsFolly
  • 89,708
  • 20
  • 100
  • 143
kerry
  • 629
  • 1
  • 5
  • 9
  • Did you connect to your DB ? – PoulsQ Sep 05 '16 at 09:44
  • Stop using the the deprecated mysql_ functions and switch to MySQLi and also check DB connection – Bibek Jana Sep 05 '16 at 09:45
  • The mysql_ functions are deprecated - avoid and upgrade your code to mysqli or pdo – Melchizedek Sep 05 '16 at 09:46
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in _meow_ code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Sep 05 '16 at 09:47
  • I was having problems with a really long script so I just cut and pasted the section giving problems. I started testing and got this new error - and yes I forgot the line - include('connect-db.php'); I feel like such an idiot - I am so sorry for wasting your time. – kerry Sep 05 '16 at 09:50
  • That error means either you have not connected to your database `mysql_connect()` or your query has failed. – RiggsFolly Sep 05 '16 at 09:50
  • Have you really got a column named `1d` .... thats a numeric one before the `d`? – RiggsFolly Sep 05 '16 at 09:51
  • Yes I know I must switch to mysqli but I have so much code and need to learn the new syntax and I am lazy :-) but thanks for the advice – kerry Sep 05 '16 at 09:53

3 Answers3

0

I don't know table structure so.I not say perfect. You can check problem by

mysql_query($query) or die(mysql_error());
Bharat Dangar
  • 557
  • 3
  • 18
  • 2
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in _meow_ code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Sep 05 '16 at 09:47
  • Every time you use the mysql_ database extension in meow code a Kitten is strangled somewhere in the world :-) If you are just learning PHP, spend your energies learning the PDO or mysqli database extensions. Start here - I know you are right - let me finish this project then I wll save some kittens :-) – kerry Sep 05 '16 at 10:17
  • @kerry Writing code with the crappy `mysql_query` interface doesn't save time. It only creates problems. Write it your code correctly the first time. PDO raises exceptions on errors, it's significantly more friendly to use, it will make your life *easier* if you switch now. The error you're experiencing is probably caused by not having a valid connection. – tadman Sep 05 '16 at 11:14
0

You can use this snippet as example:

<?php

 //CONNECT FIRST
$DB_SERVER = "dbhost";
$DB_USER = "dbuser";
$DB_PASSWORD = "dbpass";
$DB_NAME = "database";

$conn = new mysqli($DB_SERVER, $DB_USER, $DB_PASSWORD, $DB_NAME);

if ($conn->connect_error) {
      die("Connection failed." . $conn->connect_error);
}

$user_name = "Radu Radu";
$query = "SELECT * from users where user_name='$user_name'";
$result = $conn->query($query);

if ($result->num_rows > 0) {
        echo "User exist!";
} else {


   echo "User does not exist!";
}
Radu Radu
  • 177
  • 16
  • Converting it to `mysqli` is great, but use *prepared statements* and never put data like `$user_name` directly in the query. This is just as sloppy as the original, plus has a query that's completely unrelated to the question. – tadman Sep 05 '16 at 11:15
  • This is an example, but I don't understand what's the query you talking about, I'm not an advanced php programmer and I just use it for personal purposes, if you have a better "implementation" I'll be very glad to see it, I'm here for learning purposes – Radu Radu Sep 05 '16 at 12:59
  • 1
    It's [right in the manual under "Prepared Statements"](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and comes with several examples to illustrate how to do it properly. This should be the only way you compose queries. – tadman Sep 05 '16 at 20:57
  • OK OK I am going to switch to mysqli :-) dont want any more dead kittens on my conscience :-) thank you all for the feedback and suggestions. PoulsQ actually answerd my question already but apparently it was not technically an answer so I cant accept it - Im working on sorting this out. Thanks again everybody. As an aside whilst most of you have been very helpful, I am new to PHP and Stackoverflow and find it an excellent resource. – kerry Sep 07 '16 at 09:51
  • But I noticed one person marked my question as a -1 - This question does not show any research, is unclear and unhelpful. I already stated I had tried 2 solutions I found on Stackoverflowbefore I posted - that is research. The question was very clear. There is no such thing as a stupid question only stupid answers and arrogance! Thanks to everyone else. – kerry Sep 07 '16 at 09:54
0

Expecting, that you already had a db connection.

<?php
$team='West Ham';
$query=mysql_query("SELECT * from analysed WHERE team ='$team'");
$result = mysql_query($query);

$return_no_of_rows=mysql_num_rows($result);
if($return_no_of_rows>0)
{
 echo "Return Atleast one row";
}
else
{
 echo "Return Nothing";
 die();
}

?>

Jaymin
  • 1,585
  • 1
  • 16
  • 37