Jump to content

Looping through my database and checking if that info is identical to a variable


Mythic Fr0st

Recommended Posts

This stores all my info into those variables, however I need to loop through my database, and check if there is any info in "user" that matches the entered username (to signup)...

 

if ($user == $row['user'])

 

will return the first bit of information stored in my row 'user', wont it? if so how do I return ALL of it, and could you give me an example please, thanks in advance

 

<?php
    //req files
    require("C:\Program Files\EasyPHP1-8\www\Main Files\Require Files\Notice.php");
    require("C:\Program Files\EasyPHP1-8\www\Main Files\Require Files\Banner.php");
    require("C:\Program Files\EasyPHP1-8\www\Main Files\Require Files\mysqlconnect.php");
    require("C:\Program Files\EasyPHP1-8\www\Main Files\Require Files\buttons.css");
    //begin login check
    $user = $_POST['user'];
    $pw1 = $_POST['pw1'];
    $pw2 = $_POST['pw2'];
    $em1 = $_POST['em1'];
    $em2 = $_POST['em2'];
    $hph = $_POST['hph'];
    $mph = $_POST['mph'];
    $loc = $_POST['locn'];
    $postcode = $_POST['pc'];
    $heard = $_POST['heard'];
    $con = $_POST['con'];
    $city = $_POST['city'];
    $result = mysql_query("SELECT * FROM `signup`")or die(return_error

(__FILE__,__LINE__,mysql_error()));
    $row = mysql_fetch_array($result);
    //add Where $id = 'id'
    $ident = array();

    if ($user ==  
    mysql_query("INSERT INTO signup(`user`,`pw1`,`pw2`) VALUES('$user','$pw1','$pw2')")or die

(return_error(__FILE__,__LINE__,mysql_error())); 
    
?>
<html>
    <head>
        <title>
            Girl Cafe
        </title>
    </head>
    <body bgcolor="pink">
    </body>
</html>

Not sure if I understand exactly what you want.

 

$sql = "SELECT id,name,address,telephone FROM users WHERE name LIKE '%john%'";

 

this will select all users where name is LIKE (look this up in google) 'john'.

 


$res = mysql_query( $sql );
while( $row = mysql_fetch_array( $res ) )
{
  echo row['name'];
  echo '<br>';
  echo row['address'];
}

 

I guess you need to put your logic inside the loop.

 

Hope that helps.

 

monk.e.boy

Thanks, but not quite what I meant

 

I mean, like I wanna check if the data in $user, is in the database, so you can't signup with someone elses name / Email address

 

How do I do that?

 

E.G

 

if ($user = any data from the row "user"

So Like

For ($i = 0; $i < number of registered users in my db; $i++)

    {

    if ($user == $row.1.['user'];

    }

 

I wanna check to make sure $user has not been used

you're close. use while instead of for:

<?php
        $db = mysql_connect("xxx", "xxx", "xxx");
        mysql_select_db("your_db");

        $sql = "SELECT * FROM your_table";
        $query = mysql_query($sql);

        while($row = mysql_fetch_array($query)){
                if($row['user_name'] == $_POST['user_name']){
                        /*print error*/
                }else{
                        /*continue to register new user*/
                }
        }
?>

 

alternatively, if memory serves this will work in PHP, but you may be able to use a foreach loop:

<?php
        $db = mysql_connect("xxx", "xxx", "xxx");
        mysql_select_db("your_db");

        $sql = "SELECT * FROM your_table";
        $query = mysql_query($sql);
        $row = mysql_fetch_array($query);
        foreach($row['user_name'] as $key => $val){
                if($val == $_POST['user_name']){
                        /*print error*/
                }else{
                        /*continue to register new user*/
                }
        }
?>

 

that may be faster (if it works, that is).

Seems awful complicated...

 

Why not just do a SELECT query with the $user in the WHERE clause and if it comes back with any rows then print a message saying that the username  has already been registered by someone else.

>> Seems awful complicated...

 

Why not just do a SELECT query with the $user in the WHERE clause and if it comes back with any rows then print a message saying that the username  has already been registered by someone else.

 

Thats what im trying to do, just check if "any" of them in that row are matching the data in $user

 

I can do what boo_lolly said (PS Thanks for the code boo_lolly) easily enough, but its quite large lol, I dont wanna have alot of those in my code

Are you trying to pull one row and see if the user name is in that one row, or pull all the rows and see if the username is in the table?

 

If its the latter:

 

//mysql stuff

$q = mysql_query("SELECT username FROM users WHERE username = '{$uname}'");
$n = mysql_num_rows($q);
if($n > 0) {
// it exists
}
else {
//it doesnt exist
}

Im trying to see if username is in that "one" row, should I use that code?

 

OH, i thought you wanted to check to see if that user is located in any row in the database. i don't see why you wouldn't want to tho. your welcome for the code. but my code will search the entire table to see if the username has already been taken. if you want to check a specific row, you need to narrow the query down to one single row, you do this by adding a WHERE clause, like so:

 

<?php
        $db = mysql_connect("xxx", "xxx", "xxx");
        mysql_select_db("your_db");

        $sql = "SELECT * FROM your_table WHERE something = 'something_else'";
        $query = mysql_query($sql);

        $numRows = mysql_num_rows($query);

        if($numRows < 1){
                /*continue to register new user*/
        }else{
                /*print error*/
        }
?>

Archived

This topic is now archived and is closed to further replies.

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