php mysql query not working

Discussion in 'Programmer's Corner' started by metric_electric, Dec 8, 2014.

  1. metric_electric

    Thread Starter New Member

    Aug 21, 2013
    19
    0
    please I need help with figuring out whats wrong with my code. I cannot seem to pull data from my table on localhost phpmyadmin. Below are my codes.


    database connect file:
    <?php


    error_reporting(E_ALL);

    $db = new mysqli("127.0.0.1","root","","test"); // connect to database (test) with user name password and table


    if($db->connect_error)
    {

    echo 'could not connect to database server...connection failed';
    }
    else
    {
    echo 'connected to database server....<br> <br>';


    }
    ?>


    login file:

    <?php

    include 'core/init.php'; // include users.php and database connection




    if (empty($_POST) == false){
    $username = $_POST['username'];
    $password = $_POST['password'];


    If(empty($username) == true || empty($password) == true){ // if username and password fields are empty
    $errors[] = 'you need to enter username and password';
    }else if(user_exists($username) == false){ // if user does not exist
    $errors[] = 'username does not exist, please register';
    }else if (user_active($username) == false ){ //if user is not active
    $errors[] = 'you have not activated your account';
    }else{
    $login = login($username,$password);
    if($login == false){
    $errors[]= 'username and password is not correct';
    }else{
    //set user sessions
    // redirect user to home

    }
    }

    print_r($errors); // print error message corresponding to conditional statement

    }

    ?>


    users.php:
    <?php

    //include 'general.php'

    function user_exists($username){
    $username = sanitize($username);
    $query=mysql_query("SELECT user_id FROM lr WHERE username='$username' "); // line 7, table
    // table name is lr
    return(mysql_result($query,0)==1) ? true : false; // line 8

    }
    // more codes below but not applied to this problem
    ?>

    output message:
    connected to database server....


    Warning: mysql_result() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/lr/core/functions/users.php on line 8
    Array ( [0] => username does not exist, please register )





    please can anyone assist. When I put in my username and password it says ..."username does not exist, please register". I use the same username and password (MD5 encrypted) to login into my website. I am new to php mysql. I am following a tutorial online
     
  2. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    First of all, please use code tags. Second, where are you connecting to the db? Apparently mysql_query is returning false as it failed. Use some error checking to see why.
     
  3. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,674
    2,724
    From this page:

    Code (Text):
    1.  
    2. <?php
    3. $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');/*
    4. * This is the "official" OO way to do it,
    5. * BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
    6. */if ($mysqli->connect_error) {
    7. die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
    8. }/*
    9. * Use this instead of $connect_error if you need to ensure
    10. * compatibility with PHP versions prior to 5.2.9 and 5.3.0.
    11. */if (mysqli_connect_error()) {
    12. die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
    13. }
    14.  
    15. echo 'Success... ' . $mysqli->host_info . "\n";$mysqli->close();?>
    16.  
    17.  
     
    kubeek likes this.
  4. metric_electric

    Thread Starter New Member

    Aug 21, 2013
    19
    0
    kubeek & joeyd999

    I have been successfully connecting to the database. I changed my query code to the below - I added the mysql error check.

    users.php:
    function user_exists($username){
    $username = sanitize($username);
    $query=mysql_query("SELECT COUNT(user_id) FROM lr WHERE username='$username' ");
    if($query==FALSE){
    die("error2: ".mysql_error());
    }else{
    return(mysql_result($query,0)==1) ? true : false;
    }


    I got the error message below:

    output:
    connected to database server....

    error2: No database selected
     
  5. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    Answer Kubeek's question, show the code that connects to the database. Code like this is one approach

    Code (Text):
    1. $connection = mysql_connect("localhost","root");
    2. if(!$connection) {
    3.    die("Database connection failed: " . mysql_error());
    4. }else{
    5.    $db_select = mysql_select_db("nameofdatabase",$connection);
    6.    if (!$db_select) {
    7.        die("Database selection failed:: " . mysql_error());
    8.    }
    9. }
     
Loading...