bastardnoise Posted May 25, 2009 Share Posted May 25, 2009 I've been working on my first php/mysql page. Obviously, I am having some difficulties with mysql. I've been trying to use mysql_fetch_assoc to grab specific information from a database so that I can display it elsewhere. Here is the code I've been using <?php // Make a MySQL Connection mysql_connect("xxx", "xxx", "xxx") or die(mysql_error()); mysql_select_db("xxx") or die(mysql_error()); //Getting specific information the database //database = david_weintraub //column = planned_hours //row = $week_1 $p_1_f="SELECT planned_hours FROM david_weintraub WHERE week='$week_1'"; $p_1=mysql_query($p_1_f) or die(mysql_error()); $p_1_row=mysql_fetch_assoc($p_1); echo $p_1_row['planned_hours']; ?> This doesn't work. It echos nothing back and the page is blank. I'm baffled as to what I am doing wrong. Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/159521-mysql_fetch_assoc-help-for-a-beginner/ Share on other sites More sharing options...
Maq Posted May 25, 2009 Share Posted May 25, 2009 Where is $week_1 initialized? You can echo out the query to see exactly what's being queried. Quote Link to comment https://forums.phpfreaks.com/topic/159521-mysql_fetch_assoc-help-for-a-beginner/#findComment-841454 Share on other sites More sharing options...
bastardnoise Posted May 25, 2009 Author Share Posted May 25, 2009 I have a separate page where users submit both planned_hours and week into the database. Does this answer your question? I'm not entirely sure what you mean by 'initialized'. How do I echo out the query to see exactly what's being queried? Quote Link to comment https://forums.phpfreaks.com/topic/159521-mysql_fetch_assoc-help-for-a-beginner/#findComment-841456 Share on other sites More sharing options...
Maq Posted May 25, 2009 Share Posted May 25, 2009 I'm not entirely sure what you mean by 'initialized'. You're using '$week_1' in your query, where do you assign it a value? How do I echo out the query to see exactly what's being queried? echo $p_1_f; Quote Link to comment https://forums.phpfreaks.com/topic/159521-mysql_fetch_assoc-help-for-a-beginner/#findComment-841459 Share on other sites More sharing options...
bastardnoise Posted May 25, 2009 Author Share Posted May 25, 2009 You're using '$week_1' in your query, where do you assign it a value? I have a page called david_weintraub.php where $week_1 is assigned a value. echo $p_1_f; When put in my script, this code reads out: SELECT planned_hours FROM david_weintraub WHERE week='' Quote Link to comment https://forums.phpfreaks.com/topic/159521-mysql_fetch_assoc-help-for-a-beginner/#findComment-841462 Share on other sites More sharing options...
Maq Posted May 25, 2009 Share Posted May 25, 2009 I have a page called david_weintraub.php where $week_1 is assigned a value. You need to pass it to the page where you're performing the query. You can use multiple methods, GET, POST, sessions, etc... it all depends on what/how you want to pass it. Please post some pertinent code from david_weintraub.php where '$week_1' gets its value. When put in my script, this code reads out: SELECT planned_hours FROM david_weintraub WHERE week='' That's exactly my point. '$week_1' does not have a value, so you're comparing week to nothing which doesn't return anything. Quote Link to comment https://forums.phpfreaks.com/topic/159521-mysql_fetch_assoc-help-for-a-beginner/#findComment-841463 Share on other sites More sharing options...
bastardnoise Posted May 25, 2009 Author Share Posted May 25, 2009 You need to pass it to the page where you're performing the query. You can use multiple methods, GET, POST, sessions, etc... it all depends on what/how you want to pass it. Please post some pertinent code from david_weintraub.php where '$week_1' gets its value. Here is the code used in david_weintraub.php where '$week_1' gets its value. $week_1 = 1; mysql_query("INSERT INTO david_weintraub('week') VALUES ('$week_1'); Can you give me an example of how I could pass the information submitted by the david_weintraub.php page to the page I'd like it too (for now that page is called practice.php)? It may help if you see the david_weintraub.php page here: http://barchardslab.site50.net/students/david_weintraub.php Quote Link to comment https://forums.phpfreaks.com/topic/159521-mysql_fetch_assoc-help-for-a-beginner/#findComment-841474 Share on other sites More sharing options...
Maq Posted May 25, 2009 Share Posted May 25, 2009 Are you supposed to let the user choose a week? How are you supposed to know what week it is? The reason I'm asking is because if you're hard-coding $week_1 to '1', then you mine as well just hard code it in the file you perform the query... Quote Link to comment https://forums.phpfreaks.com/topic/159521-mysql_fetch_assoc-help-for-a-beginner/#findComment-841481 Share on other sites More sharing options...
bastardnoise Posted May 25, 2009 Author Share Posted May 25, 2009 The reason I'm asking is because if you're hard-coding $week_1 to '1', then you mine as well just hard code it in the file you perform the query... Okay, I'll do this. I'm still confused as to how to pass information from my david_weintraub.php page to my practice.php. Why can't I access the information submitted into my mysql database through david_weintraub.php when I connect into the same database in both pages. All I want to do with the practice.php is simply display specific information from the database in a customized table. Can you provide me an example of a GET, POST, sessions, etc... method that'd be appropriate for me? Quote Link to comment https://forums.phpfreaks.com/topic/159521-mysql_fetch_assoc-help-for-a-beginner/#findComment-841498 Share on other sites More sharing options...
Maq Posted May 25, 2009 Share Posted May 25, 2009 Something like this? A user can enter a number in the next field (week) and it will submit to the same page to check the database. if(isset($_POST['submit'])) { $week_1 = $_POST['week']; // Make a MySQL Connection mysql_connect("xxx", "xxx", "xxx") or die(mysql_error()); mysql_select_db("xxx") or die(mysql_error()); //Getting specific information the database //database = david_weintraub //column = planned_hours //row = $week_1 $p_1_f="SELECT planned_hours FROM david_weintraub WHERE week='$week_1'"; $p_1=mysql_query($p_1_f) or die(mysql_error()); $p_1_row=mysql_fetch_assoc($p_1); echo $p_1_row['planned_hours']; } ?> </pre> <form method="POST" action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>"> < Quote Link to comment https://forums.phpfreaks.com/topic/159521-mysql_fetch_assoc-help-for-a-beginner/#findComment-841499 Share on other sites More sharing options...
bastardnoise Posted May 25, 2009 Author Share Posted May 25, 2009 Something like this? A user can enter a number in the next field (week) and it will submit to the same page to check the database. It is not necessary for a user to enter a week and have that number submit to the same page to check the database. I want to display specific information in the same row as $week_1 into a table. And I would do this for 17 different weeks all in the same table. Quote Link to comment https://forums.phpfreaks.com/topic/159521-mysql_fetch_assoc-help-for-a-beginner/#findComment-841501 Share on other sites More sharing options...
Maq Posted May 25, 2009 Share Posted May 25, 2009 You need to use some sort of loop from your query to create the table and insert the values. i.e. </pre> <table border="1"> Week Planned Hours for($i=0; $i{ ?> hours } ?> </ You're also going to need to change your query to select all the planned hours for each week (take out the where condition). Quote Link to comment https://forums.phpfreaks.com/topic/159521-mysql_fetch_assoc-help-for-a-beginner/#findComment-841502 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.