problem of data recovery

0 favourites
From the Asset Store
For problem solving, puzzles, bubbles, platforms and other adventures.
  • Hello. I am trying to return the ID of my user.

    except that nothing is displayed.

    <?php header('Access-Control-Allow-Origin: *'); // Connection BDD /////////////////////////////////// try { $base = new PDO('mysql:host=localhost; dbname=stockage', 'root', ''); } catch(exception $e) { die('Erreur '.$e->getMessage()); } ///////////////////////////////////////////////////// $stmt = $base->prepare("SELECT id FROM members where username = ?"); if ($stmt->execute(array($_GET['name']))) { while ($row = $stmt->fetch()) { echo($row["id"]); } } ?>

    http://zupimages.net/viewer.php?id=19/20/n3po.png

  • I see several problems here. First, you're not binding anything to your SQL statement so it's searching for:

    SELECT id FROM members where username = ?

    It's looking for a user who's name is ?

    Next problem, you have your AJAX query malformed. It's sending exactly this:

    http://localhost/login.php?name=&Username&

    Which means, your $_GET will = &Username&

    This is how the URL should look:

    "http://localhost/login.php?name="&Username

    Notice the quote locations? Here, you're telling Construct to add in the contents of the variable Username. You don't need the & at the end unless you're adding something else to the query.

    Next problem. You're trying to use the $_GET like an array but you're saying you just want the ID of a specific user. Plus, you're passing the $_GET as a string and you're not 'imploding' it into an array.

    And finally, you're not telling MySQL to look for an array of names by using the "IN" statement, you're telling it to use the "WHERE" statement which probably would confuse it and therefore, your $stmt->execute would fail and return nothing.

    If you just want the ID of ONE user try this:

    $sql = "SELECT id FROM members where username = ?";
    $stmt = $base->prepare($sql);
    $stmt->bind_param("s", $_GET['name']);
    $stmt->execute();
    $result = $stmt->get_result();
    $row = $result->fetch_object();
    echo $row->id;
    

    Assuming that your usernames are unique, it will return just the first occurence of the $_GET that it finds.

  • And I believe (not positive) there will be another problem.

    You have the global var count set as an integer. I'm pretty sure AJAX.LastData is considered a string which means you'd need to convert it like so:

    Set count to int(Ajax.LastData)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • <?php header('Access-Control-Allow-Origin: *'); // Connection BDD /////////////////////////////////// try { $base = new PDO('mysql:host=localhost; dbname=stockage', 'root', ''); } catch(exception $e) { die('Erreur '.$e->getMessage()); } ///////////////////////////////////////////////////// $sql = "SELECT id FROM members where username = ?"; $stmt = $base->prepare($sql); $stmt->bind_param("s", $_GET['name']); $stmt->execute(); $result = $stmt->get_result(); $row = $result->fetch_object(); echo $row->id; ?>

  • that looks correct.

    But please, post your code as JS. It's much easier to read.

  • <?php
    
    try
    {
     $bdd = new PDO('mysql:host=localhost;dbname=stockage;charset=utf8', 'root', '');
    }
    catch (Exception $e)
    {
     die('Erreur : ' . $e->getMessage());
    }
    
    	$sql = "SELECT id FROM user where username = ?";
    	$stmt = $base->prepare($sql);
    	$stmt->bind_param("s", $_GET['name']);
    	$stmt->execute();
    	$result = $stmt->get_result();
    	$row = $result->fetch_object();
    	echo $row->id;
    
    ?>
  • Ok, does it work?

  • no /:

  • You changed the sql?

    SELECT id FROM members where username = ?

    SELECT id FROM user where username = ?

    Which is the correct name for the table you're trying to read from?

    What errors are you getting if any? What is the AJAX.LastRequest showing?

  • I have to create my table.

    she is called user.

    AJAX.LastRequest shows nothing.

  • Then you need to check and see if the php file is actually working by going directly to the URL.

    http://www.mysite.com/phpfilename.php?name=username.

    Nextly, you removed the

    header('Access-Control-Allow-Origin: *');

    which means, when AJAX tries to connect to that php file, it will get an error and/or return nothing.

  • on my php page.

    3 errors.

     Notice: Undefined variable: base in C:\wamp64\www\login.php on line 13
    
    Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\wamp64\www\login.php on line 13
    
    Error: Call to a member function prepare() on null in C:\wamp64\www\login.php on line 13
    
  • Because now you changed $base to $bdd

    <?php
    
    try
    {
     $base = new PDO('mysql:host=localhost;dbname=stockage;charset=utf8', 'root', '');
    }
    catch (Exception $e)
    {
     die('Erreur : ' . $e->getMessage());
    }
    	$sql = "SELECT id FROM user where username = ?";
    	$stmt = $base->prepare($sql);
    	$stmt->bind_param("s", $_GET['name']);
    	$stmt->execute();
    	$result = $stmt->get_result();
    	$row = $result->fetch_object();
    	echo $row->id;
    
    ?>

    Assuming you have a table named user with a field username and a field id (and those are case sensitive) this should work.

    login.php?name=theusername should give you the ID

    Once it works, but this back at the top:

    header('Access-Control-Allow-Origin: *');

  • I realized afterwards. except that the error persists.

    Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\wamp64\www\login.php on line 13
    
    Error: Call to a member function prepare() on null in C:\wamp64\www\login.php on line 13
    
  • Ok, I'm wholly unfamiliar with PDO and how it connects but the error you're getting is because it's failing to make a connection to the database so we need to do some error checking. I'm making a guess that you're using MySQL so let's try establishing a mysql connection instead:

    You currently have the PDO password set to '' which means, you aren't using one. I have yet to see an install of MySQL that allows no password on the root account unless you specifically set it to none.

    In the script below, you're going to need to replace DB_PASSWORD with the password you used when you set up the root account and put it inside single quotes like this:

    'password'

    If you're sure you don't have a password for that database then replace DB_PASSWORD with:

    ''

    Hopefully, this will work. Just run this from the browser. Once it does work, you can add in the:

    header('Access-Control-Allow-Origin: *');

    Back at the top.

    Take note, this produces a string as the AJAX result. From what I saw you had the result set to a number. If this does work, you will need to change it to see the errors.

    <?php
    $base = new mysqli('localhost', 'root', DB_PASSWORD, 'stockage');
    // Check connection
    if ($base->connect_errno) {
     die("Failed to connect to MySQL: " . $base->connect_error);
    }
    
    $sql = "SELECT id FROM user where username = ?";
    
    if (!$stmt = $base->prepare($sql))
    {
     echo "Prepare failed: (" . $stmt->errno . ") " . $stmt->error;
    }
    if (!$stmt->bind_param("s", $_GET['name'])){
     echo "Bind failed: (" . $stmt->errno . ") " . $stmt->error; 
    }
    if (!$stmt->execute()){
     echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; 
    }
    if (!$result = $stmt->get_result()) {
     echo "Get result failed: (" . $stmt->errno . ") " . $stmt->error; 
    }
    if ($result->num_rows > 0) { 
     $row = $result->fetch_object();
     echo $row->id;
    }
    else
    {
     echo 'No user found with that name';
    }
    ?>
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)