Jump to content
Pheonix

Add Experience to Player

Recommended Posts

I want to give a user experience if they defeat the monster. I want to use mysqli. I would appreciate your help. Please checkout my current fight.php  I am new so my code is sloppy. 

fight.php

  • Like 1

Share this post


Link to post
Share on other sites

That is my problem I think. Updating the old mysql to mysqli. I tried the free mccodes but, it did not install all the way. 

Share this post


Link to post
Share on other sites

What type of error message do you get ? Upon first glance the script you posted looks like it should work. I think Magictallguy is on the right track and the problem lies in the connection.

Share this post


Link to post
Share on other sites
On 2/6/2020 at 12:17 AM, Pheonix said:

That is my problem I think. Updating the old mysql to mysqli. I tried the free mccodes but, it did not install all the way. 

MCC Lite (and v1) are hard-coded to MySQL.
So you've got quite the task ahead of you. Not only would you need to initialise the MySQLi connection, you'd then need to update every file that runs a query.

Basic instantiation (OOP):

// Begin connection
$db = new mysqli('host', 'user', 'pass', 'dbname');
// Verify connection
if($db->connect_error) {
    exit('Connect Error (' . $db->connect_errno . ') ' . $db->connect_error);
}

Basic instantiation (procedural):

// Begin connection
$db = mysqli_connect('host', 'user', 'pass', 'dbname');
// Verify connection
if(mysqli_connect_error()) {
    exit('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}


Assume variables have values: $id, $name
Queries (OOP - non-prepared): (assume $name is escaped)

// Select
$select = $db->query('SELECT userid, username FROM users WHERE userid = '.$id) or exit($db->error);

// Insert
$db->query('INSERT INTO users (userid, username) VALUES ('.$id.', "'.$name.'")') or exit($db->error);

// Update
$db->query('UPDATE users SET username = "'.$name.'" WHERE userid = '.$id) or exit($db->error);

// Delete
$db->query('DELETE FROM users WHERE userid = '.$id) or exit($db->error);

Queries (procedural - non-prepared): (assume $name is escaped)

// Select
$select = mysqli_query($db, 'SELECT userid, username FROM users WHERE userid = '.$id) or exit(mysqli_error($db));

// Insert
mysqli_query($db, 'INSERT INTO users (userid, username) VALUES ('.$id.', "'.$name.'")') or exit(mysqli_error($db));

// Update
mysqli_query($db, 'UPDATE users SET username = "'.$name.'" WHERE userid = '.$id) or exit(mysqli_error($db));

// Delete
mysqli_query($db, 'DELETE FROM users WHERE userid = '.$id) or exit(mysqli_error($db));

Queries (OOP - prepared):

// Select (with parameters)
$statement = $db->prepare('SELECT userid, username FROM users WHERE userid = :id'); 
$statement->bind(':id', $id);
$statement->execute();

// Select (without parameters)
$statement = $db->query('SELECT userid, username FROM users');
$statement->execute();
// -------

// Insert (with parameters)
$statement = $db->prepare('INSERT INTO users (userid, username) VALUES (:id, :name)');
$statement->bind(':id', $id);
$statement->bind(':name', $name);
$statement->execute();

// Insert (without parameters)
$statement = $db->query('INSERT INTO settings (name, content) VALUES ("demonstrative_name", "some value")');
$statement->execute();


Returning data (OOP - non-prepared): (assume $name is escaped)

// 1 result expected, assume matching data exists
$select = $db->query('SELECT userid, username FROM users WHERE username = "'.$name.'" LIMIT 1');
$row = $db->fetch_assoc($select);
echo $row['username'];

// multiple results expected, assume matching data exists
$select = $db->query('SELECT userid, username FROM users WHERE userid BETWEEN 1 AND 100');
while($row = $db->fetch_assoc($select)) {
    echo $row['username'].'<br>';
}

Returning data (procedural): (assume $name is escaped)

// 1 result expected, assume matching data exists
$select = mysqli_query($db, 'SELECT userid, username FROM users WHERE username = "'.$name.'" LIMIT 1');
$row = mysqli_fetch_assoc($select);
echo $row['username'];

// multiple results expected, assume matching data exists
$select = mysqli_query($db, 'SELECT userid, username FROM users WHERE userid BETWEEN 1 AND 100');
while($row = mysqli_fetch_assoc($select)) {
    echo $row['username'].'<br>';
}

Returning data (OOP - prepared):

// 1 result expected, assume matching data exists
$stmt = $db->prepare('SELECT userid, username FROM users WHERE username = :name LIMIT 1');
$stmt->bind(':name', $name);
$stmt->execute();
$row = $stmt->fetch_assoc();
echo $row['username'];

// multiple results expected, assume matching data exists
$stmt = $db->query('SELECT userid, username FROM users WHERE userid BETWEEN 1 AND 100');
$rows = $stmt->fetch_assoc();
foreach($rows as $row) {
    echo $row['username'].'<br>';
}



Recommendation: Throw your MySQLi into a wrapping class. Doing this means you'd be able to change your connection type in one file without having to edit absolutely everything that relies on it.
Recommendation: Update your code to use Prepared Statements.
Recommendation: OOP vs Procedural: It's entirely up to you which method you use. However, you're working on a project that re-uses variables/functions quite often, so I'd advise OOP

Note: Prepared Statements are not available in procedural-style MySQLi.

  • Like 3

Share this post


Link to post
Share on other sites

I am sorry. It should work now. Thank you for all your help Magic. I am thinking of releasing my files for help with my game problem. 

 

error_log

Edited by Pheonix
Added error Logs
  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...