CBaZ Posted July 3, 2008 Share Posted July 3, 2008 the issue is this stores it in my database but all in a single row. also it keeps adding the names eventhough they already exist. <?php error_reporting(E_ALL) ; ini_set('display_errors','1'); ?> <?php $connect = mysql_connect("localhost", "user", "pw") or die ("Could not connect to database: " . mysql_error()); mysql_select_db("db", $connect) or die ("Could not select database"); $query = mysql_query("SELECT * FROM users WHERE username != '" . $username . "'") or die("QUERY FAILED: " . mysql_error()); $triv = file_get_contents("/trivia/users_online2.txt"); $date = date("YmdHis"); echo "$triv"; if (mysql_num_rows($query) >= 0) { for($i=0;$i<count($triv);$i++) { $result = "INSERT INTO users (username, created) values ('".$triv."', '".$date."')" ; }} else { echo "number of rows: '".mysql_num_rows($query)."'<br>"; exit; } $row2 = mysql_query($result) or die("QUERY FAILED: " . mysql_error()); $sql = mysql_query("SELECT * FROM users WHERE user_id != '$user_id' ORDER BY created"); ?> </code> Quote Link to comment https://forums.phpfreaks.com/topic/113024-usernames-being-added-in-mysql-from-txt-in-seperate-rows/ Share on other sites More sharing options...
CBaZ Posted July 3, 2008 Author Share Posted July 3, 2008 still need some help figuring out why it can not find existing username or how to manage this insert to be in seperated rows <code><?php error_reporting(E_ALL) ; ini_set('display_errors','1'); ?> <?php $connect = mysql_connect("localhost", "user", "pw"); mysql_select_db("db", $connect); $query = mysql_query("SELECT * FROM users WHERE username != '" . $username . "'"); $triv = file_get_contents("/trivia/users_online2.txt"); $date = date("YmdHis"); echo "$triv"; $sql = sprintf( 'SELECT `username` FROM `users` WHERE `username` = \'%s\' LIMIT 1', mysql_real_escape_string( '" . $username . "' ) ); $qry = mysql_query( $sql ); $rows = mysql_num_rows( $qry ); if ( FALSE === $rows ) { exit; } elseif ( 0 === $rows ) { for($i=0;$i<count($triv);$i++) { $result = "INSERT INTO users (username, created) values ('".$triv."', '".$date."')"; }} else { $username = ($triv); $count = mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `username`='".$username."'")); if (mysql_num_rows($count) > 0) { return "username exists"; }} $row2 = mysql_query($result) or die("QUERY FAILED: " . mysql_error()); $sql = mysql_query("SELECT * FROM users WHERE user_id != '$user_id' ORDER BY created"); ?></code> Quote Link to comment https://forums.phpfreaks.com/topic/113024-usernames-being-added-in-mysql-from-txt-in-seperate-rows/#findComment-581211 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.