2

I tried to copy How to Check if value exists in a MySQL database and make my own, but for some reason it wont work...

This is what I got:

<?php
$host = '127.0.0.1';
$username = 'root';
$password = '';
$dbname = 'multiplayer';
$con=mysqli_connect($host, $username, $password, $dbname);

$check_player_ip=mysqli_query($con, 'SELECT `player_ip` FROM `playerdata` WHERE username = "remco" AND active = 0');
    if (mysqli_num_rows($check_player_ip) == 0) {
      //didnt find anything
    } else{
      //found something
    }
?>

I get this error: Parse error: syntax error, unexpected '$check_player_ip' (T_VARIABLE) in C:\xampp\htdocs\test.php on line 1

Solution

If you get the T_VARIABLE error, check the varriable before this rule. You may forgot the place the ';' xD

Thanks for all support!

Community
  • 1
  • 1
Remco
  • 37
  • 7
  • You have an error (extra i) : **mysqli_query** should be **mysql_query** – Arkni Oct 04 '14 at 20:59
  • 1
    @Arkni Absolutely not. Rather `mysql_crap` should *at least* be `mysqli_crap`. Suggestions to the contrary ought to be posted a decade ago. – user2864740 Oct 04 '14 at 21:01
  • @user2864740 ,So in this case he should add the connection instance as first parameter(Edit: he edited his question). – Arkni Oct 04 '14 at 21:06
  • @Arkni Ah, now we're getting somewhere :D – user2864740 Oct 04 '14 at 21:07
  • @user2864740 Edited connection to it for your wishes :) – Remco Oct 04 '14 at 21:10
  • 1
    @Remco If you're going to change your question at every 2 mins. with updates from answers given, I will vote to close. **DON'T DO THAT**. Leave your original question as is, and place comments under answers. – Funk Forty Niner Oct 04 '14 at 21:12

3 Answers3

2

You are mixing MySQL APIs, they do not mix together. mysql_num_rows

Use mysqli_num_rows()

Also make sure your DB connection is also mysqli_* and not mysql_*

EDIT after you've edited your question.

You need to pass DB connection to your query:

