I'm creating my own blogging site. I cant seem to figure out whats wrong with this for 2 days. What I'm trying to do is get the author id from a table and then get author information from another table.
// Get post author
$post_id = $_GET["view"];
$post_row = mysql_fetch_assoc(mysql_query("SELECT post_author FROM posts WHERE post_id='$post_id'"));
$post_author = $post_row["post_author"];
// Get post author information
$author_row = mysql_fetch_assoc(mysql_query("SELECT id FROM users WHERE id='$post_author'"));
$author_username = $author_row["user_name"];
echo $author_username;
However, I'm getting this error message, Notice: Undefined index: user_name in... I'm sure I've spelled the field name right. There's nothing wrong with the first part of the code. How is the second part a problem? There exactly the same.
You do
SELECT id
but then you look for the row user_name. It's not there, so you get the warning that the index isn't there.
You are selecting column id in your query, but then you are using $author_row["user_name"].
change it to $author_row["id"] or use SELECT user_name in your query
You are selecting only field id from the database. user_name is not there because you haven't selected it.
$author_row = mysql_fetch_assoc(mysql_query("SELECT id, user_name FROM users WHERE id='$post_author'"));
Debugging tip: Always do print_r( $author_row ) to see what the query actually returns in cases like this. Not applicable in this case but equally important is to do echo mysql_error() to check that there wasn't an error in the query itself.
$author_row = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE id='$post_author'"));
use this query to get all information of the user.
Related
I have table which contain [Username, Email] and I'm checking them for non repeating any of them,
but It's case-sensitive , so if there is user that's Username is "SelvsterTP", if other user typed it, he won't be able to register, but if he type "selvstertp" for example, no errors face him! ,
I think of making extra column called 'UsernameCheck' and upload to it Username 'lowercase' , then check on that column (same with Email) ,
but it seems to me not the best code for that situation, so any ideas or suggestions?
Original code
$CheckusernameRow =
RowCountDB("Id","users","Username",$Username); //function
to get rows
My Idea
$CheckusernameRow =
RowCountDB("Id","users","UsernameCheck",strtolower($Username));
Did you try the LOWER() selector?
$lowerUsername = strtolower($Username);
$query = "SELECT id FROM users WHERE LOWER(Username) LIKE '$lowerUsername'";
here is my prob. :
I got a users table with a column "music_style" who get value from a select input
example : rock, metal, pop,...
I want to select all other users who have the same "music_style" as me.
Example : If I put "metal" on my profile, i want to see all other profile with metal.
I hope to have been clear, sorry for my bad english :s
Assuming that you are using an sql based dbms, you may use the following sql statement:
SELECT * FROM `USERSTABLE` WHERE `music_style` = '$myStyle';
$myStyle is the php variable where you are storing the input.
Thanks for your answer, it work perfectly. But it select all user with the same music_style, including me. I tried to correct this but it didn't work .
I tried this :
$id = $_GET['id']
("SELECT * FROM users WHERE `music_style` = '$myStyle' AND 'id' != '$id' ");
i have a user database and i have to assign a user id for them for example AA01
AND after insert 1 person into database i have to insert another person with user id AA02 AND i have increment it and so on.
what i have already tried is
$sql_user_id=mysql_query("SELECT MAX(user_id) FROM `users` desc LIMIT 1");
$new_array = explode('AA',$sql_user_id);
$number=$new_array[1];
$newnumber=$number+1;
and i am getting wrong with result Resource id #5
You have to fetch the query results before you can use them.
$result=mysql_query("SELECT MAX(user_id) AS maxId FROM `users` desc LIMIT 1");
$sql_user_id = mysql_fetch_assoc($result);
$new_array = explode('AA',$sql_user_id['maxId']);
$number=$new_array[1];
$newnumber=$number+1;
FYI, I added an alias to your query as it makes referencing a value returned from a function much easier to do.
mysql_query() returns a resource, not a value you can operate directly. You have to fetch the value from this resource first:
$res = mysql_query('SELECT MAX(`user_id`) FROM `users`');
$val = mysql_fetch_row($res);
$new_array = explode('AA', $val[0]);
...
Also, notice that MAX() causes an implicit grouping to happen. So there is no order, and there is only one result. Specifying any of this in your query is futile.
In addition, notice that, since user_id is aparently a text, it might not work as expected. When sorting text, 2 comes after 10. You may need to use a different reference to fetch the latest user_id. If you can help it, don't use text to index your records.
Hi there i am working on PHP code that is selecting columns from two tables.
Here is my code:
$result2 = mysql_query("SELECT *
FROM `videos`, `m_subedvids`
WHERE `videos.approved`='yes' AND
`videos.user_id`='$subedFOR'
ORDER BY `videos.indexer`
DESC LIMIT $newVID");
while($row2 = mysql_fetch_array($result2))
{
$indexer = addslashes($row2['videos.indexer']);
$title_seo = addslashes($row2['videos.title_seo']);
$video_id = addslashes($row2['videos.video_id']);
$title = addslashes($row2['videos.title']);
$number_of_views = addslashes($row2['videos.number_of_views']);
$video_length = addslashes($row2['videos.video_length']);
}
When i try to print $indexer with echo $indexer; it's not giving me any results.
Where is my mistake in this code?
It seems to me like the key 'indexer' isn't in your results. It's hard to tell, since you haven't listed a definition for your table and you're using SELECT * so we can't see the names.
It makes the program easier to read later, if instead of SELECT *..., you use SELECT col1, col2, .... Yes, SELECT * will save you some typing right now, but you'll lose that time later when you or anyone else who works on your code has to check the table definition every time they work with that line of code.
So, try changing your query to explicitly select the columns you use. If it's an invalid column you'll get an error right away rather than this silent failure you're getting now, and you'll thank yourself later as well.
So long as videos.indexer is a unique field name among all tables used in the query you can change
$indexer = addslashes($row2['videos.indexer']);
to
$indexer = addslashes($row2['indexer']);
You don't need to (or can not) use the table name when referring to the result.
I've run into a SUPER weird error and can't seem to find out what it is. I'm running a PHP loop to query data from mySQL and it doesn't seem to work. My query looks like this
$q_routes = "SELECT * FROM routes";
$r_routes = mysql_query($q_routes);
while ($row_routes = mysql_fetch_assoc($r_routes)) {
$route_id = $row_routes['route_id'];
$route_name = $row_routes['route_short_name'];
}
When I echo the $route_name it shows a list of the route names. When I echo the $route_id, it has a list of blank fields.
I've gone into phpmyadmin to run a query for "SELECT route_id FROM routes and it says that route_id does not exist. When I do the same with route_name it exists. Is there any reason for this?
Table Structure
Inside the while loop do:
var_dump($row_routes);
This will show you what fields have been fetched, along with their exact spelling and the number of characters in the strings, in case there are hidden or whitespace characters.