php mysql query not working

Thread Starter

metric_electric

Joined Aug 21, 2013
33
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
 

kubeek

Joined Sep 20, 2005
5,795
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.
 

joeyd999

Joined Jun 6, 2011
5,287
From this page:

Code:
<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');/*
* This is the "official" OO way to do it,
* BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
*/if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
}/*
* Use this instead of $connect_error if you need to ensure
* compatibility with PHP versions prior to 5.2.9 and 5.3.0.
*/if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}

echo 'Success... ' . $mysqli->host_info . "\n";$mysqli->close();?>
 

Thread Starter

metric_electric

Joined Aug 21, 2013
33
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
 

sirch2

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

Code:
$connection = mysql_connect("localhost","root");
if(!$connection) {
   die("Database connection failed: " . mysql_error());
}else{
   $db_select = mysql_select_db("nameofdatabase",$connection);
   if (!$db_select) {
       die("Database selection failed:: " . mysql_error());
   }
}
 
Top