$check_player_ip=mysqli_query($con,'SELECT...`

$con being your DB connection. Change accordingly.

Plus WHERE username = remco - the word remco needs to be wrapped in quotes, it's a string and not an int.

WHERE username = 'remco'


Sidenote:

Your present code is open to SQL injection.
Use prepared statements, or PDO with prepared statements.


Edit 2:

Try inverting the quotes:

$check_player_ip=mysqli_query($con, "SELECT `player_ip` FROM `playerdata` WHERE username = 'remco' AND active = 0");
    if (mysqli_num_rows($check_player_ip) == 0) {
      //didnt find anything
    } 
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • I've edited the code again and this time with "remco", still the same error :/ – Remco Oct 04 '14 at 21:12
  • @Remco Reload my answer under Edit #2 – Funk Forty Niner Oct 04 '14 at 21:14
  • I still get the T_VARIABLE error :/ – Remco Oct 04 '14 at 21:16
  • @Remco Is `username` column a VARCHAR and is `active` an `int`? – Funk Forty Niner Oct 04 '14 at 21:21
  • I got the problem, I've forget a ; to place after a variable! Thank you for helping me. Would you maybe upvote my question so I can ask tomorrow more questions on stackoverflow? – Remco Oct 04 '14 at 21:21
  • @Remco You're welcome and sure. Accept my answer though ;) because the initial problem was solved. – Funk Forty Niner Oct 04 '14 at 21:23
  • @Remco I take it you're not going to accept my answer then. – Funk Forty Niner Oct 04 '14 at 21:53
  • @Remco By the way, asking to upvote a question, isn't something that should be asked on Stack. You plan on coming back tomorrow to ask another question. I guess we know ahead of time that you're going to have problems. This isn't a "teach me how to code" or a "tutorial site", it's a site built by professionals "for" professionals. I will not exchange votes with you for mere points. What you posted in your question did **not** and was not the original cause, but something you did not show us from the start. Don't expect further help from me. Learn how to code and debug, it's your job, not ours. – Funk Forty Niner Oct 04 '14 at 21:59
  • As what you're saying is that this is a site build for professionals.... Am I then the only one who's a beginner on the whole site? Or are they already banned from this 'community'? – Remco Oct 04 '14 at 22:52
  • @Remco 90% of the time, people who are new to Stack, don't bother reading or taking the tour **http://stackoverflow.com/tour** and to understand/comprehend how to ask a question, the steps they've taken to figure out why their code isn't working, nor spending enough time to debug their code. More often than none, people give up too easily after spending no more than 10 minutes on Google 30 at the most, then immediately resort to the guys on Stack to figure out their problem **right away**. That is not how it works. Stack put the site together, I didn't. There's a certain etiquette to follow. – Funk Forty Niner Oct 04 '14 at 23:04
  • Well I've been searching for a solution for like 1,5 hour... I've readed the code serveral times but I just couldn't found the error. So thats when I thought to post this here. And about the upvote thing.... Thats only because IF I have another problem what I just cannot figure out, then I hope that I can ask that question here and not somewhere else... – Remco Oct 04 '14 at 23:09
  • @Remco Word of advice; be honest with people. If you include something to that effect in a question stating that you've spent many hours trying to fix it yourself, or that you don't know what to look for, people will genuinely want to help. When you are not honest and this being "leaving out parts of your code", then people will shy away and won't help you. Even though this is the Internet, there are actual "people" behind the screen, just like you, remember that ;) P.s.: You may not do this yourself, but I've spent up to **3 days** on a few occasions, in trying to solve a code problem. – Funk Forty Niner Oct 04 '14 at 23:13
  • @Remco Plus, don't alter code in your original question, that is a no-no. Instead, post comments under the person's answer stating that it is not working, including errors/warnings you may get in trying to use that person's answer. Something you may have tried can be made as an **Edit** and written as such, stating it is an "edit", under the original question. I'm trying to help you out here, so I hope you appreciate it. – Funk Forty Niner Oct 04 '14 at 23:15
1

you should execute that mysqli query...

while($rows = mysql_arrayAssoc($ursql)){
    $data[]=$rows;
}
if($something== $data['attribute']) //attribute(id,name...)
    echo "ok some data is in"
else 
   echo "no matching data"

I hope it help you :)

benzo
  • 421
  • 1
  • 6
  • 22
  • That doesn't help at all :) But thank you for responding. – Remco Oct 04 '14 at 21:02
  • oh ye, and ofcourse you have to setup connection do your database, didn't check well at start of php :p – benzo Oct 04 '14 at 21:04
  • I've edited the connection to the code for your wishes :) Maybe could you help me now? Or do you want to see the hole database also? :D – Remco Oct 04 '14 at 21:13
  • 1st check if your connection is ok `if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }` then in if clause `if(false===$check_player_ip)` {echo "error"} I suggest you do go debug slowly step by step...and read some documentation for MYSQLI http://php.net/manual/en/book.mysqli.php – benzo Oct 04 '14 at 21:16
  • That works fine, because otherwise my hole code wont work :) I just cut this piece of the code out of the hole part. Connection is not the problem, T_VARIABLE error is the problem. – Remco Oct 04 '14 at 21:18
  • Ffs, I got the problem, I've forget a ; to place after a variable! Thank you for helping me. Would you maybe upvote my question so I can ask tomorrow more questions on stackoverflow? :) – Remco Oct 04 '14 at 21:20
1

Try this:

<?php 
$host = '127.0.0.1';
$username = 'root';
$password = '';
$dbname = 'multiplayer';

$con = new mysqli( $host, $username, $password, $dbname );

/* Check Connection */
    if ( $con->connect_error ) {
        printf( "Connect failed: %s\n", $con->connect_error );
        exit();
    }

/* Query - Returns a resultset */
    if ( $result = $con->query('SELECT `player_ip` FROM `playerdata` WHERE username = "remco" AND active = 0 ') ) {

    if ( $result->num_rows <= 0 ) {
      //didnt find anything
          printf("No player");
    } else {
      //found something
          printf("Select returned %d rows.\n", $result->num_rows);
    }

    /* free result set */
        $result->close();
}

/* close connection */
    $con->close();
?>
c0utinh0
  • 179
  • 1
  • 8