Jump to content

Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in


AltarofScience

Recommended Posts

I got the error in the title. Here is my code:

<?php
mysql_connect("localhost", "admin", "admin") or die(mysql_error());
echo "Connected to MySQL<br />";
?>
<?php
   if($_POST) {
      $password = $_POST['password'];
      $confirm = $_POST['confirm'];   
      if($password != $confirm) { ?>
<span style='color:red'>Error: Passwords do not match!</span>      
<?php   } else {
         $dbhost = 'localhost';
         $dbuser = 'admin';
         $dbpass = 'admin';
         $dbname = 'aosdb';
         $conn = mysql_connect($dbhost,$dbuser,$dbpass)
            or die ('Error connecting to mysql');
         mysql_select_db($dbname);
         $query = sprintf("SELECT COUNT(id) FROM 'users' WHERE UPPER('username') = UPPER('%s')",
            mysql_real_escape_string($_POST['username']));
         $result = mysql_query($query);
         list($count) = mysql_fetch_row($result);
         if($count >= 1) { ?>
<span style='color:red'>Error: that username is taken.</span>
<?php      } else {
            $query = sprintf("INSERT INTO users('username','password') VALUES ('%s','%s');",
               mysql_real_escape_string($_POST['username']),
               mysql_real_escape_string(md5($password)));
            mysql_query($query);
         ?>
<span style='color:green'>Congratulations, you registered successfully!</span>
<?php
         }   
      }
   }
?>
<form method='post' action='index.php'>Username: <input type='text' name='username' /><br />
Password: <input type='password' name='password' /><br />
Confirm Password: <input type='password' name='confirm' /><br />
<input type='submit' value='Register!' />
</form>

 

MOD EDIT: code tags added.

Link to comment
Share on other sites

So I made these changes to the code and it does not throw an error anymore:

<?php
mysql_connect("localhost", "admin", "admin") or die(mysql_error());
echo "Connected to MySQL<br />";
?>
<?php
   if($_POST) {
      $password = $_POST["password"];
      $confirm = $_POST["confirm"];   
      if($password != $confirm) { ?>
<span style="color:red">Error: Passwords do not match!</span>      
<?php   } else {
         $dbhost = "localhost:3306";
         $dbuser = "root";
         $dbpass = "root";
         $dbname = "aosdb";
         $conn = mysql_connect($dbhost, $dbuser, $dbpass)
            or die ("Error connecting to mysql");
         mysql_select_db($dbname);
         $query = sprintf("SELECT COUNT(id) FROM users WHERE UPPER('username') = UPPER('%s')",
            mysql_real_escape_string($_POST["username"]));
         $result = mysql_query($query);
         list($count) = mysql_fetch_row($result);
         if($count >= 1) { ?>
<span style="color:red">Error: that username is taken.</span>
<?php      } else {
            $query = sprintf("INSERT INTO users('username', 'password') VALUES ('%s', '%s');",
               mysql_real_escape_string($_POST["username"]),
               mysql_real_escape_string(md5($password)));
            mysql_query($query);
         ?>
<span style="color:green">Congratulations, you registered successfully!</span>
<?php
         }   
      }
   }
?>
<form method="post" action="index.php">Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
Confirm Password: <input type="password" name="confirm" /><br />
<input type="submit" value="Register!" />
</form>

Link to comment
Share on other sites

It isn't throwing an error anymore because there is no logic in the code that would allow it to do so. MySQL database/table/field names don't get enclosed in quotes. If you feel compelled to enclose them in something, use `backticks`. When you're working with database queries, you should incorporate logic to check not only whether the query executed error free, but also that the results are what you would expect.

 

$query = "SELECT field_a FROM table WHERE field_b = '$condition'";
if( !$result = mysql_query( $query ) ) {
// query failed to execute
trigger_error( "DB query: $query failed with error: " . mysql_error() . " in " . __FILE__ . " on line: " . __LINE__ ); // can also use echo intstead of trigger_error() for debugging
} else {
if( mysql_num_rows($result) === 0 ) {  // INSERT/UPDATE queries use mysql_affected_rows()
// results set was empty, so handle that condition appropriately
} else {
	// query both executed successfully and returned results, so continue with the script, process the results
}
}

Link to comment
Share on other sites

Notice: DB query: SELECT COUNT(id) FROM users WHERE UPPER(username) = UPPER('AltarofScience') failed with error: Unknown column 'username' in 'where clause' in C:\Server\www\myserver.dev\public_html\Registration.php on line: 24 in C:\Server\www\myserver.dev\public_html\Registration.php on line 24

 

i am certain i did everything right though. i made sure username was spelled correctly in the table. i exported the model to get the sql file. i opened the client and did the file load thing. i got the file in the command line client and picked a database and the show table function shows table users. i tested my connection in workbench and it worked.

Link to comment
Share on other sites

Add the correct mysql_connect() and mysql_select_db() to the top of this, and let's see what that table really holds.

 

// add your DB connection credentials

$query = "DESCRIBE users";

if( $result = mysql_query($query) ) {
echo '<pre>';
while( $array = mysql_fetch_assoc($result) ) {
	print_r($array);
}
echo '</pre>';
} else {
echo mysql_error();
}

Link to comment
Share on other sites

it keeps returning no database selected.

the code i ran might have some errors but here it is:

<?php
$dbhost = "localhost:3306";
$dbuser = "admin";
$dbpass = "admin";
$dbname = "aosdb";
mysql_connect($dbhost, $dbuser, $dbpass)   
or die ("Error connecting to mysql");      
mysql_select_db($dbname);
$query= "DESCRIBE users";
if( $result = mysql_query($query) ) {
echo '<pre>';
while( $array = mysql_fetch_assoc($result) ) {
print_r($array);
}
echo '</pre>';
} else {
echo mysql_error();
}
?>

okay it was being whiny about using admin, here is the result if i use root root:

Array
(
    [Field] => id
    [Type] => int(11)
    [Null] => NO
    [Key] => PRI
    [Default] => 
    [Extra] => 
)
Array
(
    [Field] => usersname
    [Type] => varchar(50)
    [Null] => NO
    [Key] => 
    [Default] => 
    [Extra] => 
)
Array
(
    [Field] => password
    [Type] => varchar(25)
    [Null] => NO
    [Key] => 
    [Default] => 
    [Extra] => 
)

 

Link to comment
Share on other sites

the test script now returns the arrays below, which is what i wanted. and after i changed all the things back to username, it appears that the login and register scripts work, seeing as the login error for not finding a name doesn't come up. thanks to everyone who helped out, even if i was a little slow. and also thanks for showing me all that error checking stuff.

<code>

Array

(

    [Field] => id

    [Type] => int(11)

    [Null] => NO

    [Key] => PRI

    [Default] =>

    [Extra] => auto_increment

)

Array

(

    [Field] => username

    [Type] => varchar(45)

    [Null] => NO

    [Key] =>

    [Default] =>

    [Extra] =>

)

Array

(

    [Field] => password

    [Type] => varchar(45)

    [Null] => NO

    [Key] =>

    [Default] =>

    [Extra] =>

)

Array

(

    [Field] => is_admin

    [Type] => tinyint(4)

    [Null] => NO

    [Key] =>

    [Default] => 0

    [Extra] =>

)

Array

(

    [Field] => last_login

    [Type] => timestamp

    [Null] => YES

    [Key] =>

    [Default] =>

    [Extra] =>

)

</code>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.