
Valakai
Members-
Posts
23 -
Joined
-
Last visited
Never
Everything posted by Valakai
-
MySQL Fetch all rows into array using column as key
Valakai replied to Valakai's topic in PHP Coding Help
Ok, thank you for the input. I'll stick to a numeric index array. -
Well I'd suggest then to search for tutorials on session_*() functions, storing/retrieving information from a database and handling form inputs.
-
Well one solution would be to use str_replace like this $text = str_replace('\'', '\'\'', $text) Though I doubt that would be a foolproof solution. -- Gah, need to sleep soon, making too many mistakes.
-
Whoops thats MySQL.
-
Wrap your variables with addslashes(), which will escape the quotes and allow MSSQL to store them.
-
Well theres no quotes around the value= attributes in the input elements. Can you do an echo() or var_dump() on $tab and $id?
-
The brace on line 10 is closing ( } ) and not opening ( { ) which is required after an if() Correct code: mysql_connect(localhost,root,""); mysql_select_db(prerentdb) or die( "Unable to select database"); if(!empty($_POST["submit"])) { $apt = $_POST['apt']; $query="SELECT * FROM payments Where apt='$apt'"; $result=mysql_query($query); if(mysql_num_rows($result)) { $sql = "UPDATE payments SET amtpaid = '0', prevbal = '0', tentpay = '0', datepaid = ' ', late = ' ', paidsum = '0' WHERE paidsum = rentdue OR late = 'L'"; mysql_query($sql) or die("Update query failed."); echo "Records have been updated"; } }
-
Well there's some value (however slight) in them, but if you need/want something more advanced I'd suggest finding some open source software and taking it apart bit by bit.
-
MySQL Fetch all rows into array using column as key
Valakai replied to Valakai's topic in PHP Coding Help
I'm not sure if you are reading my code right, but to make it a bit simpler: while($row = mysql_fetch_assoc($query)) { $rows[$row['user_name']] = $row; } Would become: $rows['user1'] = array('user_id' => 1, 'user_name' => 'user1'); $rows['user2'] = array('user_id' => 1, 'user_name' => 'user2'); $rows['user3'] = array('user_id' => 1, 'user_name' => 'user2'); There is no overriding indexes, each value is unique and would create an array of the data. -
str_replace(' ', '', $text);
-
From a quick search http://www.phpeasystep.com/phptu/6.html http://www.1337labs.com/2010/12/23/multiple-admin-login-over-mysql-database-table/ http://www.php-mysql-tutorial.com/wikis/php-tutorial/basic-user-authentication.aspx
-
What is the value of $tab and $id? From the looks of the first error, $tab doesn't have a value and $id would be 11/' and in the second $tab is still empty and $id is a single quote
-
Can you please post the entire error string? There is no reason why that code would cause an error.
-
The code you submitted is perfectly fine, the rest of the code you didn't probably contains the error.
-
$sql = 'SELECT * FROM '.$tab.' WHERE '.$tab.'_id = \''.$id'\''; or $sql = "SELECT * FROM ".$tab." WHERE ".$tab."_id = '".$id."'"; both work.
-
MySQL Fetch all rows into array using column as key
Valakai replied to Valakai's topic in PHP Coding Help
Sorry, I copied the functions wrong In the first function $sql is just a place holder for whatever SQL I will want to execute. In the second function mysql_fetch_assoc($result) is meant to be mysql_fetch_assoc($query), $query being returned from a mysql_query() These functions would return all the records as the id_column I choose is going to be a unique id like the primary key. As an example if I ran this on a user database $rows = getRows(mysql_query('SELECT user_id, user_name, user_email FROM users'), 'user_id'); would return Array ( 1 => Array('user_id' => 1, 'user_name' => 'example1', 'user_email' => '[email protected]'), 2 => Array('user_id' => 2, 'user_name' => 'example2', 'user_email' => '[email protected]'), 3 => Array('user_id' => 3, 'user_name' => 'example3', 'user_email' => '[email protected]') ) -
If you want to keep the format Y/m/d, you can use the date() function on the $vip_purchased timestamp.
-
I would store the vip_purchased as an int(10) and populate it with the time() function at the time of purchase You can then check when 1 month has passed with this code // (time() - $vip_purchased) - Number of seconds since time of purchase // (60*60*24*31) - Number of seconds in 31 days if(((time() - $vip_purchased) > (60*60*24*31)) { // VIP Time Expired }
-
Function help -display many rows with same id-
Valakai replied to chris11's topic in PHP Coding Help
I'm not quite sure what you want, I think this is it though. echo(implode(',', $tags)); -
I've been using this code for a long time and realised it's very repetitive, but the id_column I want changes all the time function getRows() { $query = mysql_query($sql); $rows = array(); while($row = mysql_fetch_assoc($query)) { $rows[$row['id_column']] = $row; } return $rows; } So I wrote this function that will automatically create an array with a column I choose if I wish, but I'm not sure if it's very efficient. function getRows($query, $column_id = false) { $rows = array(); while($row = mysql_fetch_assoc($result)) { if($column_id === false) { $rows[] = $row; } else { if(isset($row[$column_id])) { $rows[$row[$column_id]] = $row; } else { $rows[] = $row; } } } return $rows; } I would appreciate some input as to make it better. Thanks.